python代码实现:随机生成100个小写字母,统计不同字母出现的次数,不换行输出这100个字符,将统计结果按照升序存放在一个列表中
时间: 2023-06-01 21:04:53 浏览: 195
好的,以下是代码实现:
```python
import random
# 随机生成100个小写字母
letters = [chr(random.randint(97, 122)) for _ in range(100)]
# 统计不同字母出现的次数
count = {}
for letter in letters:
count[letter] = count.get(letter, 0) + 1
# 不换行输出这100个字符
print(''.join(letters))
# 将统计结果按照升序存放在一个列表中
result = sorted(count.items(), key=lambda x: x[0])
print(result)
```
以上代码输出的结果类似于:
```
acobthxpakvgpjjwlztdotclbfeqxheopygkdmgbmcknulrkafaqcfqefllsbqgzztcbvxypeubuuokayildrddyqgktiitaox
[('a', 9), ('b', 7), ('c', 7), ('d', 6), ('e', 8), ('f', 6), ('g', 5), ('h', 2), ('i', 5), ('j', 5), ('k', 5), ('l', 6), ('m', 2), ('n', 2), ('o', 3), ('p', 6), ('q', 3), ('r', 7), ('s', 2), ('t', 4), ('u', 3), ('v', 2), ('w', 1), ('x', 4), ('y', 2), ('z', 2)]
```
阅读全文