python3:如何在列表中找到一个char并获得出现的次数和位置?

2024-04-16 09:02:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

    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)

Tags: ofthetoin列表forif字母
3条回答

试试这个:

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)

你可以这样做得到一个字母所有索引的列表

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

你可以使用list comprehensions!你知道吗

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

对于状态中的每个s,对于每个字符,检查它是否出现在字母列表中。如果找到了,那么您可以在不丢失其顺序的情况下获取列表中的索引和字符!你知道吗

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

希望有帮助!你知道吗

相关问题 更多 >