iterable-unpacking

vector unpacking for octave

此生再无相见时 提交于 2019-12-30 18:27:52
问题 Octave(/matlab)'s notation for handling multiple return values [a, b] = f(x) suggests that the values returned by f(x) are in a sort of row vector and that Octave supports vector unpacking (like Python's tuple-unpacking). Yet when I put [a, b] = [1, 2] I get error: invalid number of output arguments for constant expression Does octave support vector-unpacking? If so, what's the proper notation? I can't find anything in the documentation 回答1: I don't have Octave to test, but in MATLAB you can

vector unpacking for octave

ⅰ亾dé卋堺 提交于 2019-12-30 18:27:04
问题 Octave(/matlab)'s notation for handling multiple return values [a, b] = f(x) suggests that the values returned by f(x) are in a sort of row vector and that Octave supports vector unpacking (like Python's tuple-unpacking). Yet when I put [a, b] = [1, 2] I get error: invalid number of output arguments for constant expression Does octave support vector-unpacking? If so, what's the proper notation? I can't find anything in the documentation 回答1: I don't have Octave to test, but in MATLAB you can

Python: unpack to unknown number of variables?

南楼画角 提交于 2019-12-30 08:20:11
问题 How could I unpack a tuple of unknown to, say, a list? I have a number of columns of data and they get split up into a tuple by some function. I want to unpack this tuple to variables but I do not know how many columns I will have. Is there any way to dynamically unpack it to as many variables as I need? Thanks for your help :) 回答1: Unpack the tuple to a list? l = list(t) 回答2: You can use the asterisk to unpack a variable length. For instance: foo, bar, *other = funct() This should put the

Unpacking generalizations

♀尐吖头ヾ 提交于 2019-12-29 04:30:06
问题 >>> LOL = [[1, 2], ['three']] >>> [*LOL[0], *LOL[1]] [1, 2, 'three'] Alright! Goodbye itertools.chain. Never liked you much anyway. >>> [*L for L in LOL] File "<ipython-input-21-e86d2c09c33f>", line 1 [*L for L in LOL] ^ SyntaxError: iterable unpacking cannot be used in comprehension Oh . Why can't we have nice things? Unpacking in a comprehension seems to be obvious/pythonic, but since they've bothered to add that special error message there was a reason for disabling it. So, what's the

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

て烟熏妆下的殇ゞ 提交于 2019-12-28 04:22:08
问题 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

“Unpacking” in C

不想你离开。 提交于 2019-12-24 12:34:53
问题 I am working on rewriting a script from python to C . I'm relatively new to C. I have a variable in PYTHON which contains this values: x = [chr(113),chr(80),chr(191),chr(70)] y = "".join(x) This will return this value of y: y = qP¿F #this is string Now what I do is unpack this variable, store it to variable z to get the results that I wanted. Like this: z = struct.unpack("<f",y) print z[0] #unpack returns a tuple of size 1 The value that I get is: x = 24488.2207 which for my case is correct.

How To Merge an Arbitrary Number of Tuples in Python?

余生颓废 提交于 2019-12-23 08:57:40
问题 I have a list of tuples: l=[(1,2,3),(4,5,6)] The list can be of arbitrary length, as can the tuples. I'd like to convert this into a list or tuple of the elements, in the order they appear: f=[1,2,3,4,5,6] # or (1,2,3,4,5,6) If I know the at development time how many tuples I'll get back, I could just add them: m = l[0] + l[1] # (1,2,3,4,5,6) But since I don't know until runtime how many tuples I'll have, I can't do that. I feel like there's a way to use map to do this, but I can't figure it

Tuples and unpacking assignment support in C#?

隐身守侯 提交于 2019-12-22 02:06:34
问题 In Python I can write def myMethod(): #some work to find the row and col return (row, col) row, col = myMethod() mylist[row][col] # do work on this element But in C# I find myself writing out int[] MyMethod() { // some work to find row and col return new int[] { row, col } } int[] coords = MyMethod(); mylist[coords[0]][coords[1]] //do work on this element The Pythonic way is obivously much cleaner. Is there a way to do this in C#? 回答1: There's a set of Tuple classes in .NET: Tuple<int, int>

Unpacking tuples in a python list comprehension (cannot use the *-operator)

筅森魡賤 提交于 2019-12-20 18:33:05
问题 I am trying to create a list based on another list, with the same values repeated 3 times consecutively. At the moment, I am using: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> for i in range( len( my_list ) ): ... for j in range( 3 ): ... three_times.append( my_list[ i ] ) ... >>> print three_times [1, 1, 1, 2, 2, 2] But I would like to do it using a more Pythonic way, such as: >>> my_list = [ 1, 2 ] >>> three_times = [] >>> three_times = [ (value,) * 3 for value in my_list ] >>> print

unpacking an array of arguments in php

泄露秘密 提交于 2019-12-20 11:14:15
问题 Python provides the "*" operator for unpacking a list of tuples and giving them to a function as arguments, like so: args = [3, 6] range(*args) # call with arguments unpacked from a list This is equivalent to: range(3, 6) Does anyone know if there is a way to achieve this in PHP? Some googling for variations of "PHP Unpack" hasn't immediately turned up anything.. perhaps it's called something different in PHP? 回答1: You can use call_user_func_array() to achieve that: call_user_func_array(