简体   繁体   中英

How to find the intersection (common elements) between two columns of a single csv file in python?

I am having two columns almost (1000 rows) in a csv file without headers (tab separated). The sample content of the column values are as follows. It can be a phrase or a single word.

CSV File Format:

ac           home          

home         big         

new city     city

city         paris

heat         waves

blood        blood pressure

relation     blood

Input Format (Edit):

在此处输入图片说明

I want to compute the common elements between two columns of a csv file? Does there exist any way. I absolutely do not have no idea regarding how to achieve this.

I am completely new to file ( .csv ) and its variants. Any help is deeply appreciated.

Output

home, city, blood

I know how to compute the intersection of two dictionaries, lists etc. But that won't help me to achieve the desired solution.

Use set --> set.intersection

Ex:

import csv

with open(filename) as infile:
    reader = csv.reader(infile, delimiter="\t")
    c1, c2 = set(), set()
    for row in reader:
        if row:
            c1.add(row[0])
            c2.add(row[1])

print(c1.intersection(c2))

Output:

{'home', 'city', 'blood'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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