开发者

Python: Comparing Lists

I have come across a small problem. Say I have two lists:

list_A = ['0','1','2']
list_B = ['2','0','1']

I then have a list of lists:

matrix = [
['56','23','4'],
['45','5','67'],
['1','52','22']
]

I then need to iterate through list_A and list_B and effectively use them as co-ordinates. For example I take the firs number from list A and B which would be '0' and '2', I then use them as co-ordinates: print matrix[0][2]

I then need to do the same for the 2nd n开发者_如何转开发umber in list A and B and the 3rd number in list A and B and so forth for however long List A and B how would be. How do this in a loop?


matrix = [
['56','23','4'],
['45','5','67'],
['1','52','22']
]

list_A = ['0','1','2']
list_B = ['2','0','1']

for x in zip(list_A,list_B):
    a,b=map(int,x)
    print(matrix[a][b])
# 4
# 45
# 52


[matrix[int(a)][int(b)] for (a,b) in zip(list_A, list_B)]


The 'zip' function could be of some use here. It will generate a list of pairs from list_A and list_B.

for (x,y) in zip(list_A, list_B):
    # do something with the coordinates
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