iterable-unpacking

What does the asterisk do in *a, b, c = line.split()?

▼魔方 西西 提交于 2019-12-20 05:36:25
问题 Assume line is: "Chicago Sun 01:52" . What does *a, b, c = line.split() do? In particular, what is the significance of the asterisk? Edit: Upon testing it, it seems like "Chicago" , "Sun" and "01:52" are all stored in a , b and c . The asterisk seems to lead to "Chicago" being stored in a as the first element of a list. So, we have a = ["Chicago"] , b = "Sun" and c = "01:52" . Could anyone point to material on the functionality of the asterisk operator in this situation? 回答1: Splitting that

Default values for iterable unpacking

自古美人都是妖i 提交于 2019-12-19 17:38:56
问题 I've often been frustrated by the lack of flexibility in Python's iterable unpacking. Take the following example: a, b = range(2) Works fine. a contains 0 and b contains 1 , just as expected. Now let's try this: a, b = range(1) Now, we get a ValueError : ValueError: not enough values to unpack (expected 2, got 1) Not ideal, when the desired result was 0 in a , and None in b . There are a number of hacks to get around this. The most elegant I've seen is this: a, *b = function_with_variable

Safely unpacking results of str.split [duplicate]

久未见 提交于 2019-12-19 08:04:31
问题 This question already has answers here : How do I reliably split a string in Python, when it may not contain the pattern, or all n elements? (5 answers) Closed 2 years ago . I've often been frustrated by the lack of flexibility in Python's iterable unpacking. Take the following example: a, b = "This is a string".split(" ", 1) Works fine. a contains "This" and b contains "is a string" , just as expected. Now let's try this: a, b = "Thisisastring".split(" ", 1) Now, we get a ValueError :

Safely unpacking results of str.split [duplicate]

三世轮回 提交于 2019-12-19 08:04:08
问题 This question already has answers here : How do I reliably split a string in Python, when it may not contain the pattern, or all n elements? (5 answers) Closed 2 years ago . I've often been frustrated by the lack of flexibility in Python's iterable unpacking. Take the following example: a, b = "This is a string".split(" ", 1) Works fine. a contains "This" and b contains "is a string" , just as expected. Now let's try this: a, b = "Thisisastring".split(" ", 1) Now, we get a ValueError :

Is there a more elegant way for unpacking keys and values of a dictionary into two lists, without losing consistence?

回眸只為那壹抹淺笑 提交于 2019-12-18 12:19:23
问题 What I came up with is: keys, values = zip(*[(key, value) for (key, value) in my_dict.iteritems()]) But I am not satisfied. What do the pythonistas say? 回答1: What about using my_dict.keys() and my_dict.values()? keys, values = my_dict.keys(), my_dict.values() 回答2: keys, values = zip(*d.items()) 来源: https://stackoverflow.com/questions/6612769/is-there-a-more-elegant-way-for-unpacking-keys-and-values-of-a-dictionary-into-t

Is it possible to return two lists from a function in python

二次信任 提交于 2019-12-18 11:04:52
问题 I am new to python programming and need your help for the following: I want to return two lists from a function in python. How can i do that. And how to read them in the main program. Examples and illustrations would be very helpful. Thanks in advance. 回答1: You can return a tuple of lists, an use sequence unpacking to assign them to two different names when calling the function: def f(): return [1, 2, 3], ["a", "b", "c"] list1, list2 = f() 回答2: You can return as many value as you want by

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

好久不见. 提交于 2019-12-17 12:36:23
问题 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

What does *tuple and **dict means in Python? [duplicate]

匆匆过客 提交于 2019-12-17 10:45:08
问题 This question already has answers here : What do *args and **kwargs mean? [duplicate] (5 answers) Closed 5 years ago . As mentioned in PythonCookbook, * can be added before a tuple, and what does * mean here? Chapter 1.18. Mapping Names to Sequence Elements: from collections import namedtuple Stock = namedtuple('Stock', ['name', 'shares', 'price']) s = Stock(*rec) # here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45) In the same section, **dict presents: from collections

Returning tuple with a single item from a function

瘦欲@ 提交于 2019-12-17 09:56:33
问题 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) ...

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

青春壹個敷衍的年華 提交于 2019-12-17 07:32:07
问题 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