如何正确引用列表列表中的项目

时间:2017-03-27 03:02:41

标签: python

我正在尝试引用列表列表中的项目,但它只是在项目中吐出一个数字,而不是整个项目本身。让我说明我的意思。

# Let's say I have this list here
L = [['201','304','514'],['312','143','224']]

如果我要这样做

L2 = L[0]
print(L2[0])

结果应该是     '201' 正确的吗?

嗯,在这种情况下,我的程序只向我拍摄了2个,这是该项目的第一个数字。这是我的一些代码。

zp = [list(t) for t in zip(*[iter(masterL)] * 3)]
# masterL is just a regular list and zp creates a list of lists grouped by 3
# similar to the list L stated above
count = 0
while count < len(zp):
     current_img = zp[count]
     for rgb in current_img:
         red = int(rgb[0])
         green = int(rgb[1])
         blue = int(rgb[2])

所以在这种情况下,我们可以说上面提到的zp = L.我的代码正在吐出那个

red = 2
green = 0
blue = 1

当我希望我的代码执行此操作时

red = 201
green = 304
blue = 514

1 个答案:

答案 0 :(得分:1)

@wwii帮助我理解我走得太远了。

为了得到结果,我希望这是我的代码看起来像

# In the example above current_img would have looked like this
current_img = ['201', '304', '514']

# In order to get the output I wanted my code should have looked something like this
red = current_img[0]
green = current_img[1]
blue = current_img[2]