根据现有列表过滤字典

cps*_*ps1 5 python dictionary list genetics

还是一个Python新手所以请放轻松我...

我有一个字典设置:

new_dict
Run Code Online (Sandbox Code Playgroud)

我想过滤以返回键,其中每个键附加的任何值与我设置的现有列表中的值匹配:

list(data.Mapped_gene)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:我仍然无法完成这项工作.

如果有帮助,csv表和键都是字符串.

以下是扩大理解的完整代码:

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)]
Run Code Online (Sandbox Code Playgroud)

错误消息:"TypeError:unhashable type:'list'"

Ash*_*ary 3

使用any和列表理解:

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)]
Run Code Online (Sandbox Code Playgroud)

如果情况my_list很大,则将其转换为set第一个,因为它提供O(1)查找。