老王python之基本数据结构总习题

老王python之基本数据结构习题

**习题1:
列表a = [11,22,24,29,30,32]
1 把28插入到列表的末端
2 在元素29后面插入元素57
3 把元素11修改成6
3 删除元素32
4 对列表从小到大排序**

>>> a = [11,22,24,29,30,32]
>>> a.append(28)
>>> a
[11, 22, 24, 29, 30, 32, 28]
>>> a.insert(4,57)
>>> a
[11, 22, 24, 29, 57, 30, 32, 28]
>>> a[0] = 6
>>> a
[6, 22, 24, 29, 57, 30, 32, 28]
>>> a.pop(-2)
32
>>> a
[6, 22, 24, 29, 57, 30, 28]
>>> a.sort()
>>> a
[6, 22, 24, 28, 29, 30, 57]

**习题2:
列表b = [1,2,3,4,5]
1 用2种方法输出下面的结果:
[1,2,3,4,5,7,8]
2 用列表的2种方法返回结果:[5,4]
3 判断2是否在列表里**

1>>> b = [1,2,3,4,5]
>>> b.append(7)
>>> b.append(8)
>>> b
[1, 2, 3, 4, 5, 7, 8]

>>> b = [1,2,3,4,5]
>>> c = [7,8]
>>> b.extend(c)
>>> b
[1, 2, 3, 4, 5, 7, 8]

2>>> b
[1, 2, 3, 4, 5]
>>> c = b[-2:]
>>> c.reverse()
>>> c
[5, 4]

>>> b.reverse()
>>> b[:2]
[5, 4]

3>>> 2 in b
True

**习题3:
b = [23,45,22,44,25,66,78],
用列表解析完成下面习题:
1 生成所有奇数组成的列表
2 输出结果: [‘the content 23’,’the content 45’]
3 输出结果: [25, 47, 24, 46, 27, 68, 80]**

1>>> b = [23,45,22,44,25,66,78]
>>> [x for x in b if x % 2 == 1]
[23, 45, 25]

2>>> ["the content %d" % x for x in b[:2]]
['the content 23', 'the content 45']

3>>> [x+2 for x in b]
[25, 47, 24, 46, 27, 68, 80]

**习题4:
用range方法和列表推导的方法生成列表:[11,22,33]**

>>> [x for x in range(11,34) if x % 11 == 0]
[11, 22, 33]

**习题5:
已知元组:a = (1,4,5,6,7)
1 判断元素4是否在元组里
2 把元素5修改成8**

1>>> a = (1,4,5,6,7)
>>> 4 in a
True

2>>> b = list(a)
>>> b[2] = 8
>>> a = tuple(b)
>>> a
(1, 4, 8, 6, 7)

**习题6:
已知集合:setinfo = set(‘acbdfem’)和集合finfo = set(‘sabcdef’)完成下面操作:
1 添加字符串对象’abc’到集合setinfo
2 删除集合setinfo里面的成员m
3 求2个集合的交集和并集**

1>>> setinfo = set('acbdfem')
>>> finfo = set('sabcdef')
>>> setinfo.add('abc')
>>> setinfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'm', 'abc'])

2>>> setinfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'm', 'abc'])
>>> setinfo.remove('m')
>>> setinfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'abc'])

3>>> setinfo & finfo
set(['a', 'c', 'b', 'e', 'd', 'f'])
>>> setinfo | finfo
set(['a', 'c', 'b', 'e', 'd', 'f', 'm', 's'])

**习题7:
用字典的方式完成下面一个小型的学生管理系统。
1 学生有下面几个属性:姓名,年龄,考试分数包括:语文,数学,英语得分。
比如定义2个同学:
姓名:李明,年龄25,考试分数:语文80,数学75,英语85
姓名:张强,年龄23,考试分数:语文75,数学82,英语78
2 给学生添加一门python课程成绩,李明60分,张强:80分
3 把张强的数学成绩由82分改成89分
4 删除李明的年龄数据
5 对张强同学的课程分数按照从低到高排序输出。
6 外部删除学生所在的城市属性,不存在返回字符串 beijing**

1>>> ainfo = {'name':'liming','age':25,'source':{'chinese':80,'math':75,'english':85}}
>>> binfo = {'name':'zhangqiang','age':23,'source':{'chinese':75,'math':82,'english':78}}

2>>> ainfo['source']['python'] = 60
>>> ainfo
{'source': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'age': 25, 'name': 'liming'}
>>> binfo['source']['python'] = 80
>>> binfo
{'source': {'python': 80, 'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': 'zhangqiang'}

3>>> binfo
{'source': {'python': 80, 'math': 82, 'chinese': 75, 'english': 78}, 'age': 23, 'name': 'zhangqiang'}
>>> binfo['source']['math'] = 89
>>> binfo
{'source': {'python': 80, 'math': 89, 'chinese': 75, 'english': 78}, 'age': 23, 'name': 'zhangqiang'}

4>>> ainfo.pop('age')
25
>>> ainfo
{'source': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'name': 'liming'}

5>>> c = binfo['source'].values()
>>> c
[80, 89, 75, 78]
>>> c.sort()
>>> c
[75, 78, 80, 89]

6>>> binfo.pop('city','beijing')
'beijing'
>>> ainfo['city'] = 'shanghai'
>>> ainfo
{'source': {'python': 60, 'math': 75, 'chinese': 80, 'english': 85}, 'name': 'liming', 'city': 'shanghai'}
>>> ainfo.pop('city','beijing')
'shanghai'

猜你喜欢

转载自blog.csdn.net/lcl_xiaowugui/article/details/80765021