iterable-unpacking

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

我是研究僧i 提交于 2019-11-26 16:54:01
问题 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 : >>>

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

你。 提交于 2019-11-26 09:09:25
问题 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. 回答1: 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)

Extended tuple unpacking in Python 2

我与影子孤独终老i 提交于 2019-11-26 07:44:38
问题 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. 回答1: You could define a wrapper function that converts your list to a four tuple. For

How can I convert a dictionary into a list of tuples?

主宰稳场 提交于 2019-11-26 03:36:58
问题 If I have a dictionary like: { \'a\': 1, \'b\': 2, \'c\': 3 } How can I convert it to this? [ (\'a\', 1), (\'b\', 2), (\'c\', 3) ] And how can I convert it to this? [ (1, \'a\'), (2, \'b\'), (3, \'c\') ] 回答1: >>> d = { 'a': 1, 'b': 2, 'c': 3 } >>> d.items() [('a', 1), ('c', 3), ('b', 2)] >>> [(v, k) for k, v in d.iteritems()] [(1, 'a'), (3, 'c'), (2, 'b')] It's not in the order you want, but dicts don't have any specific order anyway. 1 Sort it or organize it as necessary. See: items(),

How does swapping of members in the python tuples (a,b)=(b,a) work internally?

流过昼夜 提交于 2019-11-26 00:15:48
问题 In [55]: a = 5 In [56]: b = 6 In [57]: (a, b) = (b, a) In [58]: a Out[58]: 6 In [59]: b Out[59]: 5 How does this swapping of values of a and b work internally? Its definitely not using a temp variable. 回答1: Python separates the right-hand side expression from the left-hand side assignment. First the right-hand side is evaluated, and the result is stored on the stack, and then the left-hand side names are assigned using opcodes that take values from the stack again. For tuple assignments with

Unpacking, extended unpacking, and nested extended unpacking

北慕城南 提交于 2019-11-25 23:51:11
问题 Consider these expressions... Please be patient... this is a LONG list... (Note: some expressions are repeated -- this is just to present a \"context\") a, b = 1, 2 # simple sequence assignment a, b = [\'green\', \'blue\'] # list asqignment a, b = \'XY\' # string assignment a, b = range(1,5,2) # any iterable will do # nested sequence assignment (a,b), c = \"XY\", \"Z\" # a = \'X\', b = \'Y\', c = \'Z\' (a,b), c = \"XYZ\" # ERROR -- too many values to unpack (a,b), c = \"XY\" # ERROR -- need

“unpacking” a tuple to call a matching function pointer

守給你的承諾、 提交于 2019-11-25 22:19:03
问题 I\'m trying to store in a std::tuple a varying number of values, which will later be used as arguments for a call to a function pointer which matches the stored types. I\'ve created a simplified example showing the problem I\'m struggling to solve: #include <iostream> #include <tuple> void f(int a, double b, void* c) { std::cout << a << \":\" << b << \":\" << c << std::endl; } template <typename ...Args> struct save_it_for_later { std::tuple<Args...> params; void (*func)(Args...); void

What does the star operator mean, in a function call? [duplicate]

こ雲淡風輕ζ 提交于 2019-11-25 21:48:15
问题 This question already has answers here : What does ** (double star/asterisk) and * (star/asterisk) do for parameters? (19 answers) asterisk in function call (3 answers) Closed last year . What does the * operator mean in Python, such as in code like zip(*x) or f(**k) ? How is it handled internally in the interpreter? Does it affect performance at all? Is it fast or slow? When is it useful and when is it not? Should it be used in a function declaration or in a call? 回答1: The single star *