tuples

Create a list of tuples from two nested lists

泄露秘密 提交于 2019-12-11 13:18:05
问题 Having a list A with an arbitrary degree of nesting, and a list B with a nesting structure equivalent to that of A ( or deeper ), how can we create a list of tuples for all corresponding elements? For example: A = ['a', ['b', ['c', 'd']], 'e'] B = [1, [2, [3, [4, 5]]], 6] >>> [('a', 1), ('b', 2), ('c', 3), ('d', [4, 5]), ('e', 6)] 回答1: Basically, all you need to do is, iterate a and b simultaneously and return the values of a and b , if the current element of a is not a list. Since your

Python tuple comparison odd behavior

╄→гoц情女王★ 提交于 2019-12-11 12:42:19
问题 Can someone please explain this behavior? In[11]: (1, 2) in [(True, 2)] Out[11]: True In[12]: (1, 2) in [(True, True)] Out[12]: False In[13]: (1, 2) in [(True, False)] Out[13]: False In[14]: ("1", 2) in [(True, 2)] Out[14]: False It feels like a bug--whenever I check if a tuple is in a list of tuples, the integer 1 is always equal to True. I don't want to report it to the Python bug tracker if it can be explained. 回答1: It's something of an implementation detail of True , see Is False == 0 and

How to join many “listed” tuples into one tuple in Python?

ぃ、小莉子 提交于 2019-12-11 12:32:17
问题 There are few answers for the problem on in Python, How to join a list of tuples into one list?, How to merge two tuples in Python?, How To Merge an Arbitrary Number of Tuples in Python?. All the answers refer to list of tuples , so the solutions provided there seem to be useless for me. Here is my problem, I have a file with tuples listed like this: (1, 5) (5, 3) (10, 3) (5, 4) (1, 3) (2, 5) (1, 5) I would like to join them in ONE tuple like this: ((1, 5), (5, 3), (10, 3), (5, 4), (1, 3), (2

Python: KeyError: 0 - function that creates a list from subsets of data frame accessed via a loop

耗尽温柔 提交于 2019-12-11 11:59:01
问题 I have a loop that creates subsets of a larger data frame, which are then used as input for a function for analysis. This function returns a list. I used prints to see where it stops. So for the first run in the loop I can see the subset, the list output of the function, but when it starts again, second run, I see the second subset but than at the function I get the following error: <ipython-input-11-8f6203e297e3> in ssd(x, y) 8 9 for i in range(x.shape[0]): ---> 10 spread_cumdiff += (x[i] -

python only show tuple items in loop x amount of times

青春壹個敷衍的年華 提交于 2019-12-11 11:51:25
问题 I need help finding a python function that will only show the value in the tuple, (x) amount of times. from random import * rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King") suit = ("hearts", "diamonds", "spades" , "clubs") users = ("I", "computer", "deck") NUMCARDS = 52 DECK = 0 PLAYER = 1 COMP = 2 count = 0 while (count < 52): for u in rankName: for i in suit: count = count + 1 w = choice(users) ''' 'computer' and 'I' should

Passing String, integer and tuple information as key for python dictionary

守給你的承諾、 提交于 2019-12-11 11:36:17
问题 I'm trying to create a python dictionary and I would like to use a key that contains strings, numerics & a list/tuple entry. The key should ideally look like ("stringA", "stringB", "stringC", integer1, (integer2, integer3, integer4)) I tried to create a namedtuple based on this documentation as follows from collections import namedtuple dictKey = namedtuple('dictKey', 'stringA stringB stringC integer1 (integer2 integer3 integer4)') but it throws me a ValueError saying it can only contain

how to make a list of tuples from a file in python

◇◆丶佛笑我妖孽 提交于 2019-12-11 11:23:26
问题 Ok I have a file like that looks like this. panite,1,1800 ruby,2,100 diamond,0.75,900 emerald,3,250 amethyst,2,50 opal,1,300 sapphire,0.5,750 benitoite,1,2000 malachite,1,60 Our teacher gave us code that uses a try/except to help us open the file. I need to open the file and read each line and make each line a tuple and then put it in a list. The list is supposed to be the last numbe divided by the middle number, and then that value followed by the name of the gem(the middle number is the

Yield a sequence of tuples instead of map

北城余情 提交于 2019-12-11 11:10:06
问题 This is my code. To my surprise, it yields a map instead of a seq of tuples as I expect. What is right way to get list of tuples in scala? for ((_, s) <- Constants.sites; line <- Source.fromFile(s"data/keywords/topkey$s.txt").getLines ) yield ((s, line)) 回答1: The reason probably is that Constants.sites is a Map , therefore it returns a map. Instead of running the comprehension over Constants.sites , run it over Constants.sites.values , you are only using the values anyway. The background is

Why is it always possible to access the .0 element of an optional tuple in Swift?

我是研究僧i 提交于 2019-12-11 11:06:40
问题 A feature in Swift has been making me wonder for a while... see the code below: class Clazz { var foo: String = "abc"; } let foo: Int = 1 let bar: Int? = 2 let baz: Clazz? = Clazz() let qux: Clazz = Clazz() let quux: (Int, String)? = (1, "abc") foo.0 //1 foo.0.0 //1 bar.0.0.0 //{Some 2} #optional baz.0 //{{foo "abc"}} #optional qux.0.0 //{foo "abc"} quux.0 //{(.0 1, .1 "abc")} #optional quux.1 //error: doesn't have a member named '1' quux!.1 //"abc" As I understand, it is because a tuple with

What am I doing wrong around adding an additional case class constructor which first transforms it parameters?

北城以北 提交于 2019-12-11 10:49:06
问题 So, I had a very simple case class: case class StreetSecondary1(designator: String, value: Option[String]) This was working just fine. However, I kept having places where I was parsing a single string into a tuple which was then used to build an instance of this case class: def parse1(values: String): StreetSecondary1 = { val index = values.indexOf(" ") StreetSecondary1.tupled( if (index > -1) //clip off string prior to space as designator and optionally use string after space as value