iterable-unpacking

Iterable Unpacking Evaluation Order

╄→尐↘猪︶ㄣ 提交于 2020-07-03 13:18:20
问题 I recently answered a question where a user was having trouble because they were appending a multi-dimensional array to another array, and it was brought to my attention in my answer that it is possible to use iterable unpacking to populate an x and y value and assign to board[x][y] on the same line. I had expected this to throw an error as x and y had at the time not been defined, as, even in the iterable-unpacking tag it reads: elements of an iterable are simultaneously assigned to multiple

Ignore part of a python tuple

杀马特。学长 韩版系。学妹 提交于 2020-06-10 02:08:21
问题 If I have a tuple such as (1,2,3,4) and I want to assign 1 and 3 to variables a and b I could obviously say myTuple = (1,2,3) a = my_tuple[0] b = myTuple[2] Or something like (a,_,b,_) = myTuple Is there a way I could unpack the values, but ignore one or more of them of them? 回答1: Your solution is fine in my opinion. If you really have a problem with assigning _ then you could define a list of indexes and do: a = (1, 2, 3, 4, 5) idxs = [0, 3, 4] a1, b1, c1 = (a[i] for i in idxs) 回答2: I

Type hints when unpacking a tuple?

梦想的初衷 提交于 2020-06-07 18:47:50
问题 Is it possible to use type hinting when unpacking a tuple? I want to do this, but it results in a SyntaxError : from typing import Tuple t: Tuple[int, int] = (1, 2) a: int, b: int = t # ^ SyntaxError: invalid syntax 回答1: According to PEP-0526, you should annotate the types first, then do the unpacking a: int b: int a, b = t 来源: https://stackoverflow.com/questions/52082939/type-hints-when-unpacking-a-tuple

Type hints when unpacking a tuple?

你。 提交于 2020-06-07 18:44:51
问题 Is it possible to use type hinting when unpacking a tuple? I want to do this, but it results in a SyntaxError : from typing import Tuple t: Tuple[int, int] = (1, 2) a: int, b: int = t # ^ SyntaxError: invalid syntax 回答1: According to PEP-0526, you should annotate the types first, then do the unpacking a: int b: int a, b = t 来源: https://stackoverflow.com/questions/52082939/type-hints-when-unpacking-a-tuple

Unpacking an array in python

天大地大妈咪最大 提交于 2020-05-13 05:13:20
问题 I have a variable data that is of (1000L, 3L) shape and I do the following to get the coordinates: x = data[:,0] y = data[:,1] z = data[:,2] Is there a way to unpack them? I tried but it doesn't work: [x,y,z] = data1[:,0:3] 回答1: You could simply transpose it before unpacking: x, y, z = data.T Unpacking "unpacks" the first dimensions of an array and by transposing the your array the size-3 dimension will be the first dimension. That's why it didn't work with [x, y, z] = data1[:, 0:3] because

Unpacking an array in python

廉价感情. 提交于 2020-05-13 05:11:05
问题 I have a variable data that is of (1000L, 3L) shape and I do the following to get the coordinates: x = data[:,0] y = data[:,1] z = data[:,2] Is there a way to unpack them? I tried but it doesn't work: [x,y,z] = data1[:,0:3] 回答1: You could simply transpose it before unpacking: x, y, z = data.T Unpacking "unpacks" the first dimensions of an array and by transposing the your array the size-3 dimension will be the first dimension. That's why it didn't work with [x, y, z] = data1[:, 0:3] because

Is there way to create tuple from list(without codegeneration)?

冷暖自知 提交于 2020-03-07 07:08:47
问题 Sometimes there are needs to create tuples from small collections(for example scalding framework). def toTuple(list:List[Any]):scala.Product = ... 回答1: If you don't know the arity up front and want to do a terrible terrible hack, you can do this: def toTuple[A <: Object](as:List[A]):Product = { val tupleClass = Class.forName("scala.Tuple" + as.size) tupleClass.getConstructors.apply(0).newInstance(as:_*).asInstanceOf[Product] } toTuple: [A <: java.lang.Object](as: List[A])Product scala>

“ValueError too many values to unpack” when using str.split in a for loop

时光怂恿深爱的人放手 提交于 2020-01-14 13:45:28
问题 I've gotten this error before where the cause was obvious, but I'm having trouble with this snippet below. #!/usr/bin/python ACL = 'group:troubleshooters:r,user:auto:rx,user:nrpe:r' for e in ACL.split(','): print 'e = "%s"' % e print 'type during split = %s' % type(e.split(':')) print 'value during split: %s' % e.split(':') print 'number of elements: %d' % len(e.split(':')) for (one, two, three) in e.split(':'): print 'one = "%s", two = "%s"' % (one, two) I've added those print statements for

How can I avoid nested tuple unpacking when enumerating zipped lists?

荒凉一梦 提交于 2020-01-14 05:46:06
问题 How can I avoid using nested tuple unpacking when enumerating a list of tuples like this? for i, (x, y) in enumerate(zip("1234", "ABCD")): # do stuff 回答1: Use itertools.count to avoid nested tuple unpacking: from itertools import count for i, x, y in zip(count(), "1234", "ABCD"): # do stuff 来源: https://stackoverflow.com/questions/11968729/how-can-i-avoid-nested-tuple-unpacking-when-enumerating-zipped-lists

star unpacking for own classes

喜欢而已 提交于 2020-01-11 05:19:06
问题 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? 回答1: The