在每个子列表中匹配的结果字母
Findings letters that match in every sublist
candidates = ['A', 'B', 'C', 'D']
如果候选人在每个子列表中至少出现一次,则必须将其退回
listOfData = [['B','C','B','A'], #D is no longer a candidate
['B', 'C', 'B', 'D'], #A is no loner a candidate
['A','D','C','B'], # B and C are still candidates
['D', 'C', 'B', 'A']] # B and C are solid matches!
在这种情况下,匹配项是 [B,C]
我无法跟踪每个 sublist
至少出现一次的 candidate
。
matches =[]
def lettersThatMatchInEverySublist():
i=0
for candidate in candidates:
for sublist in listOfData:
for char in sublist:
pass
if char == candidate:
matches.append(candidate)
return matches
这里有一些可以帮助您入门的指导措施,但除此之外,您还需要更清楚地重述您的问题。
尝试将 itertools
用于您的 listOfOptions
:
import itertools
options = itertools.product('ACTG', repeat=3) # This finds all the combinations of A, C, T, and G.
listOfOptions = [''.join(option) for option in options] # This uses list comprehension to prepare your options.
清理 findKmersSet
函数:
def findKmersSet(k, dataset):
dataset = dataset.splitlines()
kmers = []
for line in dataset:
line_list = []
for i in range(len(line)-k+1):
line_list.append(line[i:i+k])
kmers.append(line_list)
return kmers
最简单的方法 - 使用集合
>>> valid_vals = tuple(set(row) for row in listOfData)
>>> candidates = set(['A', 'B', 'C', 'D'])
>>> for validator in valid_vals:
candidates &= validator
>>> candidates
set(['C', 'B'])
candidates = ['A', 'B', 'C', 'D']
如果候选人在每个子列表中至少出现一次,则必须将其退回
listOfData = [['B','C','B','A'], #D is no longer a candidate
['B', 'C', 'B', 'D'], #A is no loner a candidate
['A','D','C','B'], # B and C are still candidates
['D', 'C', 'B', 'A']] # B and C are solid matches!
在这种情况下,匹配项是 [B,C]
我无法跟踪每个 sublist
至少出现一次的 candidate
。
matches =[]
def lettersThatMatchInEverySublist():
i=0
for candidate in candidates:
for sublist in listOfData:
for char in sublist:
pass
if char == candidate:
matches.append(candidate)
return matches
这里有一些可以帮助您入门的指导措施,但除此之外,您还需要更清楚地重述您的问题。
尝试将 itertools
用于您的 listOfOptions
:
import itertools
options = itertools.product('ACTG', repeat=3) # This finds all the combinations of A, C, T, and G.
listOfOptions = [''.join(option) for option in options] # This uses list comprehension to prepare your options.
清理 findKmersSet
函数:
def findKmersSet(k, dataset):
dataset = dataset.splitlines()
kmers = []
for line in dataset:
line_list = []
for i in range(len(line)-k+1):
line_list.append(line[i:i+k])
kmers.append(line_list)
return kmers
最简单的方法 - 使用集合
>>> valid_vals = tuple(set(row) for row in listOfData)
>>> candidates = set(['A', 'B', 'C', 'D'])
>>> for validator in valid_vals:
candidates &= validator
>>> candidates
set(['C', 'B'])