元组
python里面的元组是有序不可变的一种数据结构,只能访问与切片,以下是元组可以使用的
>>> a = (1,2,3,4)
>>> type(a)
<class 'tuple'>
>>> a[0]
1
>>> a[:2]
(1, 2)
>>> len(a)
4
>>> a.append(5)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
a.append(5)
AttributeError: 'tuple' object has no attribute 'append'
>>> a.count(a)
0
>>> a.index(2)
1
>>>
集合
python里面的集合是一种无序,不重复的数据类型,集合主要用到,求子集,交集,并集
>>> a = {1,2,3,4,5}
>>> type(a)
<class 'set'>
>>> b = {4,5,6,7,8}
>>> a.add(6)
>>>> a = {1,2,3,4,5,6}
>>> a.update(7)
Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
a.update(7)
TypeError: 'int' object is not iterable
>>> a.update((7,8))
>>> a
{1, 2, 3, 4, 5, 6, 7, 8}
>>>
>>> a.clear()
>>> del a
>>> a = {1,2,3,4,5}
>>> c = a.copy()
>>> c
{1, 2, 3, 4, 5}
>>> a.difference(b)
{1, 2, 3}
>>> a-b
{1, 2, 3}
>>> a.difference_update(b)
>>> a
{1, 2, 3}
>>>
>>>> a.discard(1)
>>>> a.remove(1)
>>>> a.pop()
1
>>> a
{1, 2, 3, 4, 5}
>>> a.intersection(b)
{4, 5}
>>> a & b
{4, 5}
>>> a.intersection_update(b)
>>> a
{4, 5}
>>> a.isdisjoint(b)
False
>>> a.issubset(b)
False
>>> a.issuperset(b)
False
>>> a
{3, 4, 5}
>>> b
{4, 5, 6, 7, 8}
>>> a.symmetric_difference(b)
{3, 6, 7, 8}
>>> a.symmetric_difference_update(b)
>>> a
{3, 6, 7, 8}
>>> b
{4, 5, 6, 7, 8}
>>> a.union(b)
{3, 4, 5, 6, 7, 8}
>>>
|