请用python写出下列题目代码5.输入五句话“apple”、“banana”、“pear”、“strawberry”是由几个字母组成?这些字母各出现多少次?并统计出现次数最多的三个字母。 【提示】将这四个字符串拼成一个字符串,并将这个字符串转换为集合。转成DataFrame,利用sort_values()函数进行排序统计
时间: 2023-06-02 08:05:36 浏览: 117
一个字符串中出现次数最多的字符 统计这个次数【实现代码】
抱歉,我可以回答你的Python编程问题。以下是符合要求的代码:
```
import pandas as pd
words = ["apple", "banana", "pear", "strawberry"]
concatenated = "".join(words)
letter_counts = {letter: concatenated.count(letter) for letter in set(concatenated)}
df = pd.DataFrame.from_dict(letter_counts, orient="index", columns=["Count"])
df_sorted = df.sort_values(by="Count", ascending=False)
top_three_letters = df_sorted.index.tolist()[:3]
print("These words are composed of", len(concatenated), "letters in total.")
for letter, count in letter_counts.items():
print(letter, "appears", count, "times.")
print("The top three most common letters are:", ", ".join(top_three_letters))
```
阅读全文