Python字典与集合学习笔记

简介: Python字典与集合学习笔记

1. 字典

       字典是Python中一种非常有用的数据结构,他以键值对的形式进行存储,可以用于存储和操作复杂的数据。


1.1 字典的创建和删除

       字典的创建使用花括号‘{}’。并使用冒号‘:’分隔键和值。


示例代码:

```python
# 创建一个字典
person = {'name': 'Tom', 'age': 20, 'gender': 'male'}
# 可以通过 print 函数将字典打印出来
print(person)  # 输出:{'name': 'Tom', 'age': 20, 'gender': 'male'}
# 可以通过 del 关键字删除字典中的元素或者整个字典
del person['age']  # 删除 age 键的值
print(person)  # 输出:{'name': 'Tom', 'gender': 'male'}
```

1.2 访问字典

       字典通过键来访问相应的值。可以使用类似于列表和字符串的索引方式来访问字典中的元素。


示例代码:

```python
person = {'name': 'Tom', 'age': 20, 'gender': 'male'}
# 通过键来访问相应的值
print(person['name'])  # 输出:'Tom'
print(person['age'])  # 输出:20
# 如果访问不存在的键,会抛出 KeyError 异常
print(person['height'])  # 抛出 KeyError 异常
```

1.3 遍历字典

       遍历字典使用for循环来实现,可以通过遍历键、值或键值对来获取字典中的元素。


示例代码:

```python
person = {'name': 'Tom', 'age': 20, 'gender': 'male'}
# 遍历键
for key in person:
    print(key)  # 输出:name, age, gender
# 遍历值
for value in person.values():
    print(value)  # 输出:Tom, 20, male
# 遍历键值对
for key, value in person.items():
    print(key, value)  # 输出:name Tom, age 20, gender male
```

1.4 添加、修改和删除字典元素

       可以使用赋值操作符来添加或修改字典中的元素。


示例代码:


```python
person = {'name': 'Tom', 'age': 20, 'gender': 'male'}
# 添加键值对
person['height'] = 180
print(person)  # 输出:{'name': 'Tom', 'age': 20, 'gender': 'male', 'height': 180}
# 修改键值对
person['age'] = 25
print(person)  # 输出:{'name': 'Tom', 'age': 25, 'gender': 'male', 'height': 180}
# 删除键值对
del person['gender']
print(person)  # 输出:{'name': 'Tom', 'age': 25, 'height': 180}
```


1.5 字典推导式

       字典推导式是一种快速创建字典的方法,它类似于列表推导式。


示例代码:

```python
# 创建一个字典,包含 1 到 5 的数值,并将每个数字平方作为值
squares = {x: x**2 for x in range(1, 6)}
print(squares)  # 输出:{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
```

2. 集合

       集合是另一种常用的Python数据结构,它是一组无序的唯一元素组成。可以用于去除重复元素和进行集合运算。


2.1 创建集合

       可以用花括号‘{}’或者‘set()’函数来创建集合。


示例代码:

```python
# 创建一个集合
fruits = {'apple', 'banana', 'orange'}
# 可以通过 print 函数将集合打印出来
print(fruits)  # 输出:{'banana', 'orange', 'apple'}
# 可以通过 set() 函数将列表或元组转换为集合
numbers = set([1, 2, 3, 4, 5])
print(numbers)  # 输出:{1, 2, 3, 4, 5}
```

2.2 向集合中添加或删除元素

       可以使用‘add()’方法来添加元素到集合中,使用‘remove()’方法来从集合中删除元素。


示例代码:


```python
fruits = {'apple', 'banana', 'orange'}
# 添加元素
fruits.add('grape')
print(fruits)  # 输出:{'banana', 'orange', 'grape', 'apple'}
# 删除元素
fruits.remove('orange')
print(fruits)  # 输出:{'banana', 'grape', 'apple'}
```

2.3 集合的交集、并集和差集和运算

       可以使用交集、并集和差集等方法来进行集合运算。


示例代码:

```python
fruits1 = {'apple', 'banana', 'orange'}
fruits2 = {'banana', 'grape', 'mango'}
# 交集
intersection = fruits1 & fruits2
print(intersection)  # 输出:{'banana'}
# 并集
union = fruits1 | fruits2
print(union)  # 输出:{'banana', 'apple', 'mango', 'orange', 'grape'}
# 差集
difference = fruits1 - fruits2
print(difference)  # 输出:{'apple', 'orange'}
```


3. 实践与练习

       以上是字典和集合的基本使用方法,下面给出一些实际案例供练习。yi

### 实践案例1:统计单词频率
```python
text = "Hello, how are you? I am fine, thank you. How about you?"
# 统计单词频率
word_frequency = {}
for word in text.split():
    if word in word_frequency:
        word_frequency[word] += 1
    else:
        word_frequency[word] = 1
# 输出结果
for word, frequency in word_frequency.items():
    print(word, frequency)
```
### 实践案例2:查找两个列表的交集
```python
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
# 查找两个列表的交集
intersection = list(set(list1) & set(list2))
print(intersection)
```


以上是关于字典和集合的学习笔记,希望对屏幕前的你有所帮助。


相关文章
|
23小时前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
23小时前
|
Python
【Python操作基础】——集合
【Python操作基础】——集合
|
1天前
|
索引 Python
Python中的列表、元组和字典各具特色
Python中的列表、元组和字典各具特色:列表是可变的,元组不可变,字典亦可变;列表和元组有序,字典无序(但在Python 3.7+保持插入顺序);元素类型上,列表和元组元素任意,字典需键不可变;列表用方括号[],元组用圆括号(),字典用大括号{}表示。列表不适合作字典键,元组可以。选择数据结构应依据实际需求。
7 2
|
3天前
|
开发者 Python
【Python 基础】递推式构造字典(dictionary comprehension)
【5月更文挑战第8天】【Python 基础】递推式构造字典(dictionary comprehension)
|
13天前
|
Python
Python中字典和集合(二)
Python中字典和集合(二)
|
13天前
|
存储 算法 索引
Python中字典和集合(一)
Python中字典和集合(一)
|
14天前
|
索引 Python
【Python21天学习挑战赛】集合 & 数据类型补充
【Python21天学习挑战赛】集合 & 数据类型补充
|
14天前
|
存储 缓存 Python
【Python21天学习挑战赛】字典 && 小数据池
【Python21天学习挑战赛】字典 && 小数据池
|
15天前
|
存储 JSON 数据处理
|
16天前
|
存储 缓存 人工智能
bidict,一个超酷的 Python 双向字典库!
bidict,一个超酷的 Python 双向字典库!
19 1