iterable-unpacking

Python-like list unpacking in C#?

…衆ロ難τιáo~ 提交于 2019-12-10 03:12:34
问题 In python, I can do something like this: List=[3, 4] def Add(x, y): return x + y Add(*List) #7 Is there any way to do this or something similar in C#? Basically I want to be able to pass a List of arguments to an arbitrary function, and have them applied as the function's parameters without manually unpacking the List and calling the function explicitly specifying the parameters. 回答1: Well, the closest would be reflection, but that is on the slow side... but look at MethodInfo.Invoke... 回答2:

Will tuple unpacking be directly supported in parameter lists in Scala?

为君一笑 提交于 2019-12-10 02:14:03
问题 In Haskell you can write: x :: (Int,Int) -> Int x (p,s) = p In Scala you would write: def x(a: (Int, Int)) = a._1 or: def x(a: (Int, Int)) = a match { case (p, s) => p } Why not have something like def x(_: (p: Int, s: Int)) = p or def x(foo: (p @ Int, s @ Int)) = p ? 回答1: The feature you're looking for is called destructuring and, in it's general form, would go well beyond just tuple unpacking. I've often found myself wishing that Scala had it since it's such a natural extension of the

Unpacking a 1-tuple in a list of length 1

☆樱花仙子☆ 提交于 2019-12-08 16:27:23
问题 Suppose I have a tuple in a list like this: >>> t = [("asdf", )] I know that the list always contains one 1-tuple. Currently I do this: >>> dummy, = t >>> value, = dummy >>> value 'asdf' Is there a shorter and more elegant way to do this? 回答1: >>> t = [("asdf", )] >>> t[0][0] 'asdf' 回答2: Try (value,), = t It's better than t[0][0] because it also asserts that your list contains exactly 1 tuple with 1 value in it. 回答3: Try [(val, )] = t In [8]: t = [("asdf", )] In [9]: (val, ) = t In [10]: val

Python: how does the generator and filter work in the codes generating prime list with filter() [closed]

谁说胖子不能爱 提交于 2019-12-08 05:13:25
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 10 months ago . Note: This question is different with using filter and generator to generator endless prime number in python although both of them are related to Python code finding all the prime numbers up to a given limit. The core codes are actually very simple, but it's hard for me to understand how it works

Please explain a python zip and unpacking solution [duplicate]

北城以北 提交于 2019-12-08 02:10:11
问题 This question already has answers here : Why does x,y = zip(*zip(a,b)) work in Python? (7 answers) Closed 6 years ago . A Python 3 learner here: The question had the following accepted answer: rr,tt = zip(*[(i*10, i*12) for i in xrange(4)]) which returns two tuples. I'd be grateful if someone could break down the answer and explain what it is doing with Python 3 in mind ( I know the range() returns an iterator in Python 3). I understand list comprehensions but I'm confused about unpacking (I

Please explain a python zip and unpacking solution [duplicate]

可紊 提交于 2019-12-06 10:35:38
This question already has answers here : Why does x,y = zip(*zip(a,b)) work in Python? (7 answers) Closed 6 years ago . A Python 3 learner here: The question had the following accepted answer: rr,tt = zip(*[(i*10, i*12) for i in xrange(4)]) which returns two tuples. I'd be grateful if someone could break down the answer and explain what it is doing with Python 3 in mind ( I know the range() returns an iterator in Python 3). I understand list comprehensions but I'm confused about unpacking (I thought you could only used a starred expression as part of an assignment target). I'm equally confused

ValueError: too many values to unpack (expected 2)

别等时光非礼了梦想. 提交于 2019-12-05 07:49:16
In the Python tutorial book I'm using, I typed an example given for simultaneous assignment . I get the aforementioned ValueError when I run the program, and can't figure out why. Here's the code: #avg2.py #A simple program to average two exam scores #Illustrates use of multiple input def main(): print("This program computes the average of two exam scores.") score1, score2 = input("Enter two scores separated by a comma: ") average = (int(score1) + int(score2)) / 2.0 print("The average of the scores is:", average) main() Here's the output. >>> import avg2 This program computes the average of

Pythonic way to unpack an iterator inside of a list

依然范特西╮ 提交于 2019-12-05 05:27:01
问题 I'm trying to figure out what is the pythonic way to unpack an iterator inside of a list. For example: my_iterator = zip([1, 2, 3, 4], [1, 2, 3, 4]) I have come with the following ways to unpack my iterator inside of a list: 1) my_list = [*my_iterator] 2) my_list = [e for e in my_iterator] 3) my_list = list(my_iterator) No 1) is my favorite way to do it since is less code, but I'm wondering if this is also the pythonic way. Or maybe there is another way to achieve this besides those 3 which

python: when can I unpack a generator?

妖精的绣舞 提交于 2019-12-05 03:42:28
How does it work under the hood? I don't understand the reason for the errors below: >>> def f(): ... yield 1,2 ... yield 3,4 ... >>> *f() File "<stdin>", line 1 *f() ^ SyntaxError: invalid syntax >>> zip(*f()) [(1, 3), (2, 4)] >>> zip(f()) [((1, 2),), ((3, 4),)] >>> *args = *f() File "<stdin>", line 1 *args = *f() ^ SyntaxError: invalid syntax The *iterable syntax is only supported in an argument list of a function call (and in function definitions). In Python 3.x, you can also use it on the left-hand side of an assignment, like this: [*args] = [1, 2, 3] Edit : Note that there are plans to

Python-like list unpacking in C#?

不羁岁月 提交于 2019-12-05 02:57:40
In python, I can do something like this: List=[3, 4] def Add(x, y): return x + y Add(*List) #7 Is there any way to do this or something similar in C#? Basically I want to be able to pass a List of arguments to an arbitrary function, and have them applied as the function's parameters without manually unpacking the List and calling the function explicitly specifying the parameters. Well, the closest would be reflection, but that is on the slow side... but look at MethodInfo.Invoke ... You can use the params keyword when defining your method and then you can directly put your list (after calling