关于python:Kangrui‘s-Python-Learning-Dairy-20200719

69次阅读

共计 2608 个字符,预计需要花费 7 分钟才能阅读完成。

明天的内容对于列表及相干操作


拜访列表元素

bicycles = ['trek','cannondale','redline','specialized']
print(bicycles[0])
print(bicycles[0].title())
print(bicycles[-1]) # 返回最初一个元素
message = "My first bicycle was a" + bicycles[0].title() + "."
print(message)

输入后果为:
trek
Trek
specialized
My first bicycle was a Trek.


批改列表元素

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)

输入后果为:
[‘honda’, ‘yamaha’, ‘suzuki’]
[‘ducati’, ‘yamaha’, ‘suzuki’]


# 在列表开端增加元素
motorcycles.append('honda')
print(motorcycles)

# 在列表中插入元素
motorcycles.insert(0,'another')
print(motorcycles)

# 在列表中删除元素
del motorcycles[0]
print(motorcycles)

输入后果为:
[‘ducati’, ‘yamaha’, ‘suzuki’, ‘honda’]
[‘another’, ‘ducati’, ‘yamaha’, ‘suzuki’, ‘honda’]
[‘ducati’, ‘yamaha’, ‘suzuki’, ‘honda’]


# 应用办法 pop() 删除并存储元素
popped_motorcycles = motorcycles.pop() # 将被弹出的值存储起来,默认为末位元素
print(popped_motorcycles)
print(motorcycles)

输入后果为:
honda
[‘ducati’, ‘yamaha’, ‘suzuki’]


# 应用办法 remove() 指定删除某个具体元素
motorcycles.remove('yamaha')
print(motorcycles)
# 只能删除第一个呈现的元素,不能删除所有反复的 

输入后果为:
[‘ducati’, ‘suzuki’]


# 应用 sort() 对列表进行永久性排序
cars = ['bwm','audi','toyota','subaru']
print(cars)
cars.sort() # 默认为字母正排序
print(cars)
cars.sort(reverse = True) # 逆排序
print(cars)

# 应用 sorted() 对列表进行长期排序
print(sorted(cars))
print(cars)

# 单纯反转列表元素
print(cars)
cars.reverse()
print(cars)

输入后果为:
[‘bwm’, ‘audi’, ‘toyota’, ‘subaru’]
[‘audi’, ‘bwm’, ‘subaru’, ‘toyota’]
[‘toyota’, ‘subaru’, ‘bwm’, ‘audi’]
[‘audi’, ‘bwm’, ‘subaru’, ‘toyota’]
[‘toyota’, ‘subaru’, ‘bwm’, ‘audi’]
[‘toyota’, ‘subaru’, ‘bwm’, ‘audi’]
[‘audi’, ‘bwm’, ‘subaru’, ‘toyota’]


# 确定列表元素个数
print(len(cars))

输入后果为:
4


# 遍历整个列表
magicians = ['alice', 'david', 'caroline']
for magician in magicians:
    print(magician.title() + ', that was a great trick !')

输入后果为:
Alice, that was a great trick !
David, that was a great trick !
Caroline, that was a great trick !


# 创立数字列表
for value in range(1, 5): # range() 让 python 从指定的第一个值开始数,并在达到你指定的第二个值后进行,因而不蕴含第二个值
    print(value)
numbers = list(range(1, 5)) # 将系列数字转换为列表
print(numbers)
even_numbers = list(range(2, 11, 2)) # 指定步长为 2,打印偶数
print(even_numbers)

输入后果为:
1
2
3
4
[1, 2, 3, 4]
[2, 4, 6, 8, 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]


# 简略的统计计算
print(min(squares))
print(max(squares))
print(sum(squares))

输入后果为:
1
100
385


# 简要写法
squares = [value**2 for value in range(1,11)]
print(squares)

输入后果为:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


# 应用列表的一部分(切片)print(squares[0:3])
print(squares[:4]) # 未指定结尾,则从第一个开始
print(squares[-3:]) # 开端的三个元素 

输入后果为:
[1, 4, 9]
[1, 4, 9, 16]
[64, 81, 100]


# 复制列表
another_squares = squares[:]
print(another_squares)

输入后果为:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]


# 不可变的列表——元组
dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
for dimension in dimensions:
    print(dimension)

输入后果为:
200
50
200
50


dimensions = (400,100) # 不能批改繁多元素,但能从新定义整体
print(dimensions)

输入后果为:
(400, 100)

正文完
 0