python中集合的元素可以是任意数据类型_Python数据类型之列表

列表

列表是由一系列按特定循序排列的元素组成(即有序集合)。使用中括号[]来表示,并用逗号来分隔其中的元素。

列表的元素可以是任意类型。

list 列表(序列)中的每个元素都分配一个数字索引,从 0 开始,第一个索引是 0,第二个索引是 1,依此类推。访问list时索引号不能越界,不然会抛出 IndexError 异常。

操作列表(增,删,改,查)

访问列表

通过列表索引访问列表元素

color = ['red', 'yellow', 'blue']print(color[0])>>> red

index:用于从列表中找出某个值第一个匹配项的索引位置。

color = ['red', 'yellow', 'blue']print(color.index('red'))>>> 0

修改列表元素

通过索引修改元素

color = ['red', 'yellow', 'blue']

color[0]= 'black'

print(color)>>>['black', 'yellow', 'blue']

在列表中添加元素

insert:可在列表的任何位置添加新的元素,为此,必须要指定新元素的索引和值。

color = ['red', 'yellow', 'blue']

color.insert(0,'white')print(color)>>> ['white', 'red', 'yellow', 'blue']

append:在列表末尾追加新的对象。

color = ['red', 'yellow', 'blue']

color.append('white')print(color)>>> ['red', 'yellow', 'blue', 'white']

extend:在列表的末尾一次性追加另一个序列中的多个值。

color = ['red', 'yellow', 'blue']

color.extend('white')print(color)>>> ['red', 'yellow', 'blue', 'w', 'h', 'i', 't', 'e']

append和extend的区别在于:

append添加的是一个任意数据类型(数字,字符串,元组,列表,字典等)。

color = ['red', 'yellow', 'blue']

color.append(12)print(color)>>> ['red', 'yellow', 'blue', 12]

color= ['red', 'yellow', 'blue']

color.append('white')print(color)>>> ['red', 'yellow', 'blue', 'white']

color= ['red', 'yellow', 'blue']

color.append(('white'))print(color)>>> ['red', 'yellow', 'blue', 'white']

color= ['red', 'yellow', 'blue']

color.append(['white'])print(color)>>> ['red', 'yellow', 'blue', ['white']]

color= ['red', 'yellow', 'blue']

color.append({'白':'white'})print(color)>>> ['red', 'yellow', 'blue', {'白': 'white'}]

#append 字符串和元组效果一样?

extend是添加一个序列(字符串,元组,列表)。

color = ['red', 'yellow', 'blue']

color.extend('white')print(color)>>> ['red', 'yellow', 'blue', 'w', 'h', 'i', 't', 'e']

color= ['red', 'yellow', 'blue']

color.extend(('white'))print(color)>>> ['red', 'yellow', 'blue', 'w', 'h', 'i', 't', 'e']

color= ['red', 'yellow', 'blue']

color.extend(['white'])print(color)>>> ['red', 'yellow', 'blue', 'white']

color= ['red', 'yellow', 'blue']

color.extend({'白':'white'})print(color)>>>['red', 'yellow', 'blue', '白']

#extend字符串和元组效果也一样?注意extend字典

删除元素

del:通过索引删除

color = ['red', 'yellow', 'blue']delcolor[0]print(color)>>> ['yellow', 'blue']

#使用del可以删除任何位置的列表元素,条件是知道其索引。del可以用于字典元素,甚至是其他变量的删除操作。

pop:移除列表中的一个元素(默认是最后一个),并且返回该元素的值。

color = ['red', 'yellow', 'blue']print(color.pop())>>> blue

#pop方法可以删除列表中任何位置的元素,在括号中指定要删除的元素的索引。

remove:用于删除列表中某个值得第一个匹配项。无返回值。

color = ['red', 'yellow', 'blue']print(color.remove('yellow'))>>> None

str_list = ['in', 'be', 'to', 'in', 'or']

str_list.remove('in')

print(str_list)

>>> ['be', 'to', 'in', 'or']

del是通过索引来删除。pop也是通过索引删除元素,如果没有指定元素索引,默认最后一个。remove是通过指定元素来删除。

pop和remove共同点在于都对原列表进行修改。不同点在于pop通过索引删除元素,remove直接指定要删除的元素,并且一个有返回值,一个没有返回值。两个都对列表进行原地修改(修改原来的列表)。

组织列表(排序)

sort():用于在原位置对列表进行永久性排序,原位置意味着改变原来的列表。而不是简单的返回一个已排序的列表副本。

错误示范:

str_list = ['in', 'be', 'to', 'and', 'or']print(str_list.sort())>>> None

正确示范:

str_list = ['in', 'be', 'to', 'and', 'or']

str_list.sort()print(str_list)>>> ['and', 'be', 'in', 'or', 'to']

如果需要一个排好序的列表又保留原来的列表,简单的赋值不起作用,这样做就会让srt_1和str_2都指向了同一个列表。

str_1 = ['in', 'be', 'to', 'and', 'or']

str_2=str_1.sort()print(str_2)>>> None

需要通过切片复制整个列表,然后对复制的列表进行排序操作。

str_1 = ['in', 'be', 'to', 'and', 'or']

str_2=str_1[:]

str_2.sort()print(str_2)>>> ['and', 'be', 'in', 'or', 'to']

sorted():对列表进行临时排序。

str_1 = ['in', 'be', 'to', 'and', 'or']print(sorted(str_1))print(str_1)>>> ['and', 'be', 'in', 'or', 'to']>>> ['in', 'be', 'to', 'and', 'or']

sorted可以用于任何序列,却总返回一个列表。

str = 'python'

print(sorted(str))>>> ['h', 'n', 'o', 'p', 't', 'y']

sort和sorted都可接受reverse参数:即排序之后倒着打印。

int_list = [5, 8, 3, 7, 2, 6, 1, 4]

int_list.sort(reverse=True)print(int_list)>>> [8, 7, 6, 5, 4, 3, 2, 1]

sorted接收reverse参数怎么写。。。。???

reverse():反转列表元素的排列顺序,不是按字符顺序相反的顺序排列列表元素。

int_list = [5, 8, 3, 7, 2, 6, 1, 4]

int_list.reverse()print(int_list)>>> [4, 1, 6, 2, 7, 3, 8, 5]

#永久性地修改列表元素的排列顺序,但可以随时恢复到原来的排列顺序,只需要对列表再次调用reverse方法即可。

还有一种高级排序,以后说。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值