简体   繁体   English

Python比较两个列表

[英]Python compare two list

I'm trying to find all the rows inside training_full.csv (two column,"macroclass" and "description") from contatti.csv (containing two columns, "name" and "surname"). 我试图从contatti.csv(包含两列,“名称”和“姓”)中找到training_full.csv(两列,“ macroclass”和“ description”)内的所有行。 I want to retrieve all the rows of "description", inside training_full.csv, in which there is "name" and "surname" contained in contatti.csv. 我想在training_full.csv中检索“说明”的所有行,其中contatti.csv中包含“名称”和“姓”。

The script I've created seems to evaluate only the first row of training_full.csv and, for this reason, print only the first row of training_full.csv (in which the script finds a match). 我创建的脚本似乎只评估training_full.csv的第一行,因此,仅打印training_full.csv的第一行(脚本在其中找到匹配项)。 If I modify training_full.csv in way that in the first row there isn't any match, the result is empty. 如果我以第一行没有任何匹配的方式修改training_full.csv,则结果为空。

Here the code: 这里的代码:

import csv

match=[]

with open('xxxxxxxxxxx/training_full1.csv', encoding='utf-8') as csvfile, open('output.csv', 'wb') as output, open('xxxxxxxxxxx/contatti.CSV') as contatti:
spamreader = csv.reader(csvfile)
spamreader_contacts = csv.reader(contatti, delimiter=';')
spamwriter = csv.writer(output)
for row_desc in spamreader:
    #print(righe[0])
    for row_cont in spamreader_contacts:
        #print(row[0])
        if (row_cont[0] + " " + row_cont[1]) in row_desc[0]:
            match.append(row_desc[0])

print(match)

Thanks for any help, 谢谢你的帮助,

Filippo. 菲利波。

Looking at your problem, it seems to be separable in three parts: 1) Read the names, and build a list 2) Compare the training file with the names list 3) Write the matches 查看您的问题,它似乎可以分为三个部分:1)读取名称,并建立一个列表2)将训练文件与名称列表进行比较3)编写匹配项

Doing that, we can end up with a solution similar to: 这样做,我们可以得到类似于以下的解决方案:

import csv

names = []
with open('xxxxxxxxxxx/contatti.csv', 'rb') as f:
    contatti = csv.reader(f, delimiter=';')
    for row in contatti:
        names.append("{} {}".format(row[0], row[1]))

matches=[]
with open('xxxxxxxxxxx/training_full1.csv', 'rb', encoding='utf-8') as f:
    training = csv.reader(f)
    for row in training:
        for name in names:
            if name in row[1]: # description being the second column 
                matches.append(row[1])
                break

with open('output.csv', 'wb') as f:
    output = csv.writer(f)
    for match in matches:
        output.writerow(match)

print(matches)

The main issue with your solution attempt, was, as pointed out in the comments, that once you looked for the first match, you exhausted your csv reader. 如注释中所指出的,解决方案尝试的主要问题是,一旦您寻找第一个匹配项,就会用尽csv阅读器。 In the solution I present, a list of names is first being built. 在我介绍的解决方案中,首先会建立一个名称列表。 This will ensure that we can search the for names multiple times. 这样可以确保我们可以多次搜索的名称。

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

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