Python - 基于列表位置匹配对象

时间:2016-01-11 14:56:32

标签: python loops indexing

我正在尝试根据列表中的位置匹配列表中的对象。对于特定CSV,列表中的位置始终相同,但会因客户端而异。

我有一个包含5个客户端的字典,以及CSV中包含数据的列:

d = {'Client1':{
                'file1DataPositions':[1,3,5,6,9],
                'file2DataPositions':[2,4,5,7,8]},
     'Client2':{
                'file1DataPositions':[3,4,5,6,7],
                'file2DataPositions':[1,5,7,8,9]},
     'Client3':{
                'file1DataPositions':[2,6,7,8,9],
                'file2DataPositions':[1,2,7,8,9]},
     'Client4':{
                'file1DataPositions':[2,3,4,6,6],
                'file2DataPositions':[1,3,5,7,9]},
     'Client5':{
                'file1DataPositions':[2,4,6,8,9],
                'file2DataPositions':[5,6,7,8,9]}
     }

我有一个填充的嵌入式列表(已解析的CSV):

l = [['data1','123','ABC','CompanyName','Employee1','15','123 Notareallstreet','Bobs Trucks','Newyork'],
     ['data2','456','DEF','CompanyName','Employee2','15','123 Notareallstreet','Bobs Trucks','Newyork'],
     ['data3','789','GHI','CompanyName','Employee3','15','123 Notareallstreet','Bobs Trucks','Newyork'],
     ['data4','012','JKL','CompanyName','Employee4','15','123 Notareallstreet','Bobs Trucks','Newyork']]

根据嵌入列表中索引的位置,我想查看索引是否匹配:

def iterationTest():
    for item in l:
        for lis in item:
            if lis.index() == d['Client1']['file1DataPositions']:
                print('Match Found')

iterationTest()

当我运行它时,我得到一个:" index()至少需要1个参数(0给定)"错误。

有没有正确的方法来做我想做的事情?

2 个答案:

答案 0 :(得分:1)

使用enumerate()

def iterationTest():
    for item in l:
        for idx, lis in enumerate(item):
            if idx in d['Client1']['file1DataPositions']:
                print('Match Found')

iterationTest()

这将迭代lis中的每个item,并且在循环的每个阶段,它都会提供对lis及其索引idx(您的访问权限)的访问权限用于比较)。

答案 1 :(得分:0)

你到底想要做什么?你在寻找列表索引吗? 我猜你正试图这样做:

d = {'Client1':{
                'file1DataPositions':[1,3,5,6,9],
                'file2DataPositions':[2,4,5,7,8]},
     'Client2':{
                'file1DataPositions':[3,4,5,6,7],
                'file2DataPositions':[1,5,7,8,9]},
     'Client3':{
                'file1DataPositions':[2,6,7,8,9],
                'file2DataPositions':[1,2,7,8,9]},
     'Client4':{
                'file1DataPositions':[2,3,4,6,6],
                'file2DataPositions':[1,3,5,7,9]},
     'Client5':{
                'file1DataPositions':[2,4,6,8,9],
                'file2DataPositions':[5,6,7,8,9]}
     }
l = [['data1','123','ABC','CompanyName','Employee1','15','123 Notareallstreet','Bobs Trucks','Newyork'],
     ['data2','456','DEF','CompanyName','Employee2','15','123 Notareallstreet','Bobs Trucks','Newyork'],
     ['data3','789','GHI','CompanyName','Employee3','15','123 Notareallstreet','Bobs Trucks','Newyork'],
     ['data4','012','JKL','CompanyName','Employee4','15','123 Notareallstreet','Bobs Trucks','Newyork']]

def iterationTest(Preview=True):
    for clientKey , clientValue in d.iteritems():
        for dataKey, dataValue in clientValue.iteritems():
            if Preview:
                print "Data Position: %s"%dataKey
            for eachnum in dataValue:
                if eachnum <= len(l[0])-1:
                    if Preview:
                        print "Match Number: %s"%eachnum
                        print "Number Value: %s"%l[0][eachnum]
                        print "------------------------------"
            if Preview:
                print "=============================="

iterationTest()