简体   繁体   English

Python-根据列表位置匹配对象

[英]Python - Matching Objects Based on List Position

I am trying to match objects in a list, based on their positions in a list. 我试图根据对象在列表中的位置来匹配列表中的对象。 The position in the list is always the same for a specific CSV, but varies based on client. 对于特定的CSV,列表中的位置始终相同,但根据客户而有所不同。

I have a Dictionary that contains 5 clients and the column in the CSV where the data is contained: 我有一个包含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]}
     }

I have a populated, embedded list (a parsed CSV): 我有一个填充的嵌入式列表(已解析的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']]

Based on the position of the index in the embedded list, I would like to check to see if the index matches: 根据索引在嵌入式列表中的位置,我想检查一下索引是否匹配:

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

iterationTest()

When I run this, I get an: "index() takes at least 1 argument (0 given)" error. 当我运行它时,我得到一个“ index()至少接受1个参数(给定0)”错误。

Is there a correct way to do what I am trying to do? 有正确的方法去做我想做的事吗?

Use enumerate() : 使用enumerate()

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

iterationTest()

This will iterate over each lis in item , and at each stage in the loop it will provide access to both lis and its index, idx (which you use for comparison). 这将遍历item每个lis ,并且在循环的每个阶段,它将提供对lis及其索引idx (用于比较)的访问。

what you exactly trying to do? 您到底想做什么? you are looking for index of list? 您在寻找清单的索引吗? i'm guessing you are trying to do this: 我猜你正在尝试这样做:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM