我正在尝试查找子列表的索引,返回与另一个子列表具有相同索引的值。当我有重复项时,我一直遇到问题,因为它只返回最低索引。

lines = [ [ 1 , 1], [3, 4] ] 
x = 0 
while x < (len(lines)-1): 
    for ch in lines[x]: 
        print lines[x+1][lines[x].index(ch)] 
    x += 1 

我想得到

的输出
3 
4 

但是我得到了

3 
3 

请您参考如下方法:

我想我明白了。在这两次迭代中,for 循环 ch1 所以 lines[x].index(ch) 将是 1。为什么不直接循环第二个以更直接的方式:

lines = [ [ 1 , 1], [3, 4] ] 
x = 0 
y=0 
while x < (len(lines)-1): 
    while y < len(lines[x]): 
        print lines[x+1][y] 
        y= y+1 
    x = x+1 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!