Python 3:如何在列表中找到char并获取出现次数和位置?

时间:2017-03-20 15:46:32

标签: python python-3.x

我有一个字母列表,我想看看这些字母是否出现在状态列表中。如果它们出现,我想知道出现哪个字母以及它出现在哪个位置。我想将它存储在变量中,以便我可以将它与另一个字符串进行比较。以下是我的示例代码:

    letters = ['a','b','c','d','e']
    states = ['minnesota','new york','florida']
    found_states = [] 

    for letter in letters:
        for state in states:
            if letter in state:
            found_states.append(state)
            #Here instead of appending to a list
            #I want to find the position of each letter
            #without losing the letter itself
            found_letter = {'e':4,'a':8} #desired result for minnesota
            #i want to use found_letter variable to perform additional
            #if statements
    print(found_states)

4 个答案:

答案 0 :(得分:0)

试试这个:

found_letter = {}
i = 0
for letter in letters:
    i=i+1
    for state in states:
        if letter in state:
            found_states.append(state)
            if letter in found_letter:
                found_letter[letter].append(i)
            else:
                found_letter[letter] = []
                found_letter[letter].append(i)
print(found_states)

答案 1 :(得分:0)

你可以做这样的事情来获得信件的所有索引的列表

state = 'ohio'
letter = 'o'
occurences = [i for i, c in enumerate(state) if c == letter]

答案 2 :(得分:0)

您可以使用list comprehensions

  

列表推导提供了创建列表的简明方法。共同   应用程序将创建新列表,其中每个元素都是结果   一些操作应用于另一个序列的每个成员或   可迭代的,或创建满足a的元素的子序列   某种条件。

对于每个状态,对于每个字符,检查它是否存在于您的字母列表中。如果找到,那么您可以在列表中获取索引和字符而不会丢失其顺序!

>>> [(s,[(index,c) for index,c in enumerate(s) if c in letters]) for s in states]
[('minnesota', [(4, 'e'), (8, 'a')]), ('new york', [(1, 'e')]), ('florida', [(5, 'd'), (6, 'a')])]

如果你打破列表理解,

>>> [s for s in states] 
['minnesota', 'new york', 'florida']

>>> [[c for index,c in enumerate(s)] for s in states] 
[['m', 'i', 'n', 'n', 'e', 's', 'o', 't', 'a'], ['n', 'e', 'w', ' ', 'y', 'o', 'r', 'k'], ['f', 'l', 'o', 'r', 'i', 'd', 'a']]

>>> [[(index,c) for index,c in enumerate(s)] for s in states]
[[(0, 'm'), (1, 'i'), (2, 'n'), (3, 'n'), (4, 'e'), (5, 's'), (6, 'o'), (7, 't'), (8, 'a')], [(0, 'n'), (1, 'e'), (2, 'w'), (3, ' '), (4, 'y'), (5, 'o'), (6, 'r'), (7, 'k')], [(0, 'f'), (1, 'l'), (2, 'o'), (3, 'r'), (4, 'i'), (5, 'd'), (6, 'a')]]

>>> [(s,[(index,c) for index,c in enumerate(s)]) for s in states]
[('minnesota', [(0, 'm'), (1, 'i'), (2, 'n'), (3, 'n'), (4, 'e'), (5, 's'), (6, 'o'), (7, 't'), (8, 'a')]), ('new york', [(0, 'n'), (1, 'e'), (2, 'w'), (3, ' '), (4, 'y'), (5, 'o'), (6, 'r'), (7, 'k')]), ('florida', [(0, 'f'), (1, 'l'), (2, 'o'), (3, 'r'), (4, 'i'), (5, 'd'), (6, 'a')])]

为了更容易访问元素,您可以使用dict理解!

>>>res =  {s:[(index,c) for index,c in enumerate(s) if c in letters] for s in states}
>>> print res
{'minnesota': [(4, 'e'), (8, 'a')], 'new york':[(1, 'e')], 'florida':[(5, 'd'), (6, 'a')]}

所以当你想要访问某个州时说'佛罗里达'

>>> print res['florida']
[(5, 'd'), (6, 'a')]

希望它有所帮助!

答案 3 :(得分:0)

你可以使用很多方法:)

list_of_letters = ['a', 'b', 'c', 'd', 'e', 'n']
states = ['minnesota', 'new york', '

    def search_letters(letters):
        found_states = {}
        for letter in letters:
            for state in states:
                for i, char in enumerate(state):
                    if char in letter:
                        found_states.setdefault(state, []).append(letter + ":" + str(i + 1))
        return found_states

使用后:

print search_letters(list_of_letters)

您将收到如下内容:

{'new york': ['e:2', 'n:1'], 'florida': ['a:7', 'd:6'], 'minnesota': ['a:9', 'e:5', 'n:3', 'n:4']}

因此,您将收到类似于此问题说明中描述的内容。 当然,如果需要,您可以对特定字典键的列表进行排序。