简体   繁体   中英

Matching values between two lists in Python?

I'm a bit stumped with what logic to use to be able to match a list to a CSV-file/list containing values. I've had an idea to use a for loop to simple iterate through the CSV and match it:

for j in range(len(data)): 
    if STR_list[j] in data[j]: 
        print(data[j])

But this doesn't actually print out matches as I want it. Here is what the the data and STR_list values look like when printed (before the for loop above):

print(STR_list):
['AGATC', '4', 'AATG', '1', 'TATC', '5']  

print(data)
[OrderedDict([('name', 'Alice'), ('AGATC', '2'), ('AATG', '8'), ('TATC', '3')]), OrderedDict([('name', 'Bob'), ('AGATC', '4'), ('AATG', '1'), ('TATC', '5')]), OrderedDict([('name', 'Charlie'), ('AGATC', '3'), ('AATG', '2'), ('TATC', '5')])]

So in this case, the row with 'Bob' would have been a match as the values line up. Should I be using regex for this or am I right in thinking a for loop could be used?

Edit: Here is how I open the CSV (so it seems like it's a list after all?)

with open('file.csv') as csvfile:
reader = csv.DictReader(csvfile)
data = list(reader)
if STR_list[j] in data[j]: 

This line should be tabbed over to be within the for loop. Assuming that was a copy/paste error:

STR_list[j] isn't looking at the entire STR_list, only the j-th item in it (So when you look at data[0], you're looking at 'AGATC', when you look at data[1] you're looking at '4' etc).

What you want is to look for all of STR_list and see if it is in the 2nd, 3rd, and 4th position of each check for data.

Additionally, STR_list needs to be formatted the same way that data is, so you would want a list of tuples there (or an OrderedDict, I'm not familiar with that data type so I don't know if that's entirely what makes up data).

Really, what you want to look for is the equivalent of:

if [('AGATC', '2'), ('AATG', '8'), ('TATC', '3')] in a subset of OrderedDict([('name', 'Alice'), ('AGATC', '2'), ('AATG', '8'), ('TATC', '3')])

I realize I'm not giving you exactly the code you need, but I hope I'm explaining it so that you can understand and figure it out for yourself.

for j in range(len(data)):
    #we flatten the OrderedDict into a list
    flattened_data = [x for item in data[j].items() for x in item]
    #Now we verify that the list STR_list and the list flattened_data (minus the 2 first element, aka "name" and the actual name) are equal
    if sum([1 for x1, x2 in zip(STR_list, flattened_data[2:]) if x1 == x2])==len(STR_list):
       #Now we print the name of the person which is at index 1 inside the new list
       print(flattened_data[1])
    
 

Basically you need learn how to compare two lists and how to select a specific element from a list

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