点击上方程序员共成长”,选择“置顶或者星标”

你关注的就是我关心的!

 

往期链接:


[Python从入门到上瘾] 1、Python简介和变量

[Python从入门到上瘾] 2、Python中的基本数据类型(上)


在上一节的基本数据类型中讲了数字(Number)、字符串、布尔。

今天这一节讲一下列表和集合(本来都想讲完,但是东西有点多,字典和元组下一节再聊)


1、列表(List)


列表是由一系列元素按照一定顺序组成的,元素可以是数字、字符串、对象等等,在列表中元素是可以重复的


在Python中用方括号[ ] 来表示列表,每个元素用逗号(,)隔开。


names = ['tom', 'lily', 'xiaoming']'tom''lily''xiaoming']


如何获取列表中的元素?

访问列表可以通过 [num] 去获取元素, num为元素的下标,下标就是每个元素的顺序,从0开始。


names = ['tom', 'lily', 'xiaoming']# 打印tomprint(names[0])# 打印xiaomingprint(names[2])# 报错: list index out of range 下标不能超过列表的总长度print(name[3])'lily''xiaoming']
# 打印tom
print(names[0])
# 打印xiaoming
print(names[2])

# 报错: list index out of range 下标不能超过列表的总长度
print(name[3])


如何获取添加元素?


添加元素可以通过append或者insert来完成。

通过append的添加,会在列表的末尾添加上元素


names = ['xiaoming', 'tom']# ['xiaoming', 'tom']print(names)names.append('lily')# ['xiaoming', 'tom', 'lily']print(names)'tom']
# ['xiaoming', 'tom']
print(names)
names.append('lily')
# ['xiaoming', 'tom', 'lily']
print(names)


通过insert添加时,会将元素添加到指定的索引处。

insert(索引num, 元素)


>>> names = ['xiaoming', 'tom']>>> names.insert(1, 'wang')>>> names['xiaoming', 'wang', 'tom']'tom']
>>> names.insert(1'wang')
>>> names
['xiaoming''wang''tom']


如何删除列表?


通过del的方式删除整个列表,通过del(num)则删除索引是num的元素。


>>> words = ['a', 'b', 'c', 'd']>>> del words[1]>>> print(words)['a', 'c', 'd']>>> del words# 因为words已经被删除了,所以执行print的时候会报错>>> print(words)Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name 'words' is not defined'b''c''d']
>>> del words[1]
>>> print(words)
['a''c''d']
>>> del words
# 因为words已经被删除了,所以执行print的时候会报错
>>> print(words)
Traceback (most recent call last):
  File "<stdin>", line 1in <module>
NameError: name 'words' is not defined


另外可以通过pop(索引[默认是-1])来删除(弹出)元素,并返回。del不会返回删除的元素


>>> list = ['a', 'b', 'c', 'd']>>> list.pop()'d'>>> list.pop(0)'a''b''c''d']
>>> list.pop()
'd'
>>> list.pop(0)
'a'


也可以通过remove删除指定的元素(有重复元素时,只删除从左到右第一个匹配到的)


>>> list = ['a', 'b', 'a', 'd']>>> list.remove('a')>>> list['b', 'a', 'd']'b''a''d']
>>> list.remove('a')
>>> list
['b''a''d']


获取列表的长度(元素个数)


可以通过len(列表名)的方式查看列表内的元素个数(列表长度)


>>> words = ['a', 'b', 'c', 'd']>>> print(len(words))4'b''c''d']
>>> print(len(words))
4


判断元素是否在列表中

通过in关键字判断元素是否在列表内,返回布尔类型


>>> a = ['a', 'b', 'c']>>> 'a' in aTrue'a''b''c']
>>'a' in a
True


列表的截取(分片)  ** 重要

与字符串类似,可以通过list[初始索引:终止索引:步长]的方式去完成。步长默认是1。(步长指跳过元素的个数,1位按顺序)


>>> words = ['a', 'b', 'c', 'd', 'e', 'f', 'g']# 索引从0开始>>> words[1: 3]['b', 'c']# 从0索引开始6结束,步长为2>>> words[0:6:2]['a', 'c', 'e']'b''c''d''e''f''g']
# 索引从0开始
>>> words[13]
['b''c']
# 从0索引开始6结束,步长为2
>>> words[0:6:2]
['a''c''e']


