简体   繁体   English

如何检查字符串列表中的int列表?

[英]How to check if list of int in the list of string?

I have 2 defauldict(list); 我有2个默认值(列表); one of them only display the number and another one contain the whole line of text. 其中一个仅显示数字,另一个包含整个文本行。

I have list of int (lookup) 我有int列表(查找)

[112, 896, 455, 1164, 1164, 336, 386, 521, 1011, 1033]

and list of string (description) 和字符串列表(描述)

[['ZNF91', 'Q05481', 'VAR_057393', 'p.Tyr112His', 'Polymorphism', 'rs296091', '-'],
 ['ZNF91', 'Q05481', 'VAR_057394', 'p.Thr896Ala', 'Polymorphism', 'rs296093', '-'],...]

I am trying to come up with if statement to check while i run the program, basically, when i use number that match the line, i want to print the line of that string for example, if i use number 112, which match the first line (p.Tyr112His), i want to have it print the whole line that contains p.Tyr112His; 我试图在运行程序时想出if语句来检查,基本上,当我使用与该行匹配的数字时,例如,如果我使用与第一个匹配的数字112,我想打印该字符串的行。行(p.Tyr112His),我希望它打印出包含p.Tyr112His的整行; which is: 这是:

'ZNF91', 'Q05481', 'VAR_057393', 'p.Tyr112His', 'Polymorphism', 'rs296091', '-'. 'ZNF91','Q05481','VAR_057393','p.Tyr112His','多态性','rs296091','-'。

Looking at the revision history for this question, it appears that you are building the list of integers and the list of strings at the same time while iterating over a csv file. 查看此问题的修订历史记录,您似乎在遍历一个csv文件的同时要构建整数列表和字符串列表。 If that's the case, you might want to consider a slightly different data structure: 在这种情况下,您可能需要考虑略有不同的数据结构:

data = defaultdict(list)
with open(csvfile) as f:
    reader = csv.reader(f)
    for line in reader:
        number = ...  #parse the number from the line here
        data[number].append(line)

Now if you want access to all of the lines which contain the number 112, you can just do: 现在,如果要访问包含数字112的所有行,则可以执行以下操作:

for line in data[112]:
   print (line)

If you need the list of numbers, you can get that easily: list(data.keys()) (or just data.keys() if you're using python 2.x) 如果您需要数字列表,则可以轻松获取: list(data.keys()) (如果使用的是python 2.x,则仅是data.keys() )。

There can be many different ways to do it. 可以有许多不同的方法来做到这一点。 The following is one simple approach 以下是一种简单的方法

list_of_ints=[112, 896, 455, 1164, 1164, 336, 386, 521, 1011, 1033]
list_of_strings=[['ZNF91', 'Q05481', 'VAR_057393', 'p.Tyr112His', 'Polymorphism', 'rs296091', '-'],
 ['ZNF91', 'Q05481', 'VAR_057394', 'p.Thr896Ala', 'Polymorphism', 'rs296093', '-']]
for ints in list_of_ints:
    for st in list_of_strings:
        if str(ints) in ','.join(st):
            print st


['ZNF91', 'Q05481', 'VAR_057393', 'p.Tyr112His', 'Polymorphism', 'rs296091', '-']
['ZNF91', 'Q05481', 'VAR_057394', 'p.Thr896Ala', 'Polymorphism', 'rs296093', '-']

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

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