龙空技术网

Python学习笔记—列表

鲸鱼Xmr 105

前言:

如今小伙伴们对“python将数字存入列表”可能比较关心,你们都需要学习一些“python将数字存入列表”的相关文章。那么小编同时在网上汇集了一些有关“python将数字存入列表””的相关文章,希望看官们能喜欢,小伙伴们快快来了解一下吧!

# _*_ coding:utf-8 _*_

#########列表简介#########

bicycles = ['trek', 'cannondale', 'redline', 'specialized']

print(bicycles)

-------------------------------------------

['trek', 'cannondale', 'redline', 'specialized']

-------------------------------------------

#访问列表元素

print(bicycles[0].title())

-------------------------------------------

Trek

-------------------------------------------

#通过将索引指定为-1,可以让python返回最后一个列表元素

print(bicycles[-1])

-------------------------------------------

specialized

-------------------------------------------

#修改、添加和删除元素

#修改元素

motorcycles = ['honda', 'yamaha', 'suzuki']

print(motorcycles)

motorcycles[0] = 'ducati'

print(motorcycles)

-------------------------------------------

['honda', 'yamaha', 'suzuki']

['ducati', 'yamaha', 'suzuki']

-------------------------------------------

#在列表末尾添加元素

motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.append('ducati')

print(motorcycles)

-------------------------------------------

['honda', 'yamaha', 'suzuki', 'ducati']

-------------------------------------------

#在列表中插入元素

motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0,'ducati')

print(motorcycles)

-------------------------------------------

['ducati', 'honda', 'yamaha', 'suzuki']

-------------------------------------------

#从列表中删除元素

#使用del语句删除元素

motorcycles = ['honda', 'yamaha', 'suzuki']

del motorcycles[0]

print(motorcycles)

-------------------------------------------

['yamaha', 'suzuki']

-------------------------------------------

#使用方法pop()删除列表末尾元素,并让你能够接着使用它

motorcycles = ['honda', 'yamaha', 'suzuki']

poppde_motorcycle = motorcycles.pop()

print(motorcycles)

print(poppde_motorcycle)

-------------------------------------------

['honda', 'yamaha']

suzuki

-------------------------------------------

#弹出列表中任何位置处的元素

motorcycles = ['honda', 'yamaha', 'suzuki']

first_owned = motorcycles.pop(0)

print(first_owned)

-------------------------------------------

honda

-------------------------------------------

#根据值删除元素

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']

motorcycles.remove('yamaha')

print(motorcycles)

-------------------------------------------

['honda', 'suzuki', 'ducati']

-------------------------------------------

#组织列表

#使用方法sort()对列表按字母顺序进行永久性排序

cars = ['bmw', 'audi', 'toyota', 'subaru']

cars.sort()

print(cars)

-------------------------------------------

['audi', 'bmw', 'subaru', 'toyota']

-------------------------------------------

#按与字母顺序相反的顺序排列列表元素

cars.sort(reverse=True)

print(cars)

-------------------------------------------

['toyota', 'subaru', 'bmw', 'audi']

-------------------------------------------

#使用函数sorted()对列表进行临时排序,如果要按与字母顺序相反的顺序显示列表,也可向函数sorted()传递参数reverse=True

cars = ['bmw', 'audi', 'toyota', 'subaru']

print("Here is the original list:")

print(cars)

print("\nHere is the sorted list:")

print(sorted(cars))

print("\nHere is the sorted list again:")

print(cars)

-------------------------------------------

Here is the original list:

['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:

['audi', 'bmw', 'subaru', 'toyota']

Here is the sorted list again:

['bmw', 'audi', 'toyota', 'subaru']

-------------------------------------------

#倒着打印列表

cars = ['bmw', 'audi', 'toyota', 'subaru']

cars.reverse()

print(cars)

-------------------------------------------

['subaru', 'toyota', 'audi', 'bmw']

-------------------------------------------

#确定列表的长度

cars = ['bmw', 'audi', 'toyota', 'subaru']

print(len(cars))

-------------------------------------------

4

-------------------------------------------

#########操作列表#########

#遍历整个列表

magicians = ['alice', 'david', 'carolina']

for magician in magicians:

print(magician)

-------------------------------------------

alice

david

carolina

-------------------------------------------

#创建数值列表

#使用函数range()

for value in range(1,5):

print(value)

-------------------------------------------

1

2

3

4

-------------------------------------------

#使用range()创建数字列表

number = list(range(1,6))

print(number)

-------------------------------------------

[1, 2, 3, 4, 5]

-------------------------------------------

#还可以指定步长

even_numbers = list(range(2,11,2))

print(even_numbers)

-------------------------------------------

[2, 4, 6, 8, 10]

-------------------------------------------

#如何将1~10的平方加入到一个列表中

squares = []

for value in range(1,11):

square = value**2

squares.append(square)

print(squares)

-------------------------------------------

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

-------------------------------------------

#对数字列表执行简单的统计计算

digits = [1,2,3,4,5,6,7,8,9,0]

print(min(digits))

print(max(digits))

print(sum(digits))

-------------------------------------------

0

9

45

-------------------------------------------

#列表解析

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

print(squares)

-------------------------------------------

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

-------------------------------------------

#使用列表的一部分

#切片

players = ['charles', 'martina', 'michael', 'florence', 'eli']

print(players[0:3]) #打印前三名队员

print(players[:4]) #没有指定起始索引,Python从开头开始提取

print(players[2:]) #提取从第3个元素到列表末尾所有元素

print(players[-3:]) #输出最后三名队员

-------------------------------------------

['charles', 'martina', 'michael']

['charles', 'martina', 'michael', 'florence']

['michael', 'florence', 'eli']

['michael', 'florence', 'eli']

-------------------------------------------

#遍历切片

players = ['charles', 'martina', 'michael', 'florence', 'eli']

for player in players[:3]:

print(player.title())

-------------------------------------------

Charles

Martina

Michael

-------------------------------------------

#复制列表

my_foods = ['pizza', 'falafel', 'carrot cake']

friend_foods = my_foods[:]

my_foods.append('cannoli')

friend_foods.append('ice cream')

print("My favorite foods are:")

print(my_foods)

print("\nMy friend's favorite foods are:")

print(friend_foods)

-------------------------------------------

My favorite foods are:

['pizza', 'falafel', 'carrot cake', 'cannoli']

My friend's favorite foods are:

['pizza', 'falafel', 'carrot cake', 'ice cream']

-------------------------------------------

#元组(不可变的列表称为元组)

#定义元组

dimensions = (200,50)

print(dimensions[0])

print(dimensions[1])

-------------------------------------------

200

50

-------------------------------------------

#遍历元组中的所有值

dimensions = (200,50)

for dimension in dimensions:

print(dimension)

-------------------------------------------

200

50

-------------------------------------------

#修改元组变量

dimensions = (400,100)

for dimension in dimensions:

print(dimension)

-------------------------------------------

400

100

-------------------------------------------

标签: #python将数字存入列表 #python把数字放入列表