另外

list[num:] 表示从num位开始一直到最后,包括num位

list[:num] 从起始位置开始,到num位结束,不包括第num位

list[num: -1] 表示从num为开始,一直到最后一个,不包括最后一个

list[:] 等同于list返回全部

list[::-1] 表示num1到num2倒叙排列,如果[::-1]表示全部倒序


>>> words = ['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> words[5:]['f', 'g']>>> words[:2]['a', 'b']>>> words[1:-1]['b', 'c', 'd', 'e', 'f']['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> words[:: -1]['g', 'f', 'e', 'd', 'c', 'b', 'a']'a''b''c''d''e''f''g']
>>> words[5:]
['f''g']
>>> words[:2]
['a''b']
>>> words[1:-1]
['b''c''d''e''f']
['a''b''c''d''e''f''g']
>>> words[:: -1]
['g''f''e''d''c''b''a']


列表相加


>>> list1 = ['a', 'b', 'c']>>> list2 = ['q', 'w', 'e']# 会产生一个新对象>>> list1 + list2['a', 'b', 'c', 'q', 'w', 'e']# 也是拼接元素,但是不会产生新的对象,而是改变了list1>>> list1.extend(list2)>>> list1['a', 'b', 'c', 'q', 'w', 'e']'b''c']
>>> list2 = ['q''w''e']
# 会产生一个新对象
>>> list1 + list2
['a''b''c''q''w''e']
# 也是拼接元素,但是不会产生新的对象,而是改变了list1
>>> list1.extend(list2)
>>> list1
['a''b''c''q''w''e']


统计元素在列表中出现的次数,通过count


>>> list = ['a', 'b', 'c', 'a', 'a', 'c']>>> list.count('a')3>>> list.count('b')1>>> list.count('c')2'b''c''a''a''c']
>>> list.count('a')
3
>>> list.count('b')
1
>>> list.count('c')
2


列表的排序

通过sort对列表进行排序


>>> list = ['z', 'a', 'g', 'k', 'n']# 默认升序>>> list.sort()>>> list['a', 'g', 'k', 'n', 'z']# reverse=True,为降序>>> list.sort(reverse=True)>>> list['z', 'n', 'k', 'g', 'a']>>>'a''g''k''n']
# 默认升序
>>> list.sort()
>>> list
['a''g''k''n''z']
# reverse=True,为降序
>>> list.sort(reverse=True)
>>> list
['z''n''k''g''a']
>>>



2、 集合(Set)


集合和上面提到的列表类似,但是有个最重要的不同就是,集合中的元素不可以重复!!


创建集合


1、创建空集合

创建空集合的时候,和其他数据类型不同。必须用set{}

比如创建一个名称为name的集合


name = set()


2、创建非空集合,推荐以下两种方式



#  创建空集合时不能用{}, 因为这是创建字典的方式>>> name = {'tom', 'lily', 'lucy'}>>> name{'tom', 'lucy', 'lily'}>>> name = set(['tom', 'lily', 'lucy'])>>> name{'tom', 'lucy', 'lily'}
>>> name = {'tom''lily''lucy'}
>>> name
{'tom''lucy''lily'}
>>> name = set(['tom''lily''lucy'])
>>> name
{'tom''lucy''lily'}



集合添加元素


>>> name = {'tom', 'lily', 'lucy'}>>>>>> name.add('wang')>>> name{'tom', 'lucy', 'wang', 'lily'}>>> name.add('wang')# 因为集合中已经存在了‘wang’,再次添加并不会生效,因为集合不能有重复的元素>>> name{'tom', 'lucy', 'wang', 'lily'}'lily''lucy'}
>>>
>>> name.add('wang')
>>> name
{'tom''lucy''wang''lily'}
>>> name.add('wang')
# 因为集合中已经存在了‘wang’,再次添加并不会生效,因为集合不能有重复的元素
>>> name
{'tom''lucy''wang''lily'}


也可以通过update进行元素的添加


>>> a = {'a', 'b', 'c', 'd'}>>> a.update({'z', 'x'})>>> a{'x', 'c', 'z', 'b', 'd', 'a'}>>> a.update(['j','k'])>>> a{'j', 'x', 'k', 'c', 'z', 'b', 'd', 'a'}'b''c''d'}
>>> a.update({'z''x'})
>>> a
{'x''c''z''b''d''a'}
>>> a.update(['j','k'])
>>> a
{'j''x''k''c''z''b''d''a'}


