iterable-unpacking

Is it possible to unpack a tuple in Python without creating unwanted variables?

橙三吉。 提交于 2019-12-03 01:04:32
Is there a way to write the following function so that my IDE doesn't complain that column is an unused variable? def get_selected_index(self): (path, column) = self._tree_view.get_cursor() return path[0] In this case I don't care about the second item in the tuple and just want to discard the reference to it when it is unpacked. In Python the _ is often used as an ignored placeholder. (path, _) = self._treeView.get_cursor() You could also avoid unpacking as a tuple is indexable. def get_selected_index(self): return self._treeView.get_cursor()[0][0] If you don't care about the second item, why

Understanding *x ,= lst

☆樱花仙子☆ 提交于 2019-12-02 16:04:25
I'm going through some old code trying to understand what it does, and I came across this odd statement: *x ,= p p is a list in this context. I've been trying to figure out what this statement does. As far as I can tell, it just sets x to the value of p . For example: p = [1,2] *x ,= p print(x) Just gives [1, 2] So is this any different than x = p ? Any idea what this syntax is doing? *x ,= p is basically an obfuscated version of x = list(p) using extended iterable unpacking . The comma after x is required to make the assignment target a tuple (it could also be a list though). *x, = p is

Python swapping lists

大城市里の小女人 提交于 2019-12-01 17:13:39
In python, when I assign a list to another, like: a = [1,2,3] b = a Now b and a point to the same list. Now considering two lists, a = [1,2,3] b = [4,5,6] a,b = b,a Now how is it that they are swapped like any other data type and does not end up both pointing to the same list? Looks like Python internally swaps the items. Check this program a, b = [1, 2], [2, 3] def func(): a, b = b, a import dis dis.dis(func) Output 4 0 LOAD_FAST 0 (b) 3 LOAD_FAST 1 (a) 6 ROT_TWO 7 STORE_FAST 1 (a) 10 STORE_FAST 0 (b) 13 LOAD_CONST 0 (None) 16 RETURN_VALUE So, Python pushes references from b and a in the

vector unpacking for octave

空扰寡人 提交于 2019-12-01 16:56:59
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 I don't have Octave to test, but in MATLAB you can "unpack" cell arrays. x = {1 2}; [x1,x2] = x{:} x1 = 1 x2 = 2 You can convert numerical vector to a cell

Python swapping lists

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 15:16:27
问题 In python, when I assign a list to another, like: a = [1,2,3] b = a Now b and a point to the same list. Now considering two lists, a = [1,2,3] b = [4,5,6] a,b = b,a Now how is it that they are swapped like any other data type and does not end up both pointing to the same list? 回答1: Looks like Python internally swaps the items. Check this program a, b = [1, 2], [2, 3] def func(): a, b = b, a import dis dis.dis(func) Output 4 0 LOAD_FAST 0 (b) 3 LOAD_FAST 1 (a) 6 ROT_TWO 7 STORE_FAST 1 (a) 10

star unpacking for own classes

寵の児 提交于 2019-12-01 05:13:44
I was wondering if it's possible to use star unpacking with own classes rather than just builtins like list and tuple . class Agent(object): def __init__(self, cards): self.cards = cards def __len__(self): return len(self.cards) def __iter__(self): return self.cards And be able to write agent = Agent([1,2,3,4]) myfunc(*agent) But I get: TypeError: visualize() argument after * must be a sequence, not Agent Which methods do I have to implement in order to make unpacking possible? The exception message: argument after * must be a sequence should really say, argument after * must be an iterable .

Python: unpack to unknown number of variables?

时光怂恿深爱的人放手 提交于 2019-12-01 03:35:35
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 :) Unpack the tuple to a list? l = list(t) You can use the asterisk to unpack a variable length. For instance: foo, bar, *other = funct() This should put the first item into foo , the second into bar , and all the rest into other . Update: I forgot to mention that this is

`x = y, z` comma assignment in JavaScript [duplicate]

淺唱寂寞╮ 提交于 2019-11-30 13:45:45
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Javascript syntax: what comma means? I came across the code while reading this article (do a Ctrl + F search for Andre Breton ): //function returning array of `umbrella` fibonacci numbers function Colette(umbrella) { var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon; Array.prototype.embrace = [].push; while(2 + staircase++ < umbrella) { bassoon = galleons + brigantines;

`x = y, z` comma assignment in JavaScript [duplicate]

丶灬走出姿态 提交于 2019-11-30 08:21:39
Possible Duplicate: Javascript syntax: what comma means? I came across the code while reading this article (do a Ctrl + F search for Andre Breton ): //function returning array of `umbrella` fibonacci numbers function Colette(umbrella) { var staircase = 0, galleons = 0, brigantines = 1, armada = [galleons, brigantines], bassoon; Array.prototype.embrace = [].push; while(2 + staircase++ < umbrella) { bassoon = galleons + brigantines; armada.embrace(brigantines = (galleons = brigantines, bassoon)); } return armada; } What does the x = (y = x, z) construct mean? Or more specifically, what does the

How to extract dictionary single key-value pair in variables

大憨熊 提交于 2019-11-30 07:54:27
I have only a single key-value pair in dictionary. I want to assign key to one variable and it's value to another variable. I have tried with below ways but I am getting error for same. >>> d ={"a":1} >>> d.items() [('a', 1)] >>> (k,v) = d.items() Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 1 value to unpack >>> (k, v) = list(d.items()) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: need more than 1 value to unpack I know that we can extract key and value one by one, or by for loop and iteritems(), but