iterable-unpacking

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

﹥>﹥吖頭↗ 提交于 2019-11-27 19:58:47
This question already has an answer here: Extended tuple unpacking in Python 2 4 answers 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) moooeeeep I found out that the related PEP3132 gives some examples for Python 2.x as well: Many algorithms require splitting a

Invalid syntax python starred expressions

自古美人都是妖i 提交于 2019-11-27 17:46:22
问题 I am trying to unpack set of phone numbers from a sequence, python shell in turn throws an invalid syntax error. I am using python 2.7.1. Here is the snippet >>> record = ('Dave', 'dave@example.com', '773-555-1212', '847-555-1212') >>> name, email, *phone-numbers = record SyntaxError: invalid syntax >>> Please explain. Is there any other way of doing the same? 回答1: You are using Python 3 specific syntax in Python 2. The * syntax for extended iterable unpacking in assignments is not available

How can I explode a tuple so that it can be passed as a parameter list?

纵然是瞬间 提交于 2019-11-27 17:21:02
问题 Let's say I have a method definition like this: def myMethod(a, b, c, d, e) Then, I have a variable and a tuple like this: myVariable = 1 myTuple = (2, 3, 4, 5) Is there a way I can pass explode the tuple so that I can pass its members as parameters? Something like this (although I know this won't work as the entire tuple is considered the second parameter): myMethod(myVariable, myTuple) I'd like to avoid referencing each tuple member individually if possible... 回答1: You are looking for the

Swapping Columns with NumPy arrays

限于喜欢 提交于 2019-11-27 15:11:47
问题 When I have a=1 and b=2 , I can write a,b=b,a so that a and b are interchanged with each other. I use this matrix as an array: [ 1, 2, 0, -2] [ 0, 0, 1, 2] [ 0, 0, 0, 0] Swapping the columns of a numpy array does not work: import numpy as np x = np.array([[ 1, 2, 0, -2], [ 0, 0, 1, 2], [ 0, 0, 0, 0]]) x[:,1], x[:,2] = x[:,2], x[:,1] It yields: [ 1, 0, 0, -2] [ 0, 1, 1, 2] [ 0, 0, 0, 0] So x[:,1] has simply been overwritten and not transferred to x[:,2] . Why is this the case? 回答1: If you're

Star * operator on left vs right side of an assignment statement

a 夏天 提交于 2019-11-27 15:07:46
This questions stems from PEP 448 -- Additional Unpacking Generalizations and is present in Python 3.5 as far as I'm aware (and not back-ported to 2.x ). Specifically, in the section Disadvantages , the following is noted: Whilst *elements, = iterable causes elements to be a list , elements = *iterable , causes elements to be a tuple . The reason for this may confuse people unfamiliar with the construct. Which does indeed hold, for iterable = [1, 2, 3, 4] , the first case yields a list : >>> *elements, = iterable >>> elements [1, 2, 3, 4] While for the second case a tuple is created: >>>

asterisk in tuple, list and set definitions, double asterisk in dict definition

主宰稳场 提交于 2019-11-27 14:49:32
I'm playing now with Python 3.5 interpreter and found very interesting behavior: >>> (1,2,3,"a",*("oi", "oi")*3) (1, 2, 3, 'a', 'oi', 'oi', 'oi', 'oi', 'oi', 'oi') >>> [1,2,3,"a",*range(10)] [1, 2, 3, 'a', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> ('aw','aw',*range(10),*(x**2 for x in range(10))) ('aw', 'aw', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81) >>> {"trali":"vali", **dict(q=1,p=2)} {'q': 1, 'p': 2, 'trali': 'vali'} >>> {"a",1,11,*range(5)} {0, 1, 2, 3, 4, 11, 'a'} I have never seen that neither in documentation and examples nor in any source code despite several years of

Django - How to do tuple unpacking in a template 'for' loop

孤街醉人 提交于 2019-11-27 10:39:33
问题 In my views.py, I'm building a list of two-tuples, where the second item in the tuple is another list, like this: [ Product_Type_1, [ product_1, product_2 ], Product_Type_2, [ product_3, product_4 ]] In plain old Python, I could iteration the list like this: for product_type, products in list: print product_type for product in products: print product I can't seem to do the same thing in my Django template: {% for product_type, products in product_list %} print product_type {% for product in

Returning tuple with a single item from a function

谁说我不能喝 提交于 2019-11-27 09:25:29
Just came across this little bit of weirdness in Python and thought I'd document it write it as a question here in case anyone else is trying to find an answer with the same fruitless search terms I was Looks like tuple unpacking makes it so you can't return a tuple of length 1 if you're expecting to iterate over the return value. Although it seems that looks are deceiving. See the answers. >>> def returns_list_of_one(a): ... return [a] ... >>> def returns_tuple_of_one(a): ... return (a) ... >>> def returns_tuple_of_two(a): ... return (a, a) ... >>> for n in returns_list_of_one(10): ... print

Getting a Python function to cleanly return a scalar or list, depending on number of arguments

我的梦境 提交于 2019-11-27 07:01:28
问题 Disclaimer: I'm looking for a Python 2.6 solution, if there is one. I'm looking for a function that returns a single value when passed a single value, or that returns a sequence when passed multiple values: >>> a = foo(1) 2 >>> b, c = foo(2, 5) >>> b 3 >>> c 6 To be clear, this is in an effort to make some function calls simply look nicer than: a, = foo(1) or a = foo(1)[0] Right now, the inelegant solution is something along these lines: def foo(*args): results = [a + 1 for a in args] return

Why is it valid to assign to an empty list but not to an empty tuple?

天大地大妈咪最大 提交于 2019-11-27 04:45:25
This came up in a recent PyCon talk . The statement [] = [] does nothing meaningful, but it does not throw an exception either. I have the feeling this must be due to unpacking rules. You can do tuple unpacking with lists too, e.g., [a, b] = [1, 2] does what you would expect. As logical consequence, this also should work, when the number of elements to unpack is 0, which would explain why assigning to an empty list is valid. This theory is further supported by what happens when you try to assign a non-empty list to an empty list: >>> [] = [1] Traceback (most recent call last): File "<stdin>",