基本数据结构之元组和集合习题
**一、 元组;a = (1,2,3)
1 有2种方法输出实现下面的结果:(5,2,3)
2 判断2是否在元组里**
>>> a = (1,2,3)
>>> b = list(a)
>>> b.insert(0,5)
>>> b.remove(1)
>>> b
[5, 2, 3]
>>> a = tuple(b)
>>> a
(5, 2, 3)
>>> a
(1, 2, 3)
>>> b = list(a)
>>> del b[0]
>>> b.insert(0,5)
>>> b
[5, 2, 3]
>>> a = tuple(b)
>>> a
(5, 2, 3)
>>> a
(5, 2, 3)
>>> 2 in a
True
**二、集合a = set([‘a’,’b’,’c’])做下面的操作:
1 添加字符串’jay’到集合a里。
2 集合b = set([‘b’,’e’,’f’,’g’])
用2种方法求集合a和集合b的并集。**
>>> a = set(['a','b','c'])
>>> a.add('jay')
>>> a
set(['a', 'c', 'b', 'jay'])
>>> a
set(['a', 'c', 'b', 'jay'])
>>> b = set(['b','e','f','g'])
>>> a | b
set(['a', 'c', 'b', 'e', 'g', 'jay', 'f'])
>>> a & b | a - b | b - a
set(['a', 'c', 'b', 'e', 'g', 'jay', 'f'])