iterable-unpacking

Handling empty case with tuple filtering and unpacking

最后都变了- 提交于 2021-02-20 09:33:23
问题 I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them: lista = [1, 2, 3] listb = [7, 8, 9] filtered_a, filtered_b = zip(*[(a, b) for (a, b) in zip(lista, listb) if a < 3]) This gives filtered_a == (1, 2) and filtered_b == (7, 8) However, changing the final condition from a < 3 to a < 0 causes an exception to be raised: Traceback (most recent call last): ... ValueError: need more than 0

Handling empty case with tuple filtering and unpacking

大兔子大兔子 提交于 2021-02-20 09:32:28
问题 I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them: lista = [1, 2, 3] listb = [7, 8, 9] filtered_a, filtered_b = zip(*[(a, b) for (a, b) in zip(lista, listb) if a < 3]) This gives filtered_a == (1, 2) and filtered_b == (7, 8) However, changing the final condition from a < 3 to a < 0 causes an exception to be raised: Traceback (most recent call last): ... ValueError: need more than 0

Handling empty case with tuple filtering and unpacking

浪尽此生 提交于 2021-02-20 09:31:43
问题 I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them: lista = [1, 2, 3] listb = [7, 8, 9] filtered_a, filtered_b = zip(*[(a, b) for (a, b) in zip(lista, listb) if a < 3]) This gives filtered_a == (1, 2) and filtered_b == (7, 8) However, changing the final condition from a < 3 to a < 0 causes an exception to be raised: Traceback (most recent call last): ... ValueError: need more than 0

Unpack 1 variable, rest to a list

回眸只為那壹抹淺笑 提交于 2021-02-16 06:04:33
问题 I was wondering if this was possible: def someFunction(): return list(range(5)) first, rest = someFunction() print(first) # 0 print(rest) # [1,2,3,4] I know it could be accomplished with these 3 lines: result = someFunction() first = result[0] rest = result[1:] 回答1: If you are using Python 3.x, it is possible to do this first, *rest = someFunction() print (first, rest) Read more about it in this PEP In Python 2, the best you can do is result = someFunction() first, rest = result[0], result[1:

Expand tuple into arguments while casting them? [duplicate]

百般思念 提交于 2021-02-08 08:23:54
问题 This question already has answers here : Call int() function on every list element? (7 answers) Closed 4 years ago . I have this: blah = random.randint(int(minmax[0]), int(minmax[1])) I know this is possible: minimum, maximum = int(minmax[0]), int(minmax[1]) blah = random.randint(minimum, maximum) Can I do this second one in a single line using tuple-argument expansion? For example, if minmax was a tuple of integers to begin with, I could do: blah = random.randint(*minmax) But I don't have a

Expand tuple into arguments while casting them? [duplicate]

强颜欢笑 提交于 2021-02-08 08:23:52
问题 This question already has answers here : Call int() function on every list element? (7 answers) Closed 4 years ago . I have this: blah = random.randint(int(minmax[0]), int(minmax[1])) I know this is possible: minimum, maximum = int(minmax[0]), int(minmax[1]) blah = random.randint(minimum, maximum) Can I do this second one in a single line using tuple-argument expansion? For example, if minmax was a tuple of integers to begin with, I could do: blah = random.randint(*minmax) But I don't have a

Expand tuple into arguments while casting them? [duplicate]

落爺英雄遲暮 提交于 2021-02-08 08:23:12
问题 This question already has answers here : Call int() function on every list element? (7 answers) Closed 4 years ago . I have this: blah = random.randint(int(minmax[0]), int(minmax[1])) I know this is possible: minimum, maximum = int(minmax[0]), int(minmax[1]) blah = random.randint(minimum, maximum) Can I do this second one in a single line using tuple-argument expansion? For example, if minmax was a tuple of integers to begin with, I could do: blah = random.randint(*minmax) But I don't have a

Expand tuple into arguments while casting them? [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-02-08 08:22:22
问题 This question already has answers here : Call int() function on every list element? (7 answers) Closed 4 years ago . I have this: blah = random.randint(int(minmax[0]), int(minmax[1])) I know this is possible: minimum, maximum = int(minmax[0]), int(minmax[1]) blah = random.randint(minimum, maximum) Can I do this second one in a single line using tuple-argument expansion? For example, if minmax was a tuple of integers to begin with, I could do: blah = random.randint(*minmax) But I don't have a

Safe unpack empty tuple array

旧街凉风 提交于 2021-02-05 11:21:25
问题 The line import re; print(re.findall("(.*) (.*)", "john smith")) outputs [("john", "smith")] , which can be unpacked like [(first_name, last_name)] = re.findall(...) . However, in the event of a non-match ( findall returning [] ) this unpacking throws ValueError: not enough values to unpack (expected 1, got 0) . What is the correct way to safely unpack this array of tuples, which would work in both match ( [("john", "smith")] ) and non-match ( [] ) scenarios? 回答1: The generic answer is to

Safe unpack empty tuple array

依然范特西╮ 提交于 2021-02-05 11:21:12
问题 The line import re; print(re.findall("(.*) (.*)", "john smith")) outputs [("john", "smith")] , which can be unpacked like [(first_name, last_name)] = re.findall(...) . However, in the event of a non-match ( findall returning [] ) this unpacking throws ValueError: not enough values to unpack (expected 1, got 0) . What is the correct way to safely unpack this array of tuples, which would work in both match ( [("john", "smith")] ) and non-match ( [] ) scenarios? 回答1: The generic answer is to