移除集合内的元素

通过remove来进行元素的移除


>>> a = {'a', 'b', 'c', 'd'}>>> a.remove('b')>>> a{'d', 'a', 'c'}# remove列表中不存在的元素 则会报错>>> a.remove('z')Traceback (most recent call last):  File "<stdin>", line 1, in <module>KeyError: 'z''a''b''c''d'}
>>> a.remove('b')
>>> a
{'d''a''c'}
# remove列表中不存在的元素 则会报错
>>> a.remove('z')
Traceback (most recent call last):
  File "<stdin>", line 1in <module>
KeyError: 'z'


也可以通过discard(元素),和remove的区别在于当删除不存在的元素则不会报错


>>> a = {'a', 'b', 'c', 'd'}>>> a.discard('a')>>> a{'b', 'd', 'c'}>>> a.discard('z')>>> a{'b', 'd', 'c'}'b''c''d'}
>>> a.discard('a')
>>> a
{'b''d''c'}
>>> a.discard('z')
>>> a
{'b''d''c'}


当然与列表类似也可以通过pop从集合中随机弹出一个元素,并返回。列表中弹出最后一个,而集合中随机弹出


>>> a = {'a', 'b', 'c', 'd'}>>> a.pop()'b'>>> a.pop()'d'>>> a{'a', 'c'}>>>'b''c''d'}
>>> a.pop()
'b'
>>> a.pop()
'd'
>>> a
{'a''c'}
>>>


集合之间的运算


>>> set1 = {'a', 'b', 'c', 'd'}>>> set2 = {'a', 'd', 'z', 'x'}# 在集合set1 而不在set2中的元素>>> set1 - set2{'b', 'c'}# set1和set2的所有元素,合集>>> set1 | set2{'d', 'a', 'x', 'c', 'z', 'b'}# 不同时在set1和set2的元素>>> set1 ^ set2{'x', 'c', 'z', 'b'}# 同时在set1和set2的元素,交集>>> set1 & set2{'d', 'a'}'b''c''d'}
>>> set2 = {'a''d''z''x'}
# 在集合set1 而不在set2中的元素
>>> set1 - set2
{'b''c'}
# set1和set2的所有元素,合集
>>> set1 | set2
{'d''a''x''c''z''b'}
# 不同时在set1和set2的元素
>>> set1 ^ set2
{'x''c''z''b'}
# 同时在set1和set2的元素,交集
>>> set1 & set2
{'d''a'}


当然也可以通过其他方法实现上面的运算


>>> set1 = {'a', 'b', 'c', 'd'}>>> set2 = {'a', 'd', 'z', 'x'}# 交集>>> set1.intersection(set2){'d', 'a'}# 合集>>> set1.union(set2){'d', 'a', 'x', 'c', 'z', 'b'}# 差集>>> set1.difference(set2){'b', 'c'}'a''b''c''d'}
>>> set2 = {'a''d''z''x'}
# 交集
>>> set1.intersection(set2)
{'d''a'}

# 合集
>>> set1.union(set2)
{'d''a''x''c''z''b'}

# 差集
>>> set1.difference(set2)
{'b''c'}


清空集合内容

使用clear() 来删除集合内容


>>> a = {'a', 'b', 'c', 'd'}>>> a.clear()# 返回一个空集合(set)>>> aset()'b''c''d'}
>>> a.clear()
# 返回一个空集合(set)
>>> a
set()


其他补充


获取集合的长度,判断元素是否在集合中与上面的列表方法一样。在这里就不赘述了。


下一节的内容讲述一下 元组(tuple)和字典(dictionary)





万水千山总是情,点个「在看」行不行。




[Python从入门到上瘾] 3、Python中的基本数据类型(中)


相关文章:

  • 2018-08-17
  • 2021-10-01
  • 2022-12-23
  • 2021-10-25
  • 2021-11-06
  • 2022-01-11
  • 2021-04-05
  • 2021-08-19
猜你喜欢
  • 2021-10-06
  • 2021-10-20
  • 2022-12-23
  • 2021-08-08
  • 2021-12-11
  • 2022-12-23
  • 2021-09-26
相关资源
相似解决方案