我的目标是为更长的元组或更长的列表提供更优雅的子元组或子列表解包。
例如,我有一个带有子数组的数组
Autoresizing
尝试使用包含2个元素的数组和子元组或子列表,我有以下内容:
s = [['yellow', 1,5,6], ['blue', 2,8,3], ['yellow', 3,4,7], ['blue',4,9,1], ['red', 1,8,2,11]]
OR
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
我可以解压's'是否有元组或列表:
s = [['yellow', 1], ['blue', 2], ['yellow', 3], ['blue', 4], ['red', 1]]
生成结果
for k, v in s:
print('k = {0}, v = {1}'.format(k,v))
假设我有以下数组,每个子数组包含四个元素:
k = yellow, v = 1
k = blue, v = 2
k = yellow, v = 3
k = blue, v = 4
k = red, v = 1
我可以使用变量a,b,c,d
解包'bongo'bongo =
[[1, 2, 3, 4], [6, 3, 2, 3], [5, 7, 11, 15], [2, 4, 7, 8]]
尽管能够解压缩混合的chr / number子数组,但我似乎在解压缩混合的'chr'和数字子列表(或子元组(未显示,但得到相同的结果)时出现问题):
for a,b,c,d in bongo:
print('a = {0}, b = {1}, c={2}, d={3}'.format(a,b,c,d))
a = 1, b = 2, c=3, d=4
a = 6, b = 3, c=2, d=3
a = 5, b = 7, c=11, d=15
a = 2, b = 4, c=7, d=8
也就是说,做一个解包我得到了一个错误所需的结果:
s = [['yellow', 1,5,6], ['blue', 2,8,3], ['yellow', 3,4,7], ['blue',
4,9,1], ['red', 1,8,2,11]]
我的问题:是否有一种更优雅的解包方式,这样我想获得第一个元素,比如一键,其余的?
用伪代码来说明 - 它不能直接在python中运行:
for a,b,c,d in s:
print('a = {0}, b = {1}, c = {2}, d = {3} '.format(a,b,c,d))
a = yellow, b = 1, c = 5, d = 6
a = blue, b = 2, c = 8, d = 3
a = yellow, b = 3, c = 4, d = 7
a = blue, b = 4, c = 9, d = 1
Traceback (most recent call last):
File "<pyshell#288>", line 1, in <module>
for a,b,c,d in s:
ValueError: too many values to unpack (expected 4)
以获得以下输出:
for k[0][0], v[0][1:4] in s:
print('k[0][0] = {0}, v[0][1:4] = {1}'.format(k[0][0],v[0][1:4]))
启示: 尝试使用第3.4.1节here中的defaultdict,尤其是使用子元组解包数组。
谢谢你, 悉尼的安东尼
答案 0 :(得分:1)
您可以先转换为所需的格式:
>>> ss = {x[0]: x[1:] for x in s}
>>> ss
{'blue': [4, 9, 1], 'red': [1, 8, 2, 11], 'yellow': [3, 4, 7]}
>>> for s, v in ss.items():
... print "a = {0} b = {1} c = {2} d = {3}".format(s, *v)
...
a = blue b = 4 c = 9 d = 1
a = red b = 1 c = 8 d = 2
a = yellow b = 3 c = 4 d = 7
>>>
答案 1 :(得分:1)
继Azim先生的回答之后,他在第5行使用了* v。这激发了我将其用于进一步实验到数组/元组/列表而不是字典。
此代码产生相同的结果:
s = [('yellow', 1, 5, 6), ('blue', 2, 8, 3), ('green', 4, 9, 1), ('red', 1, 8, 2)]
for x, *y in s:
temparray = [b for b in y]; Note we don't use *y
print('x = {0}, temparray = {1}'.format(x, temparray))
作为
for x, *y in s:
print('x = {0}, y = {1}'.format(x,y)); note we don't use *y
x = yellow, y = [1, 5, 6]
x = blue, y = [2, 8, 3]
x = green, y = [4, 9, 1]
x = red, y = [1, 8, 2]
type(y)
<class 'list'>
结论:*运算符不仅可以应用于字典,还可以应用于数组/元组/列表中。当应用于&#39; for&#39;循环,如
for var1 *var2 in aListorTupleorArray:
# var1 gets the first element of the list or tuple or array
# *var2 gets the remaining elements of the list or tuple or array
print('var1 = {0}, var2 = {1}'.format(var1,var2);#Note we don't use the * in *var2. just use var2
谢谢, 令人兴奋的悉尼安东尼
答案 2 :(得分:0)
这是打印* v和v。
之间的细微差别#printing v in the loop
for s,v in ss.items():
print("s = {0}, v = {1}".format(s,v)); #printing s & v
s = yellow, v = [3,4,7]
s = blue, v = [4,9,1]
s = red, v = [1,8,2]
然后我们
#printing *v in the loop
for s,v in ss.items():
print("s = {0}, *v = {1}".format(s,*v)); #printing s & *v
s = yellow, v = 3 4 7
s = blue, v = 4 9 1
s = red, v = 1 8 2
请注意这里的细微之处:我们是否在&#39;中使用了* v。 loop,print v或* v产生相同的结果:
#printing v in the loop
for s,*v in ss.items():
print("s = {0}, v = {1}".format(s,v)); #printing s & v
#printing v in the loop
for s,*v in ss.items():
print("s = {0}, v = {1}".format(s,*v)); #printing s & v
产生相同的结果:
s = yellow, v = [[3,4,7]]
s = blue, v = [[4,9,1]]
s = red, v = [[1,8,2]]
谢谢你, 悉尼的安东尼