iterable-unpacking

Unpacking argument lists for ellipsis in R

给你一囗甜甜゛ 提交于 2019-11-28 19:42:51
问题 I am confused by the use of the ellipsis ( ... ) in some functions, i.e. how to pass an object containing the arguments as a single argument. In Python it is called "unpacking argument lists", e.g. >>> range(3, 6) # normal call with separate arguments [3, 4, 5] >>> args = [3, 6] >>> range(*args) # call with arguments unpacked from a list [3, 4, 5] In R for instance you have the function file.path(...) that uses an ellipsis. I would like to have this behaviour: > args <- c('baz', 'foob') >

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

a 夏天 提交于 2019-11-28 17:23:41
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 products %} print product {% endfor %} {% endfor %} I get this error from Django: Caught an exception

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

走远了吗. 提交于 2019-11-28 12:33:34
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 results if len(results) > 1 else results[0] Is there any syntactic sugar (or functions) that would make

Why is Scala's syntax for tuples so unusual?

怎甘沉沦 提交于 2019-11-28 06:40:20
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? Scala knows the arity of the tuples and is thus able to provide accessors like _1 , _2 , etc., and produce a compile

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

回眸只為那壹抹淺笑 提交于 2019-11-28 06:30:28
This question already has an answer here: What do *args and **kwargs mean? [duplicate] 5 answers 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 import namedtuple Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time']) # Create a prototype instance stock

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

天涯浪子 提交于 2019-11-28 05:50:20
问题 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

Tuple Unpacking in Map Operations

≡放荡痞女 提交于 2019-11-28 03:45:23
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? A work around is to use case : arrayOfTuples map {case (e1: Int, e2: String) => e1.toString + e2} I like the tupled function; it's both convenient and

Idiomatic way to unpack variable length list of maximum size n

荒凉一梦 提交于 2019-11-28 03:06:57
问题 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. 回答1: Fix the length of the list, padding with None . def fixLength(lst, length):

Python: Splat/unpack operator * in python cannot be used in an expression?

十年热恋 提交于 2019-11-28 00:39:29
Does anybody know the reasoning as to why the unary ( * ) operator cannot be used in an expression involving iterators/lists/tuples? Why is it only limited to function unpacking? or am I wrong in thinking that? For example: >>> [1,2,3, *[4,5,6]] File "<stdin>", line 1 [1,2,3, *[4,5,6]] ^ SyntaxError: invalid syntax Why doesn't the * operator: [1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6] whereas when the * operator is used with a function call it does expand: f(*[4, 5, 6]) is equivalent to f(4, 5, 6) There is a similarity between the + and the * when using lists but not when extending a list

Python star unpacking for version 2.7

自古美人都是妖i 提交于 2019-11-27 22:40:40
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? Andbdrew in python 2.X, you can do: c = (1, 2, 3) a, b = c[0], c[1:] as long as c has at least one member it will work because if c only has