例子:
>>> A={"python",123,("python",123)}
>>> print(A)
{('python', 123), 123, 'python'}
>>> B=set("pypy123123")
>>> print(B)
{'y', '2', '1', '3', 'p'}
还有:S<=T或S
还有:S.clear()移除S中的所有元素
S.pop()随机返回S的一个元素,更新S,若S为空产生KeyError异常
集合的典型应用场景:数据去重
>>> creat = "cat","dog","tiger"
>>> creat
('cat', 'dog', 'tiger')
>>> color=(0x000000,"blue",creat)
>>> color
(0, 'blue', ('cat', 'dog', 'tiger'))
操作例子:
>>> creat = "cat","dog","tiger"
>>> creat
('cat', 'dog', 'tiger')
>>> color=(0x000000,"blue",creat)
>>> color
(0, 'blue', ('cat', 'dog', 'tiger'))
>>> creat[::-1]
('tiger', 'dog', 'cat')
>>> color[-1][2]
'tiger'
>>> ls=["cat","dog","tiger",1024]
>>> ls
['cat', 'dog', 'tiger', 1024]
>>> it=ls
>>> it
['cat', 'dog', 'tiger', 1024]
要注意,方括号[]真正创建一个列表,赋值只是传递引用
>>> ls=["cat","dog","tiger",1024]
>>> ls
['cat', 'dog', 'tiger', 1024]
>>> ls[1:2] = [1,2,3,4]
>>> ls
['cat', 1, 2, 3, 4, 'tiger', 1024]
>>> ls = ['cat', 1, 2, 3, 4, 'tiger', 1024]
>>> it = tuple(ls)
>>> it
('cat', 1, 2, 3, 4, 'tiger', 1024)
>>> d = {"中国":"北京","美国":"华盛顿","法国":"巴黎"}
>>> d
{'中国': '北京', '美国': '华盛顿', '法国': '巴黎'}
>>> d["中国"]
'北京'
生成一个空字典
>>> de = {};type(de) #type()函数用来检测数据类型
<class 'dict'>
>>> d = {"中国":"北京","美国":"华盛顿","法国":"巴黎"}
>>> d.get("中国","伊斯兰堡")
'北京'
>>> d.get("巴基斯坦","伊斯兰堡")
'伊斯兰堡'