使用 for 循环在 python 3+ 中的任何序列(列表、字符串、元组)中查找最频繁出现的字符
Find Most Frequent Character(s) in any sequence(list, string, tuple) in python 3+ with for loop
问题:
10.最常见的字符
编写一个程序,让用户输入一个字符串并显示该字符串中出现频率最高的字符。
这是给那些正在学习 "Starting out with Python" 第 9 章第 10 题的 cs 入门的答案。这个问题完全是用我在本书前面章节中学到的知识来回答的。我在这个网站上找不到任何类似的东西。这段代码对于像我这样的初学者来说可能还可以,所以我想分享一下。我知道这段代码看起来很糟糕,但它完成了工作......我在 Youtube 上找到的原始代码是用 Java 编写的,这里是 link:https://www.youtube.com/watch?v=dyWYLXKSPus
抱歉我的英语不好!)
string = "a11aawww1cccertgft1tzzzzzz1ggg111"
mylist_char = []
mylist_count = []
char = None
count = 0
for ch in string:
temp_char = ch
temp_count = 0
for ch1 in string:
if temp_char == ch1:
temp_count += 1
if temp_count > count:
count = temp_count
char = temp_char
mylist_char.append(char)
mylist_count.append(count)
for x in range(len(string)):
for ch in string:
temp_char = ch
temp_count = 0
for ch1 in string:
if temp_char == ch1:
temp_count += 1
if temp_count == count and not(temp_char in mylist_char):
mylist_char.append(temp_char)
mylist_count.append(temp_count)
for x in range(len(mylist_char)):
print("Character", mylist_char[x], "occurred", mylist_count[x], "times")
我对你的解决方案的问题是它看起来像 Java 在 Python 中重写——如果你想使用 Java,请使用 Java。如果您想使用 Python,请充分利用它所提供的功能。我在下面写了一个 "simple" 解决方案,它没有使用任何复杂的 Python 函数(例如,如果我真的在写它,我会使用 defaultdict 和理解)
string = "a11aawww1cccertgft1tzzzzzz1ggg111"
dictionary = {}
for character in list(string):
if character in dictionary:
dictionary[character] += 1
else:
dictionary[character] = 1
results = []
for key, value in dictionary.items():
results.append((value, key))
for value, key in reversed(sorted(results)):
print("Character", key, "occurred", value, "times")
# Most frequent Character
def most_frequent(a_string):
# Create a string of symbols to exclude from counting.
symbols = ' ,.-/?'
characters = []
characters_count = []
# Check each individual character in the string.
for ch in a_string:
# Check that the character is not one of the symbols.
if ch not in symbols:
# If its not and we haven't seen it already,
# append it to the characters list.
if ch not in characters:
characters.append(ch)
# And in the same index in the characters_count list
characters_count.append(1)
else:
# If it is in the characters list, find its index
# and add 1 to the same index at characters_count
position = characters.index(ch)
characters_count[position] = characters_count[position] + 1
# find the largest value in the character_count list, it's index
# and show the character at the same index at the characters list.
print(characters[characters_count.index(max(characters_count))])
def main():
# Get a string from the user.
text = input('Give me some text and I will find you the most frequent character: ')
most_frequent(text)
# Call main
main()
问题: 10.最常见的字符 编写一个程序,让用户输入一个字符串并显示该字符串中出现频率最高的字符。
这是给那些正在学习 "Starting out with Python" 第 9 章第 10 题的 cs 入门的答案。这个问题完全是用我在本书前面章节中学到的知识来回答的。我在这个网站上找不到任何类似的东西。这段代码对于像我这样的初学者来说可能还可以,所以我想分享一下。我知道这段代码看起来很糟糕,但它完成了工作......我在 Youtube 上找到的原始代码是用 Java 编写的,这里是 link:https://www.youtube.com/watch?v=dyWYLXKSPus 抱歉我的英语不好!)
string = "a11aawww1cccertgft1tzzzzzz1ggg111"
mylist_char = []
mylist_count = []
char = None
count = 0
for ch in string:
temp_char = ch
temp_count = 0
for ch1 in string:
if temp_char == ch1:
temp_count += 1
if temp_count > count:
count = temp_count
char = temp_char
mylist_char.append(char)
mylist_count.append(count)
for x in range(len(string)):
for ch in string:
temp_char = ch
temp_count = 0
for ch1 in string:
if temp_char == ch1:
temp_count += 1
if temp_count == count and not(temp_char in mylist_char):
mylist_char.append(temp_char)
mylist_count.append(temp_count)
for x in range(len(mylist_char)):
print("Character", mylist_char[x], "occurred", mylist_count[x], "times")
我对你的解决方案的问题是它看起来像 Java 在 Python 中重写——如果你想使用 Java,请使用 Java。如果您想使用 Python,请充分利用它所提供的功能。我在下面写了一个 "simple" 解决方案,它没有使用任何复杂的 Python 函数(例如,如果我真的在写它,我会使用 defaultdict 和理解)
string = "a11aawww1cccertgft1tzzzzzz1ggg111"
dictionary = {}
for character in list(string):
if character in dictionary:
dictionary[character] += 1
else:
dictionary[character] = 1
results = []
for key, value in dictionary.items():
results.append((value, key))
for value, key in reversed(sorted(results)):
print("Character", key, "occurred", value, "times")
# Most frequent Character
def most_frequent(a_string):
# Create a string of symbols to exclude from counting.
symbols = ' ,.-/?'
characters = []
characters_count = []
# Check each individual character in the string.
for ch in a_string:
# Check that the character is not one of the symbols.
if ch not in symbols:
# If its not and we haven't seen it already,
# append it to the characters list.
if ch not in characters:
characters.append(ch)
# And in the same index in the characters_count list
characters_count.append(1)
else:
# If it is in the characters list, find its index
# and add 1 to the same index at characters_count
position = characters.index(ch)
characters_count[position] = characters_count[position] + 1
# find the largest value in the character_count list, it's index
# and show the character at the same index at the characters list.
print(characters[characters_count.index(max(characters_count))])
def main():
# Get a string from the user.
text = input('Give me some text and I will find you the most frequent character: ')
most_frequent(text)
# Call main
main()