查找在每个子列表中匹配的字母

时间:2015-02-28 07:29:05

标签: python for-loop collections nested-loops nested-lists

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]

我无法跟踪每个candidate中出现的sublist至少一次。

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

2 个答案:

答案 0 :(得分:1)

最简单的方法 - 套装

>>> 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'])

答案 1 :(得分:0)

以下是一些可以帮助您入门的指导措施,但除此之外,您还需要更清楚地重述问题。

尝试将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