0

请帮我。如果元素 inb是 in a,那么我想将a(配对)放入一个新列表(C)中。

a=['SMAD3', 'CYP11A1']
['SMAD3', 'ZNF83']
['SMAD3', 'PSMD11']
['SMAD3', 'MTF2']
['SMAD3', 'CFDP1']
['SMAD3', 'ST6GALNAC2']...

b=['NPR2']
['COL2A1']
['GNPTAG']
['ENPP1']
['COL6A1']
['WISP3']...

我这样编码:

C=[ ]
for data in a:
   for line in b:
  if ...
  C.append(a)

我该如何填写if

4

3 回答 3

0

有了这个全局解决方案a,可以多于一对,并且b每个内部列表中的元素可以多于一个。

>>>a=[['ad','re','er'],['ew','fg']]  
>>>b=[['erw','dfger'],['we']] 

无论如何,这是如何做到的:

c=[]  
modified_b=[]  
a=[['aaaa','bbbb'], ['ccccc', 'dddd']]  
b=[['aaaa']] 

for  i in b:  
     modified_b.extend(i)   # this will make b one list instead of list of lists  

for data in a:  
    for element in data:  
        if element in modified_b:  
            c.append(data)  
            break            #this will break the inner loop once you find b in a  
于 2012-11-16T07:54:41.490 回答
0

假设 a 和 b 是列表:

c = []
for data in a:
    if data in b:
        c.append(data)

或者,作为列表理解:

c = [data for data in a if data in b]
于 2012-11-16T05:39:38.033 回答
0
C=[ ]

for data in a:
    if data[0] in b or data[1] in b:
         C.append(data)
于 2012-11-16T05:41:14.043 回答