Still a Python novice so please go easy on me...
I've got a dictionary set up:
new_dict
I'd like to filter to return the keys, where any of the values attached to each key match the value in an existing list I have set up:
list(data.Mapped_gene)
Any ideas?
Edit: I still haven't been able to make this work.
The csv tables and keys are all strings if that helps.
Here is the full code to broaden understanding:
import csv
new_dict = {}
with open(raw_input("Enter csv file (including path)"), 'rb') as f:
reader = csv.reader(f)
for row in reader:
if row[0] in new_dict:
new_dict[row[0]].append(row[1:])
else:
new_dict[row[0]] = row[1:]
print new_dict
#modified from: http://bit.ly/1iOS7Gu
import pandas
colnames = ['Date Added to Catalog', 'PUBMEDID', 'First Author', 'Date', 'Journal', 'Link', 'Study', 'DT', 'Initial Sample Size', 'Replication Sample Size', 'Region', 'Chr_id', 'Chr_pos', 'Reported Gene(s)', 'Mapped_gene', 'p-Value', 'Pvalue_mlog', 'p-Value (text)', 'OR or beta', '95% CI (text)', 'Platform [SNPs passing QC]', 'CNV']
data = pandas.read_csv('C:\Users\Chris\Desktop\gwascatalog.csv', names=colnames)
my_list = list(data.Mapped_gene)
my_set = set(my_list)
[k for k, v in new_dict.items() if any(x in my_set for x in v)]
Error Message: "TypeError: unhashable type: 'list'"
Use any
and a list comprehension:
my_list = list(data.Mapped_gene)
keys = [k for k, v in new_dict.items() if any(x in my_list for x in v)]
In case my_list
is huge then convert it to a set
first as it provides O(1)
lookup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With