Learn Python with Steem #05 笔记


[toc]

划重点

  • 字典 字典是Python内置的一种可变的数据结构,也是一种容器,可以放置任意类型的元素,

    特点是用 {} 定义,元素以键值对的形式组织,键和值是一一对应的映射关系。

    字典的常用操作:创建,访问,更新,删除。

  • 列表

    列表也是Python中一种可变的数据结构,其元素可以是任意类型,用 [] 定义。

    列表的常用操作:创建,访问,添加,删除,切片,排序。

编程练习

1
2
3
4
5
6
# 作业
my_list = [n**3 for n in range(1, 11)]
print(my_list)

my_dictionary = dict(zip(range(1, 11), [n**2 for n in range(1, 11)]))
print(my_dictionary)
1
2
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

补充

列表

1
2
3
4
5
6
7
8
9
# 创建

# 元素可以是任意类型
my_list = [3, 'apple', 3.14, [1, 2, 3], {'name': 'yjcps'}]
print(my_list)

# 列表生成式
my_num = [n+1 for n in range(10)]
print(my_num)
1
2
[3, 'apple', 3.14, [1, 2, 3], {'name': 'yjcps'}]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
1
2
3
4
5
# 使用整数下标访问元素,第一个元素从0开始
print(my_list[0])
print(my_list[2])
# 可以使用负数
print(my_list[-3])
1
2
3
3
3.14
3.14
1
2
# 元素个数,列表长度
print(len(my_list))
1
5
1
2
3
4
5
6
7
# 添加元素
my_list.append(555) # 添加到末尾
print(my_list)
my_list.insert(-2, 'steem') #指定位置
print(my_list)
my_list.extend([16,22,73,45])
print(my_list)
1
2
3
[3, 'apple', 3.14, [1, 2, 3], {'name': 'yjcps'}, 555]
[3, 'apple', 3.14, [1, 2, 3], 'steem', {'name': 'yjcps'}, 555]
[3, 'apple', 3.14, [1, 2, 3], 'steem', {'name': 'yjcps'}, 555, 16, 22, 73, 45]
1
2
3
# 遍历列表
for item in my_list:
    print(item)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
3
apple
3.14
[1, 2, 3]
steem
{'name': 'yjcps'}
555
16
22
73
45
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 删除元素
my_list.remove(3.14)
print(my_list)
del my_list[-1]
print(my_list)
my_list.remove('apple')
print(my_list)

print()

my_list.clear() # 清空
print(my_list)
1
2
3
4
5
[3, 'apple', [1, 2, 3], 'steem', {'name': 'yjcps'}, 555, 16, 22, 73, 45]
[3, 'apple', [1, 2, 3], 'steem', {'name': 'yjcps'}, 555, 16, 22, 73]
[3, [1, 2, 3], 'steem', {'name': 'yjcps'}, 555, 16, 22, 73]

[]
1
2
3
4
5
6
7
8
# 切片 list[起:s止:步伐]

print(my_num)
print(my_num[2:5])
print(my_num[1:8:2])
print(my_num[3:-3])
print(my_num[-2:3:-1])
print(my_num[::-1])
1
2
3
4
5
6
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[3, 4, 5]
[2, 4, 6, 8]
[4, 5, 6, 7]
[9, 8, 7, 6, 5]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 排序
my_num = [11, 4, 56, 98, 34, 6, 77, 23, 12.4, 3]
print(sorted(my_num))
print(sorted(my_num, reverse=True))
print(my_num) # 不改变原来的列表

print()

my_num.sort(reverse=True)
print(my_num) # 改变原来的列表
1
2
3
4
5
[3, 4, 6, 11, 12.4, 23, 34, 56, 77, 98]
[98, 77, 56, 34, 23, 12.4, 11, 6, 4, 3]
[11, 4, 56, 98, 34, 6, 77, 23, 12.4, 3]

[98, 77, 56, 34, 23, 12.4, 11, 6, 4, 3]

字典

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# 创建

my_dictionary = {
    'A': 1,
    'B': 2,
    'C': 3,
    'D': 4,
    'E': 5
}

print(my_dictionary)

list_keys = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
list_values = [1, 2, 3, 4, 5, 6, ]

