python基础教程——列表

来源:互联网 发布:linux traceroute没有 编辑:程序博客网 时间:2024/05/21 11:01
列表不同于元组和字符串,列表是可变的,可以改变列表的内容,并且列表有很多有用的,专门的方法。

list函数

字符串不能像列表一样被修改,所以有时根据字符串创建列表会很有用。list函数可以实现这个操作:
list(‘hello’)

[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

需要注意的是,list函数适用于所有类型的序列,而不只是字符串。

列表基本操作

1.元素赋值

x = [1,1,1]
x[1] = 2
x

[1, 2, 1]
注:不能为一个不存在的元素进行赋值。

2.删除元素

names = [‘Alice’, ‘Beth’, ‘Cecil’, ‘Dee-Dee’, ‘Earl’]
del names[2]
names

[‘Alice’, ‘Beth’, ‘Dee-Dee’, ‘Earl’]

3.分片赋值

name = list(‘Perl_1’)
name

[‘P’, ‘e’, ‘r’, ‘l’, ‘_’, ‘1’]

name[2:] = list(‘ar’)
name

[‘P’, ‘e’, ‘a’, ‘r’]

name = list(‘Perl_1’)
name[2:4] = list(‘ar’)
name

[‘P’, ‘e’, ‘a’, ‘r’, ‘_’, ‘1’]

分片赋值,可以适用与原序列不等长的序列将分片替换
name[1:] = list(‘ython’)
name

[‘P’, ‘y’, ‘t’, ‘h’, ‘o’, ‘n’]

numbers = [1,5]
numbers[1:1] = [2,3,4]
numbers

[1, 2, 3, 4, 5]

//如此赋值需要等长
numbers[::2] = [None] * (len(numbers) // 2 + len(numbers) % 2)
numbers

[None, 2, None, 4, None]

numbers[-2::-2] = [None] * (len(numbers) // 2)
numbers

[None, None, None, None, None]

列表方法

对象.方法(参数)

1.append

lst = [1,2,3]
lst.append(4)
lst

[1, 2, 3, 4]

lst.append([5,6])
lst

[1, 2, 3, 4, [5, 6]]

注意:append方法和其他一些方法类似,只是在恰当的位置修改原来的列表。这意味着,他不是简单的返回一个修改过的新列表——而是直接修改原来的列表。

2.count

count方法统计某个元素在列表中出现的次数
to’,’be’,’or’,’not’,’to’,’be’].count(‘to’)

2

x = [[1,2],1,1,[2,1,[1,2]]]
[1,2] in x

True

x = [[1,2],1,1,[2,1,[1,2,3]]]
[1,2,3] in x

False

x.count(1)

2

x.count([1,2])

1

3.extend

extend方法可以在列表的末尾一次性追加另一个序列中的多个值。
a = [1,2,3]
b = [4,5,6]
a.extend(b)
a

[1, 2, 3, 4, 5, 6]

这个操作看起来很像连接操作(+),两者的主要区别在于:extend方法修改了被扩展的序列。而连接操作则不是,他会返回一个全新的列表:
a = [1,2,3]
b = [4,5,6]
a + b

[1, 2, 3, 4, 5, 6]

a

[1, 2, 3]

4.index

index方法用于从列表中找出某个值第一个匹配项的索引位置:
kinghts = [‘We’, ‘are’, ‘the’, ‘knights’, ‘who’, ‘say’, ‘ni’]
kinghts.index(‘who’)

4

kinghts.index(‘herring’)

Traceback (most recent call last):
File “”, line 1, in
ValueError: ‘herring’ is not in list

kinghts[4]

‘who’

5.insert

insert用于将对象插入到列表中:
numbers = [1,2,3,4,5,6,7]
numbers.insert(3,’four’)
numbers

[1, 2, 3, ‘four’, 4, 5, 6, 7]

6.pop

x = [1,2,3]
x.pop()

3

注意:pop方法是唯一一个既能修改列表又能返回元素值(除了None)的列表方法。
x = [1,2,3]
x.append(x.pop())
x

[1, 2, 3]

7.remove

x = [‘to’,’be’,’or’,’not’,’to’,’be’]
x.remove(‘be’)
x

[‘to’, ‘or’, ‘not’, ‘to’, ‘be’]

可以看到:只有第一次出现的值被移除了。没有出现在列表中的值,不会被移除。值得注意的是,remove是一个没有返回值的原位置改变方法。他修改了列表却没有返回值,这与pop正好相反。

8.reverse

reverse方法将列表中的元素反向存放。
x = [‘to’,’be’,’or’,’not’,’to’,’be’]
x.reverse()
x

[‘be’, ‘to’, ‘not’, ‘or’, ‘be’, ‘to’]

此方法也改变了列表但不返回值。
注:如果需要对一个序列进行反向迭代(列表是序列的一种),可以使用reversed函数。这个函数返回一个迭代器对象(iterator),可以使用list()函数把返回的对象转换成列表:
x = [1,2,3]
list(reversed(x))

[3, 2, 1]

x//x值没改变

[1, 2, 3]

9.sort

sort方法用于在原位置对列表进行排序,默认生序。sort返回的还是原来的列表,而不是一个以排序的列表副本。
需要注意的是,sort方法返回的是空值。
x = [4,5,1,6,12,36,123,2,3,3]
x.sort()
x

[1, 2, 3, 3, 4, 5, 6, 12, 36, 123]

y = x.sort()//不要这么做
print(y)

None

y = x//如此赋值,x和y指向同一个列表
y.reverse()
y

[123, 36, 12, 6, 5, 4, 3, 3, 2, 1]

x

[123, 36, 12, 6, 5, 4, 3, 3, 2, 1]

可以使用如下方法复制列表:
第一种:y = x[:]
第二种:y = sorted(x)//sorted函数总返回一个列表
如此再更改y时,可以保证x不被改变。

另:如果想要逆向排序,可以先使用sort方法,再使用reverse方法

10.高级排序

如果希望元素能按照特定的方式进行排序,可以通过给sort方法传参数来实现。

x = [‘asdfasdf’, ‘dsf’, ‘ertsh’, ‘wrtehdfnxcjjh’, ‘hdhfgjjkfddfvsdgfd’]
x.sort(key = len)
x

[‘dsf’, ‘ertsh’, ‘asdfasdf’, ‘wrtehdfnxcjjh’, ‘hdhfgjjkfddfvsdgfd’]\

x.sort(key = len, reverse = True)
x

[‘hdhfgjjkfddfvsdgfd’, ‘wrtehdfnxcjjh’, ‘asdfasdf’, ‘ertsh’, ‘dsf’]

0 0
原创粉丝点击