簡體   English   中英

如何找到一個單詞(字符串)是否在 python 的輸入/列表中出現多次

[英]How can I find if a word (string) occurs more than once in an input/list in python

例如,如果示例輸入是:不要問你的國家能為你做什么,問你能為你的國家做什么

我的程序必須返回:“國家”一詞出現在第 5 位和第 17 位。

我只需要幫助查找字符串是否出現多次。

到目前為止,這是我的嘗試,我是 Python 新手,如果我的問題似乎太容易回答,我很抱歉。

# wordsList=[]
words=input("Enter a sentence without punctuation:\n")
# wordsList.append(words)
# print(wordsList)
for i in words:
    if i in words>1:
        print(words)
# words.split("  ")
# print(words[0])

查找出現次數

可能有幾種方法可以做到。 一種簡單的方法是將您的句子拆分為一個列表並找出出現的次數。

sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY" 
words_in_a_list = sentence.split(" ")
words_in_a_list.count("COUNTRY")

您也可以使用正則表達式,而且也很容易做到。

import re

m = re.findall("COUNTRY", sentence)

查找每次出現的位置

可能你想閱讀這篇文章 您也可以使用返回跨度的search 並編寫一個循環來查找所有這些。 知道第一個的位置后,開始進一步從這么多字符中搜索字符串。

def count_num_occurences(word, sentence):
    start = 0
    pattern = re.compile(word)
    start_locations = []
    while True:
        match_object = there.search(sentence, start)

        if match_object is not None:
            start_locations.append(match_object.start())
            start = 1 + match_object.start()
        else:
            break
    return start_locations
str = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY'

# split your sentence and make it a set to get the unique parts
# then make it a list so you ca iterate
parts = list(set(str.split(' ')))

# you count to get the nr of occurences of parts in the str
for part in parts:
    print(f'{part} {str.count(part)}x')

結果

COUNTRY 2x
YOU 4x
ASK 2x
YOUR 2x
CAN 2x
NOT 1x
DO 2x
WHAT 2x
FOR 2x

或與職位

import re

str = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR DO YOUR COUNTRY'

# split your sentence and make it a set to get the unique parts
# then make it a list so you ca iterate
parts = list(set(str.split(' ')))

# you count to get the nr of occurences of parts in the str
for part in parts:
    test = re.findall(part, str)
    print(f'{part} {str.count(part)}x')
    for m in re.finditer(part, str):
        print('     found at', m.start())

結果

DO 3x
     found at 30
     found at 58
     found at 65
ASK 2x
     found at 0
     found at 41
COUNTRY 2x
     found at 18
     found at 73
YOUR 2x
     found at 13
     found at 68
WHAT 2x
     found at 8
     found at 45
YOU 4x
     found at 13
     found at 37
     found at 50
     found at 68
NOT 1x
     found at 4
FOR 2x
     found at 33
     found at 61
CAN 2x
     found at 26
     found at 54

如果您只想要出現多次的單詞:

words=input("Enter a sentence without punctuation:\n").strip().split()
word_counts = {}

for word in words:
    if word in word_counts:
        word_counts[word] += 1
    else:
        word_counts[word] = 1

for word in word_counts.keys():
    if word_counts[word] > 1:
        print(word)

只需將所有計數存儲在字典中,然后遍歷字典以打印出現多次的計數。

也很有效,因為它只通過輸入一次,然后再通過字典一次

如果您想要單詞的實際位置:

words=input("Enter a sentence without punctuation:\n").strip().split()
word_counts = {}

for i in len(words):
    word = words[i]
    if word in word_counts:
        word_counts[word].append(i) // keep a list of indices
    else:
        word_counts[word] = [i]

for word in word_counts.keys():
    if len(word_counts[word]) > 1:
        print("{0} found in positions: {1}".format(word, word_counts[word]))

暫無
暫無

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

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