my_dictionary = dict(zip(list_keys, list_values))
print(my_dictionary)
1
2
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}
{'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6}
1
2
3
4
5
# 访问
print(my_dictionary['B'])

print(my_dictionary.keys())
print(my_dictionary.values())
1
2
3
2
dict_keys(['A', 'B', 'C', 'D', 'E', 'F'])
dict_values([1, 2, 3, 4, 5, 6])
1
2
3
4
5
6
7
for k, v in my_dictionary.items():
    print(k, v)

print()

for k in my_dictionary:
    print(my_dictionary[k])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
A 1
B 2
C 3
D 4
E 5
F 6

1
2
3
4
5
6
1
2
3
4
5
6
7
# 更新
my_dictionary['A'] = 'one'
print(my_dictionary)

my_dictionary.update({'B': 'two', 'C': 'three'})
my_dictionary.update(F='six')
print(my_dictionary)
1
2
{'A': 'one', 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6}
{'A': 'one', 'B': 'two', 'C': 'three', 'D': 4, 'E': 5, 'F': 'six'}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 删除
print(my_dictionary.popitem())
print(my_dictionary.popitem())
print(my_dictionary)

print()

print(my_dictionary.pop('B'))
print(my_dictionary)

print()

my_dictionary.clear() # 清空
print(my_dictionary)
1
2
3
4
5
6
7
8
('F', 'six')
('E', 5)
{'A': 'one', 'B': 'two', 'C': 'three', 'D': 4}

two
{'A': 'one', 'C': 'three', 'D': 4}

{}

集合

集合里的元素是唯一的,无序的,可以行交集、并集、差集等运算

1
2
3
4
5
6
# 创建

my_set_1 = {1,3,4,4,5,6,7,8}
my_set_2 = set(range(10))
print(my_set_1)
print(my_set_2)
1
2
{1, 3, 4, 5, 6, 7, 8}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 增删改 

my_set_1.add(99)
my_set_1.add(45)
print(my_set_1)


print(my_set_1.pop())
print(my_set_1.pop())
print(my_set_1)
1
2
3
4
{1, 3, 4, 5, 6, 7, 8, 99, 45}
1
3
{4, 5, 6, 7, 8, 99, 45}
1
2
my_set_1.discard(6)
print(my_set_1)
1
{4, 5, 7, 8, 99, 45}
1
2
my_set_1.remove(8)
print(my_set_1)
1
{4, 5, 7, 99, 45}
1
2
my_set_1.update([8, 15])
print(my_set_1)
1
{4, 5, 7, 8, 99, 45, 15}
1
2
3
4
# 遍历

for s in  my_set_1:
    print(s)
1
2
3
4
5
6
7
4
5
7
8
99
45
15
1
2
3
4
5
6
# 运算

print(my_set_1 & my_set_2)  # 交集
print(my_set_1 | my_set_2)  # 并集
print(my_set_1 - my_set_2)  # 差集
print(my_set_1 ^ my_set_2)  # 对称差集
1
2
3
4
{8, 4, 5, 7}
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 99, 45}
{99, 45, 15}
{0, 1, 2, 3, 99, 6, 9, 45, 15}

元组

元组类似列表,但元组的元素是不可修改的

1
2
3
# 创建
my_tuple = ('yjcps', 123, 'hacper', 55)
print(my_tuple)
1
('yjcps', 123, 'hacper', 55)
1
2
3
# 访问

print(my_tuple[2])
1
hacper
1
2
# 不可修改
my_tuple[1] = 456
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-24-703d73c28b82> in <module>()
      1 # 不可修改
----> 2 my_tuple[1] = 456


TypeError: 'tuple' object does not support item assignment

字符串

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 创建字符串
text_1 = 'betty bought a bit of butter but the butter was bitter'
text_2 = "cat bat mat cat bat cat"
text_3 = '''
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
'''

# 字符串长度
print(len(text_1), len(text_2), len(text_3))
1
54 23 214
1
2
3
4
5
6
7
8
# 首字母大写
print(text_1.capitalize())

# 全部转换为大写
print(text_2.upper())

# 全部转换为小写
print(text_2.lower())
1
2
3
Betty bought a bit of butter but the butter was bitter
CAT BAT MAT CAT BAT CAT
cat bat mat cat bat cat
1
2
3
# 检查字符串的开头结尾
print(text_2.startswith('cat'))
print(text_2.endswith('bat'))
1
2
True
False
1
2
3
4
5
# 切片,拼接

print(text_3[4:35])
print(text_3[-130:-108])
print(text_3[35:4:-1])
1
2
3
4
 Beautiful is better than ugly.
 is better than comple

.ylgu naht retteb si lufituaeB
1
2
print(text_1 + text_2)
print('#'.join(text_2))
1
2
betty bought a bit of butter but the butter was bittercat bat mat cat bat cat
c#a#t# #b#a#t# #m#a#t# #c#a#t# #b#a#t# #c#a#t
1
2
3
4
5
6
7
8
# 分割

print(text_2.split())
print(','.join(text_2.split())) 

print()

print(text_3.split('.'))
1
2
3
4
['cat', 'bat', 'mat', 'cat', 'bat', 'cat']
cat,bat,mat,cat,bat,cat

['\n    Beautiful is better than ugly', '\n    Explicit is better than implicit', '\n    Simple is better than complex', '\n    Complex is better than complicated', '\n    Flat is better than nested', '\n    Sparse is better than dense', '\n']
1
2
3
# 替换

print(text_1.replace('butter', 'hacper'))
1
betty bought a bit of hacper but the hacper was bitter
1
2
3
4
5
6
# 去除首尾某些字符
text_4 = '   123hello321   '
print(text_4.strip()) # 空格

text_4 = '****123hello321****'
print(text_4.strip('*')) # '*'
1
2
123hello321
123hello321

[DA series - Learn Python with Steem]

我的笔记: