python进阶(2)--列表

文档目录:
一、访问列表元素
二、更新列表
三、删除元素
四、列表排序
五、len()与range()/list()
六、创建一个包含1-10平方的列表
七、复制列表两种方式
八、元组:不可变的列表

---------------------------------------分割线:正文--------------------------------------------------------

一、访问列表元素

1、直接访问

#直接访问
print(myTest[0])
print(myTest[-1])
print(f"my letter is {myTest[1]}")

查看结果

a
c
my letter is b

2、访问列表切片

myTest=['a','b','c','e','f']
#访问切片,左闭右开
print(myTest[0:1])
print(myTest[:2])
print(myTest[1:3])
print(myTest[1:])
print(myTest[:])
print(myTest[-2:])
print(myTest[0:5:2])#取对应步长

查看结果

['a']
['a', 'b']
['b', 'c']
['b', 'c', 'e', 'f']
['a', 'b', 'c', 'e', 'f']
['e', 'f']
['a', 'c', 'f']

3、遍历列表

myTest=['a','b','c','e','f']
for letter in myTest:
    print(myTest)
a
b
c
e
f

二、更新列表

1、新增元素

testList=['x','y','z']
#添加元素-末尾
testList.append("u")
print(testList)
#指定位置查询
testList.insert(1,"v")
print(testList)

查看结果

['x', 'y', 'z', 'u']
['x', 'v', 'y', 'z', 'u']

2、修改元素

#修改元素
testList=['x','y','z']
testList[1]='ok'
print(testList)

查看结果

['x', 'ok', 'z']

三、删除元素

1、del

testList=['x','y','z','o']
#删除指定位置元素
del testList[0]
print(testList)

查看结果

['y', 'z', 'o']

2、pop弹出元素

#删除并弹出最后一个元素
testList=['x','y','z','o']
popTest= testList.pop();
print(popTest)
print(testList)

查看结果

['x', 'y', 'z']
z

3、pop弹出指定位置元素

#删除并弹出指定位置元素最后一个元素
testList=['x','y','z','o']
popTest= testList.pop(2);
print(popTest)
print(testList)

查看结果

z
['x', 'y', 'o']

 四、列表排序

1、字母顺利正序

cars=['ac','aa','ba','ab']
#字母顺序正序
cars.sort()
print(cars)

查看结果

['aa', 'ab', 'ac', 'ba']

2、字母顺利反序

cars=['ac','aa','ba','ab']
#字母顺序反序
cars.sort(reverse=True)
print(cars)

查看结果

['ba', 'ac', 'ab', 'aa']

3、临时排序

cars=['ac','aa','ba','ab']
#临时排序
cars2=sorted(cars)
print(cars)
print(cars2)

查看结果

['ac', 'aa', 'ba', 'ab']
['aa', 'ab', 'ac', 'ba']

4、反转列表

cars=['ac','aa','ba','ab']
#反转列表
cars.reverse()
print(cars)

查看结果

['ab', 'ba', 'aa', 'ac']

五、len()与range()/list()

1、列表长度len()

cars=['ac','aa','ba','ab']
print(len(cars))

查看结果

4

2、range()与list()一起使用

#range()与list()
print(list(range(6)))
print(list(range(0,6)))
print(list(range(2,5)))
print(list(range(0,8,2)))

查看结果

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

六、创建一个包含1-10平方的列表

1、for循环

squares=[]
for value in range(1,11):
    squares.append(value**2)
print(squares)

2、列表解析

squares=[]
squares=[value**2 for value in range(1,11)]
print(squares)

查看结果:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

七、复制列表两种方式

1、直接赋值:相同对象

myfoods=['a','b','c']
myfoods2=myfoods
#改变myfood
myfoods[0]='A'
print(myfoods)
print(myfoods2)

查看结果:

['A', 'b', 'c']
['A', 'b', 'c']

2、重新赋值:不同对象

myfoods=['a','b','c']
myfoods3=myfoods[:]
#改变myfood
myfoods[0]='A'
print(myfoods)
print(myfoods3)

查看结果:

['A', 'b', 'c']
['a', 'b', 'c']

还有两种用法是copy()与deepcopy(),不太常用

b = a.copy(): 浅拷贝, a 和 b 是一个独立的对象,但他们的子对象还是指向统一对象

b = copy.deepcopy(a): 深度拷贝, a 和 b 完全拷贝了父对象及其子对象,两者是完全独立的

八、元组:不可变的列表

1、查看元组内容

dimensions=(200,50)
print(type(dimensions))
print(dimensions[0])

查看结果:

<class 'tuple'>
200

2、遍历元组

dimensions=(200,50)
for dimension in dimensions:
    print(dimension)

查看结果:

200
50

3、修改元组变量

dimensions=(200,50)
dimensions=(400,100)
print(dimensions)

查看结果

(400, 100)
原文地址:https://www.cnblogs.com/mrwhite2020/p/14656088.html