簡體   English   中英

Python 3:如何在列表中找到一個字符並獲取出現次數和位置?

[英]Python 3: How do I find a char in a list and get the number of occurrences and position?

我有一個字母列表,我想看看這些字母是否出現在狀態列表中。 如果確實出現,我想知道出現在哪個字母以及出現在哪個位置。 我想將其存儲在變量中,以便隨后將其與另一個字符串進行比較。 下面是我的示例代碼:

    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)

嘗試這個:

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]

您可以使用列表推導

列表理解為創建列表提供了一種簡潔的方法。 常見的應用是創建新列表,其中每個元素是應用於另一個序列的每個成員或可迭代的某些操作的結果,或者創建滿足特定條件的那些元素的子序列。

對於每個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')]

希望能幫助到你!

您可以使用很多方法:)

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

因此,您將收到類似於此問題說明中所述的內容。 當然,您可以根據需要對特定字典鍵的列表進行排序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM