简体   繁体   中英

How to show duplicates of strings in lists

IDE: PyCharm
Python 3.4

At first. I'm hardly basic student in Python language. I want to learn something new. Lipsum_word_counter. And I have problem. I'm writing program who check how many words (and which one) are repeates in simple "lorem ipsum" copied to file.txt

Can someone answers how to print only strings which repeated once or more?

My current program looks:

with open("3_paragraph_of_lorem.txt", "r") as opened_file:
    list_of_lines = []
    list_of_words = []
    for line in opened_file:
        list_of_lines.append(line)
        split_line = line.split()
        for word in split_line:
            list_of_words.append(word)
            split_word = word.split()
testListDict = {}
for item in list_of_words:
  try:
    testListDict[item] += 1
  except:
    testListDict[item] = 1
print(testListDict)

Please don't be angry on me, this is my first post. I was used search.

You already have the information you need, just iterate over the results dict:

for word, number in testListDict.items():
    if number > 1:
        print(word)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM