iterable-unpacking

Where are python's splat operators * and ** valid?

北慕城南 提交于 2019-11-30 07:07:38
问题 The unpacking/splat operators * and ** differ widely in their applicability across python versions (2.7, 3.x < 3.5 and 3.x >= 3.5). For example: | 2.7 | 3.1-3.4 | 3.5 ---------------------------------------------------------------------- function(*args) ✓ ✓ ✓ x, *y, z = [1, 2, 3, 4, 5] x ✓ ✓ {**x, **y} x x ✓ Are there any more discrepancies between the various versions that I've missed? I'm looking through PEP and Readmes but the docs aren't detailed with this. 回答1: Around 1992 (not sure

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

白昼怎懂夜的黑 提交于 2019-11-30 06:47:16
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? Constantinius What about using my_dict.keys() and my_dict.values() ? keys, values = my_dict.keys(), my_dict.values() 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

overloading operator << for std::tuple - possible simplications?

假如想象 提交于 2019-11-30 04:41:19
问题 I used an answer to the SO question "iterate over tuple" to write a method to overload << . This method was tested and appears to work correctly with g++ 4.7 on Debian squeeze. However this method is kind of roundabout, since it seems << cannot be explicitly instantiated (I found a post about it here). So, one is forced to define a string method and then call that. I have a similar method for vector, which is more direct. Does anyone have suggestions about how to eliminate the extra step of

Is 'shift' evil for processing Perl subroutine parameters?

眉间皱痕 提交于 2019-11-30 04:30:50
I'm frequently using shift to unpack function parameters: sub my_sub { my $self = shift; my $params = shift; .... } However, many on my colleagues are preaching that shift is actually evil. Could you explain why I should prefer sub my_sub { my ($self, $params) = @_; .... } to shift ? The use of shift to unpack arguments is not evil. It's a common convention and may be the fastest way to process arguments (depending on how many there are and how they're passed). Here's one example of a somewhat common scenario where that's the case: a simple accessor. use Benchmark qw(cmpthese); sub Foo::x

Python Tuple Unpacking

纵饮孤独 提交于 2019-11-29 12:37:41
问题 If I have nums_and_words = [(1, 'one'), (2, 'two'), (3, 'three')] and would like nums = [1, 2, 3] words= ['one', 'two', 'three'] How would I do that in a Pythonic way? It took me a minute to realize why the following doesn't work nums, words = [(el[0], el[1]) for el in nums_and_words] I'm curious if someone can provide a similar manner of achieving the result I'm looking for. 回答1: Use zip, then unpack: nums_and_words = [(1, 'one'), (2, 'two'), (3, 'three')] nums, words = zip(*nums_and_words)

Python terminology: things to left of “= argv” in Learn Python the Hard Way exercise 13

狂风中的少年 提交于 2019-11-29 11:51:27
Zed Shaw's "Learn Python the Hard Way" frequently asks you to "write out in English" what each and every line of a script does. I am struggling to do that with some stuff associated with the function (command?) argv because I don't know what to name certain parts of the code. Heck, I don't even know what to call argv--a function? A command? Variable? I know it's a module. But back on track: Here is the code from exercise 13 : from sys import argv script, first, second, third = argv print "The script is called:", script print "Your first variable is:", first print "Your second variable is:",

Idiomatic way to unpack variable length list of maximum size n

为君一笑 提交于 2019-11-29 09:55:10
I'm reading a file and unpacking each line like this: for line in filter(fh): a, b, c, d = line.split() However, it's possible that line may have more or fewer columns than the variables I wish to unpack. In the case when there are fewer, I'd like to assign None to the dangling variables, and in the case where there are more, I'd like to ignore them. What's the idiomatic way to do this? I'm using python 2.7. Fix the length of the list, padding with None . def fixLength(lst, length): return (lst + [None] * length)[:length] First of all, think about why you want to do this. However, given that

Invalid syntax python starred expressions

蹲街弑〆低调 提交于 2019-11-29 03:50:08
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? You are using Python 3 specific syntax in Python 2. The * syntax for extended iterable unpacking in assignments is not available in Python 2. See Python 3.0, new syntax and PEP 3132 . Use a function with * splat argument unpacking to

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

◇◆丶佛笑我妖孽 提交于 2019-11-29 03:05:21
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... You are looking for the argument unpacking operator * : myMethod(myVariable, *myTuple) From the Python documentation : The reverse

Is 'shift' evil for processing Perl subroutine parameters?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 01:46:08
问题 I'm frequently using shift to unpack function parameters: sub my_sub { my $self = shift; my $params = shift; .... } However, many on my colleagues are preaching that shift is actually evil. Could you explain why I should prefer sub my_sub { my ($self, $params) = @_; .... } to shift ? 回答1: The use of shift to unpack arguments is not evil. It's a common convention and may be the fastest way to process arguments (depending on how many there are and how they're passed). Here's one example of a