2020.06.18--预习课--第6节--字符串、元组、列表、字典
一、列表
1、增
1.1、追加增
>>> list_1 = [] >>> list_1.append('a') >>> list_1 ['a'] >>> list_1.append(100) >>> list_1 ['a', 100] >>> list_1.append([]) >>> list_1.append({}) >>> list_1.append(()) >>> list_1 ['a', 100, [], {}, ()] >>>
1.2、定位增,索引可以越界
>>> list_1 ['a', 100, [], {}, ()] >>> list_1.insert(1,'x') >>> list_1 ['a', 'x', 100, [], {}, ()] >>> list_1.insert(1,'y') >>> list_1 ['a', 'y', 'x', 100, [], {}, ()] >>> list_1.insert(1,'z') >>> list_1 ['a', 'z', 'y', 'x', 100, [], {}, ()] >>> list_1.insert(100,100) >>> list_1 ['a', 'y', 'x', 100, [], {}, (), [1, 2, 3], 1, 2, 3, 100] >>>
1.3、extend
>>> list_1 ['a', 'z', 'y', 'x', 100, [], {}, ()] >>> list_2 = [1,2,3] >>> list_1.append(list_2) >>> list_1 ['a', 'z', 'y', 'x', 100, [], {}, (), [1, 2, 3]] >>> list_1.extend(list_2) >>> list_1 ['a', 'z', 'y', 'x', 100, [], {}, (), [1, 2, 3], 1, 2, 3] >>>
2、删
2.1、单个删,索引不可以越界
>>> list_1 ['a', 'z', 'y', 'x', 100, [], {}, (), [1, 2, 3], 1, 2, 3] >>> >>> del list_1[1] >>> list_1 ['a', 'y', 'x', 100, [], {}, (), [1, 2, 3], 1, 2, 3] >>> del list_1[100] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range >>>
2.2、切片删
>>> list_1 ['a', 'y', 'x', 100, [], {}, (), [1, 2, 3], 1, 2, 3, 100] >>> del list_1[10:100] >>> list_1 ['a', 'y', 'x', 100, [], {}, (), [1, 2, 3], 1, 2] >>> del list_1[1:3] >>> list_1 ['a', 100, [], {}, (), [1, 2, 3], 1, 2] >>>
2.3、删除整个变量
>>> list_2 [1, 2, 3] >>> del list_2 >>> list_2 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'list_2' is not defined >>>
3、改
3.1、单个改,索引不可越界
>>> list_1 ['a', 100, [], {}, (), [1, 2, 3], 1, 2] >>> list_1[1]='xyz' >>> list_1 >>> list_1[100] = 100 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range >>>
3.2、切片改
>>> list_1 ['a', 'xyz', [], {}, (), [1, 2, 3], 1, 2] >>> list_1[1:5]='xyz' # 切片长度大于赋值内容长度,没有内容的为空,具体表现等于与删除了 >>> list_1 ['a', 'x', 'y', 'z', [1, 2, 3], 1, 2] >>> list_1[1:3]='xyzUniden' # 切片长度小于赋值内容长度,没有索引的部分,具体表现等同于插入 >>> list_1 ['a', 'x', 'y', 'z', 'U', 'n', 'i', 'd', 'e', 'n', 'z', [1, 2, 3], 1, 2] >>>
4、查
>>> list_1 ['a', 'x', 'y', 'z', 'U', 'n', 'i', 'd', 'e', 'n', 'z', [1, 2, 3], 1, 2] >>> list_1[0] 'a' >>> list_1[1] 'x' >>> list_1[2] 'y' >>> list_1[3] 'z' >>> list_1[300] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>>
二、元组
1、定义元组时,只有一个元素是,必须要加一个逗号
>>> t = ('a') >>> t 'a' >>> type(t) <class 'str'> >>> t_1 = ([1,2,3]) >>> t_1 [1, 2, 3] >>> type(t_1) <class 'list'> >>> t_2 = ({1:'a'}) >>> t_2 {1: 'a'} >>> type(t_2) <class 'dict'> >>>
>>> t = ('a',) >>> t ('a',) >>> type(t) <class 'tuple'> >>> t_1 = ([1,2,3],) >>> t_1 ([1, 2, 3],) >>> type(t_1) <class 'tuple'> >>> t_2 = ({1:'a'},) >>> t_2 ({1: 'a'},) >>> type(t_2) <class 'tuple'> >>> t_3 = (1,) >>> t_3 (1,) >>> type(t_3) <class 'tuple'> >>>
2、元组只能查,不能增、删、改
2.1、单个查找,索引不可越界
>>> t (1, 2, 3, 'a', 'b', 'c', [1, 2], {1: 'a'}, 1.1, 2.2) >>> t[0] 1 >>> t[1] 2 >>> t[2] 3 >>> t[3] 'a' >>> t[5] 'c' >>> t[500] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>>
2.2、可以切片查找
>>> t (1, 2, 3, 'a', 'b', 'c', [1, 2], {1: 'a'}, 1.1, 2.2) >>> t[5:9] ('c', [1, 2], {1: 'a'}, 1.1) >>> t[5:90] ('c', [1, 2], {1: 'a'}, 1.1, 2.2) >>>
2.3、所有元组的子元素的地址,是不能改变的
>>> t (1, 2, 3, 'a', 'b', 'c', [1, 2, 10], {1: 'a'}, 1.1, 2.2) >>> t[6] [1, 2, 10] >>> t[6].append('x') >>> t (1, 2, 3, 'a', 'b', 'c', [1, 2, 10, 'x'], {1: 'a'}, 1.1, 2.2) >>> id(t[6]) 4526153032 >>> t[6].append('x_1') >>> t (1, 2, 3, 'a', 'b', 'c', [1, 2, 10, 'x', 'x_1'], {1: 'a'}, 1.1, 2.2) >>> id(t[6]) 4526153032 >>> t[6]=[1,2,3,4] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>>
三、字典
1、字典的key不可以重复,如果赋值重复了,会把value替换掉
>>> d = {1:2,3:4,5:6,7:8} >>> d {1: 2, 3: 4, 5: 6, 7: 8} >>> d[1]=100 >>> d {1: 100, 3: 4, 5: 6, 7: 8} >>> d[2] = 'a' >>> d {1: 100, 3: 4, 5: 6, 7: 8, 2: 'a'} >>> d[1] = 'abc' >>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a'} >>>
2、key只能是不可变类型
>>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a'} >>> d[[1,2,3]] = [1,2,3] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list' >>> d[(1,2,3)] = [1,2,3] >>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a', (1, 2, 3): [1, 2, 3]} >>> d[{1:2}] = 100 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'dict' >>>
3、遍例字典
3.1、遍例字典的key
>>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a', (1, 2, 3): [1, 2, 3]} >>> d.keys() dict_keys([1, 3, 5, 7, 2, (1, 2, 3)]) >>>
3.2、遍例字典的value
>>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a', (1, 2, 3): [1, 2, 3]} >>> d.values() dict_values(['abc', 4, 6, 8, 'a', [1, 2, 3]]) >>>
3.3、遍例字典的key和value
>>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a', (1, 2, 3): [1, 2, 3]} >>> for k,v in d.items(): ... print('key:',k,';value:',v) ... key: 1 ;value: abc key: 3 ;value: 4 key: 5 ;value: 6 key: 7 ;value: 8 key: 2 ;value: a key: (1, 2, 3) ;value: [1, 2, 3] >>>
4、批量操作
4.1、批量修改,将value全部修改为’change‘
>>> d {1: 'abc', 3: 4, 5: 6, 7: 8, 2: 'a', (1, 2, 3): [1, 2, 3]} >>> d_key = list(d.keys()) >>> d_key [1, 3, 5, 7, 2, (1, 2, 3)] >>> for i in d_key: ... d[i] = 'change' ... >>> d {1: 'change', 3: 'change', 5: 'change', 7: 'change', 2: 'change', (1, 2, 3): 'change'} >>>
4.2、批量删除,成为一个空字典
>>> d {1: 'change', 3: 'change', 5: 'change', 7: 'change', 2: 'change', (1, 2, 3): 'change'} >>> d_key = list(d.keys()) >>> d_key [1, 3, 5, 7, 2, (1, 2, 3)] >>> for i in d_key: ... del d[i] ... >>> d {} >>>
四、习题
练习一:
count函数的算法 算法: 1 定义一个函数,参数传递一个字符串 2 声明一个变量letter_count存储某个字符出现的个数 3 遍历字符串,逐一拿出来,判断是否是你想要统计的那个 4 如果是,则letter_count+1 5 如果不是,则什么都不做 6 把函数中的统计结果变量返回回来 return letter_count
def count_1(msg, s): letter_count = 0 for i in msg: if i == s: letter_count += 1 return letter_count count_content = input("请输入要统计的原内容>>:") str_count = input("请输入需要统计的字符>>:") print(count_1(count_content, str_count))
练习二:
选做:
abcxabcyabc,请统计一下abc有多少个?用函数实现
def count_2(msg, s): str_count = 0 s_long = len(s) for i in range(len(msg)): if msg[i:i+s_long] == s: str_count += 1 if i+s_long > len(msg): break return str_count count_content = input("请输入要统计的原内容>>:") str_count_content = input("请输入需要统计的字符>>:") print(count_2(count_content, str_count_content))
def count_2(msg, s): str_count = 0 s_long = len(s) filter_position = [] for i in range(len(msg)): if i in filter_position: continue if msg[i:i+s_long] == s: str_count += 1 for j in range(1, s_long): filter_position.append(i+j) if i+s_long > len(msg): break return str_count count_content = input("请输入要统计的原内容>>:") str_count_content = input("请输入需要统计的字符>>:") print(count_2(count_content, str_count_content))
ps:
方法一有bug
练习三:
选做:
xxxxxxx,请统计一下可以切割成xx多少个?用函数实现
def count_3(msg, s): str_count = 0 msg_long = len(msg) s_long = len(s) i = 0 while i < msg_long: if msg[i:i+s_long] == s: str_count += 1 i = i+s_long else: i += 1 if i + s_long > msg_long: break return str_count count_content = input("请输入要统计的原内容>>:") str_count_content = input("请输入需要统计的字符>>:") print(count_3(count_content, str_count_content))