iterable-unpacking

Python star unpacking for version 2.7

你离开我真会死。 提交于 2019-11-27 04:35:53
问题 As mentioned here, you can use the star for unpacking an unknown number of variables (like in functions), but only in python 3: >>> a, *b = (1, 2, 3) >>> b [2, 3] >>> a, *b = (1,) >>> b [] In python 2.7, the best I can come up with is (not terrible, but annoying): c = (1, 2, 3) a, b = c[0], c[1:] if len(c) > 1 else [] Is there a way to import this from __future__ like division, or will I need my own function to do unknown-length unpacking in python 2.7? 回答1: in python 2.X, you can do: c = (1,

How to extract dictionary single key-value pair in variables

允我心安 提交于 2019-11-27 02:02:55
问题 I have only a single key-value pair in a dictionary. I want to assign key to one variable and it's value to another variable. I have tried with below ways but I am getting error for same. >>> d = {"a": 1} >>> d.items() [('a', 1)] >>> (k, v) = d.items() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 1 value to unpack >>> (k, v) = list(d.items()) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 1

Why is Scala's syntax for tuples so unusual?

家住魔仙堡 提交于 2019-11-27 01:11:57
问题 In mathematics and computer science, a tuple is an ordered list of elements. In set theory, an (ordered) n-tuple is a sequence (or ordered list) of n elements, where n is a positive integer. So, for example, in Python the 2nd item of a tuple would be accessed via t[1] . In Scala, access is only possible via strange names t._2 . So the question is, why can't I access data in tuples as Sequence or List if it is by definition? Is there some sort of idea or just yet not inspected? 回答1: Scala

Meaning of using commas and underscores with Python assignment operator?

喜欢而已 提交于 2019-11-27 00:56:12
Reading through Peter Norvig's Solving Every Sudoku Puzzle essay , I've encountered a few Python idioms that I've never seen before. I'm aware that a function can return a tuple/list of values, in which case you can assign multiple variables to the results, such as def f(): return 1,2 a, b = f() But what is the meaning of each of the following? d2, = values[s] ## values[s] is a string and at this point len(values[s]) is 1 If len(values[s]) == 1 , then how is this statement different than d2 = values[s] ? Another question about using an underscore in the assignment here: _,s = min((len(values[s

Tuple Unpacking in Map Operations

笑着哭i 提交于 2019-11-27 00:11:50
问题 I frequently find myself working with Lists, Seqs, and Iterators of Tuples and would like to do something like the following, val arrayOfTuples = List((1, "Two"), (3, "Four")) arrayOfTuples.map { (e1: Int, e2: String) => e1.toString + e2 } However, the compiler never seems to agree with this syntax. Instead, I end up writing, arrayOfTuples.map { t => val e1 = t._1 val e2 = t._2 e1.toString + e2 } Which is just silly. How can I get around this? 回答1: A work around is to use case : arrayOfTuples

Getting only element from a single-element list in Python?

会有一股神秘感。 提交于 2019-11-26 22:26:40
When a Python list is known to always contain a single item, is there way to access it other than: mylist[0] You may ask, 'Why would you want to?'. Curiosity alone. There seems to be an alternative way to do everything in Python. Sequence unpacking: singleitem, = mylist # Identical in behavior (byte code produced is the same), # but arguably more readable since a lone trailing comma could be missed: [singleitem] = mylist Explicit use of iterator protocol: singleitem = next(iter(mylist)) Destructive pop: singleitem = mylist.pop() Negative index: singleitem = mylist[-1] Set via single iteration

Change what the *splat and **splatty-splat operators do to my object

狂风中的少年 提交于 2019-11-26 21:16:00
问题 How do you override the result of unpacking syntax *obj and **obj ? For example, can you somehow create an object thing which behaves like this: >>> [*thing] ['a', 'b', 'c'] >>> [x for x in thing] ['d', 'e', 'f'] >>> {**thing} {'hello world': 'I am a potato!!'} Note: the iteration via __iter__ ("for x in thing") returns different elements from the *splat unpack. I had a look in operator.mul and operator.pow , but those functions only concern usages with two operands, like a*b and a**b , and

Extended tuple unpacking in Python 2

孤者浪人 提交于 2019-11-26 20:49:06
Is it possible to simulate extended tuple unpacking in Python 2? Specifically, I have a for loop: for a, b, c in mylist: which works fine when mylist is a list of tuples of size three. I want the same for loop to work if I pass in a list of size four. I think I will end up using named tuples, but I was wondering if there is an easy way to write: for a, b, c, *d in mylist: so that d eats up any extra members. You could define a wrapper function that converts your list to a four tuple. For example: def wrapper(thelist): for item in thelist: yield(item[0], item[1], item[2], item[3:]) mylist = [(1

How to unpack tuple of length n to m<n variables [duplicate]

馋奶兔 提交于 2019-11-26 20:05:20
问题 This question already has answers here : Extended tuple unpacking in Python 2 (4 answers) Closed 4 years ago . In Python 3 I can do the following (see also PEP3132 on Extended Iterable Unpacking): a, *b = (1, 2, 3) # a = 1; b = (2, 3) What can I do to achieve the same similarly elegant in Python 2.x? I know that I could use single element access and slicing operations, but I wonder if there is a more pythonic way. My code so far: a, b = (1, 2, 3)[0], (1, 2, 3)[1:] # a = 1; b = (2, 3) 回答1: I

How do I convert a tuple of tuples to a one-dimensional list using list comprehension? [duplicate]

假装没事ソ 提交于 2019-11-26 19:57:44
问题 This question already has an answer here: Flattening a shallow list in Python [duplicate] 23 answers I have a tuple of tuples - for example: tupleOfTuples = ((1, 2), (3, 4), (5,)) I want to convert this into a flat, one-dimensional list of all the elements in order: [1, 2, 3, 4, 5] I've been trying to accomplish this with list comprehension. But I can't seem to figure it out. I was able to accomplish it with a for-each loop: myList = [] for tuple in tupleOfTuples: myList = myList + list(tuple