
Python字典:高效存储数据
字典({})是一种强大的数据结构,用于存储键值对。它是有序的、可变的,且键值不允许重复。 每个元素都通过唯一的键来访问,而不是索引。如果尝试访问字典中不存在的键,则会引发KeyError异常。
示例:
<code class="python">thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
student = {"name": "Raja", "class": 5}
print(thisdict)
print(student)</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
{'name': 'Raja', 'class': 5}</code>练习:使用嵌套循环进行字符串字符频率分析
以下练习展示如何使用Python字典和循环来统计字符串中字符的频率,并解决一些相关的字符统计问题。 注意,为了提高效率,实际应用中应优先使用Python内置的Counter对象。
1. 统计每个字母的频率:
<code class="python">s = 'guruprasanna'
char_counts = {}
for char in s:
char_counts[char] = char_counts.get(char, 0) + 1
for char, count in char_counts.items():
print(char, count)</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>g 1 u 2 r 2 p 1 a 3 s 1 n 2</code>
2. 仅列出出现一次的字母:
<code class="python">s = 'guruprasanna'
char_counts = {}
for char in s:
char_counts[char] = char_counts.get(char, 0) + 1
for char, count in char_counts.items():
if count == 1:
print(char, count)</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>g 1 p 1 s 1</code>
3. 找出出现频率最高的字母:
<code class="python">s = 'guruprasanna'
char_counts = {}
for char in s:
char_counts[char] = char_counts.get(char, 0) + 1
max_char = max(char_counts, key=char_counts.get)
max_count = char_counts[max_char]
print(max_char, max_count)
</code>输出: (可能有多个字符频率相同,输出其中一个)
<code>a 3</code>
4. 找出第一个不重复的字母:
<code class="python">s = 'guruprasanna'
char_counts = {}
for char in s:
if char not in char_counts:
char_counts[char] = 0
char_counts[char] += 1
for char in s:
if char_counts[char] == 1:
print(char)
break</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>g</code>
5. 找出第一个重复的字母:
<code class="python">s = 'guruprasanna'
char_counts = set()
for char in s:
if char in char_counts:
print(char)
break
char_counts.add(char)</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>u</code>
6. 找出最后一个不重复的字母:
<code class="python">s = 'guruprasanna'
char_counts = {}
for char in s:
char_counts[char] = char_counts.get(char, 0) + 1
for char in reversed(s):
if char_counts[char] == 1:
print(char)
break</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>s</code>
7. 找出最后一个重复的字母:
<code class="python">s = 'guruprasanna'
char_counts = {}
for char in s:
char_counts[char] = char_counts.get(char, 0) + 1
for char in reversed(s):
if char_counts[char] > 1:
print(char)
break</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>n</code>
8. 找出出现频率最高的字母 (改进版,处理多个频率最高的字符):
<code class="python">from collections import Counter s = 'guruprasanna' char_counts = Counter(s) max_count = max(char_counts.values()) most_common = [char for char, count in char_counts.items() if count == max_count] print(most_common, max_count)</code>
输出:
立即学习“Python免费学习笔记(深入)”;
<code>['a'] 3</code>
9. 统计元音 (a, e, i, o, u) 的出现频率:
<code class="python">vowels = "aeiou"
s = 'guruprasanna'
vowel_counts = {}
for char in s:
if char in vowels:
vowel_counts[char] = vowel_counts.get(char, 0) + 1
for vowel, count in vowel_counts.items():
print(vowel, count)</code>输出:
立即学习“Python免费学习笔记(深入)”;
<code>u 2 a 3</code>
这些例子展示了如何使用Python字典和循环来有效地处理字符频率统计问题。 记住,对于大型数据集,collections.Counter 提供了更简洁和高效的解决方案。
以上就是Python day-Dictionary,使用嵌套循环的字符频率的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号