操作列表

遍历列表

dragons = ["Zephy", "Minessa", "Karikaro", "Laponette", "Charlotta", "Faria", "Monet", "Chiquita", "Pio"]
for dragon in dragons: #for 遍历,':'不能少
        print(f"\t{dragon.title()} is a dragon-witch")
        print(f"I am happy to see you, {dragon.title()}")
        #依靠缩进判断关系,忘记缩进或者少缩进,甚至不必要的缩进也是错误,这就与c中的';'一样重要
print("These are the dragons in 'Dragon Star Varnir'")

结果

    Zephy is a dragon-witch
I am happy to see you, Zephy
    Minessa is a dragon-witch
I am happy to see you, Minessa
    Karikaro is a dragon-witch
I am happy to see you, Karikaro
    Laponette is a dragon-witch
I am happy to see you, Laponette
    Charlotta is a dragon-witch
I am happy to see you, Charlotta
    Faria is a dragon-witch
I am happy to see you, Faria
    Monet is a dragon-witch
I am happy to see you, Monet
    Chiquita is a dragon-witch
I am happy to see you, Chiquita
    Pio is a dragon-witch
I am happy to see you, Pio
These are the dragons in 'Dragon Star Varnir'

数值列表

for value in range(1, 5):
    print(value)##差一行为,1-5代表1,2,3,4
even_numbers = list(range(2, 12, 2))##list可以将range()转化为一胜一负列表
print(even_numbers)##可以看到差一行为导致没有12

## 创建特定数组的办法,比如1-10的平方
squares = []
for value in range (1, 11):
    square = value ** 2
    squares.append(square)
print(squares)

## 简单计算
print(f"min: {min(squares)} max: {max(squares)} sum: {sum(squares)}")

## 列表解析
cubic = [value**3 for value in range(1, 11)]
## 指定的列表名 = [表达式 for循环提供值给列表式]
print(cubic)
test = [sum(squares)/sum(cubic) for value in range(1, 11)]
print(test)
test = [sum(squares)/value for value in cubic]
print(test)

结果

1
2
3
4
[2, 4, 6, 8, 10]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
min: 1 max: 100 sum: 385
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726, 0.12727272727272726]
[385.0, 48.125, 14.25925925925926, 6.015625, 3.08, 1.7824074074074074, 1.1224489795918366, 0.751953125, 0.5281207133058985, 0.385]

使用列表的一部分

dragons = ["Zephy", "Minessa", "Karikaro", "Laponette", "Charlotta", "Faria", "Monet", "Chiquita", "Pio"]
print(dragons[0:3])
print(dragons[1:4])##差一行为
print(dragons[:4])
print(dragons[5:])
print(dragons[-3:])
for dragon in dragons[1:4]:
    print(f"I love {dragon}. だいすき(daisuki)")
print(f"别看了太艹了")#记得缩进问题

list = dragons[:]
print(list.pop())
print(f"list\t{list}")
print(f"dragons\t{dragons}")
##使用List = dragons 则是关联而不是复制
list_2 = list
list.append("Cosettia")
print(f"list:\t{list}")
print(f"list_2:\t{list_2}")

结果

['Zephy', 'Minessa', 'Karikaro']
['Minessa', 'Karikaro', 'Laponette']
['Zephy', 'Minessa', 'Karikaro', 'Laponette']
['Faria', 'Monet', 'Chiquita', 'Pio']
['Monet', 'Chiquita', 'Pio']
I love Minessa. だいすき(daisuki)
I love Karikaro. だいすき(daisuki)
I love Laponette. だいすき(daisuki)
别看了太艹了
Pio
list    ['Zephy', 'Minessa', 'Karikaro', 'Laponette', 'Charlotta', 'Faria', 'Monet', 'Chiquita']
dragons    ['Zephy', 'Minessa', 'Karikaro', 'Laponette', 'Charlotta', 'Faria', 'Monet', 'Chiquita', 'Pio']
list:    ['Zephy', 'Minessa', 'Karikaro', 'Laponette', 'Charlotta', 'Faria', 'Monet', 'Chiquita', 'Cosettia']
list_2:    ['Zephy', 'Minessa', 'Karikaro', 'Laponette', 'Charlotta', 'Faria', 'Monet', 'Chiquita', 'Cosettia']

元组

menu = ("a",  "b", "c", "d", "e")
for dish in menu:
    print(f"we provide {dish}.")
menu = ("c", "d", "e", "f", "g")
for dish in menu:##元组是不能赋值的,但可以重新定义
    print(f"we provide {dish} today.")

结果

we provide a.
we provide b.
we provide c.
we provide d.
we provide e.
we provide c today.
we provide d today.
we provide e today.
we provide f today.
we provide g today.

代码格式

PEP 8

最后修改:2022 年 02 月 14 日
如果觉得我的文章对你有用,留个言就可以了