【发布时间】:2014-11-17 22:38:32
【问题描述】:
x = [(u'red', 3.5, u'rouge'), (u'green', 2.6, u'vert'), (u'blue', 1.45, u'blu')]
y = [('bue', 14.5), ('red', 140.0), ('green', 130.0), ('', 0.0)]
我想在 x 中搜索 y[0],如果找到,将 x[0] 更改为 y[0]。
当我尝试循环遍历元组时,我收到了 TypeError: list indices must be integers, not tuple。
对不起,我会再试一次。
x = [('a', 'b','c'),('d','e','f'),('g','h','i')]
y = [('zzz',10), ('xxx',20), ('yyy',30)]
我想最终得到一个y =[('c', 10),('f',20),('i',30)] 列表
我尝试了很多东西,但我是 python 新手,并尝试了以下方法
for tup in x:
results = [(tup[2]) for tup in x]
for tup in x:
if tup[0] == y[tup[0]]:
y[tup[0]] = tup[2]
【问题讨论】:
-
你尝试了什么,显示代码??
-
让我们看看你的代码。
-
一个简单的
d = {i[0]: i[2] for i in x}然后[(d.get(i[0], i[0]), i[1]) for i in y]应该怎么做?
标签: python list tuples list-comprehension