python---数据类型(列表)

组织列表
使用sort()方法对列表永久性排序

按照字母顺序排序:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']

motorcycles.sort()

print(motorcycles)

字母倒序:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']

motorcycles.sort(reverse=True)

print(motorcycles)
使用sorted()函数进行临时排序
motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']

print("Here is the original list:")
print(motorcycles)

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

print("\nHere is the original list again:")
print(motorcycles)

也可以向sorted()传递参数reverse=True倒序排列:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']

print("Here is the original list:")
print(motorcycles)

print("\nHere is the sorted list:")
print(sorted(motorcycles, reverse=True))

print("\nHere is the original list again:")
print(motorcycles)
倒着打印列表

不是倒序,是倒着,该方法也是永久性地颠倒列表的顺序,不过可以再次调用,恢复之前的顺序:

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']
print(motorcycles)
motorcycles.reverse()

print(motorcycles)

motorcycles.reverse()
print(motorcycles)
确定列表的长度

使用len()函数

motorcycles = ['chunlan', 'yamaha', 'dayun', 'jianshe']
print(len(motorcycles))
操作列表
遍历列表

使用for循环:

magicians = ['alice', 'david', 'carolina', 'john', 'kite']
for magician in magicians:
    print(magician)
深入循环

使用缩进表示语句属于for的循环体:

magicians = ['alice', 'david', 'carolina', 'john', 'kite']
for magician in magicians:
    print(magician.title() + ", that was a great trick!")
    print("I can't wait to see your next trick, " + magician.title() + ".\n")

也可以在循环结束后执行操作,只需要不再缩进即可:

magicians = ['alice', 'david', 'carolina', 'john', 'kite']
for magician in magicians:
    print(magician.title() + ", that was a great trick!")
    print("I can't wait to see your next trick, " + magician.title() + ".\n")


print("Thank you, everyone. That was a great magic show!")
创建数值列表
使用函数range()

打印1-4:

for value in range(1, 5):
    print(value)
使用range()创建数字列表

使用list()函数将range()的结果直接转换为列表:

numbers = list(range(1,6))
print(numbers)

even_numbers = list(range(2,11,2))  #最后一个2是步长
print(even_numbers)

创建平方的列表:

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

print(squares)

也可以简写为:

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

print(squares)
对数字列表执行简单的统计
digits = [1,2,3,4,5,6,7,8,9,0]
print(min(digits))
print(max(digits))
print(sum(digits))
列表解析

列表解析将for循环和创建新的元素代码合成一行,并自动附加新元素。了解一下

squares = [value**2 for value in range(1,11)]
print(squares)
使用列表的一部分
切片

要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与range()函数一样,python在到达指定的第二个索引前面的元素停止。range(1,10)

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
print(players[1:4])
print(players[:3])
print(players[2:])
print(players[-3:])
遍历切片
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
    print(player.title())

复制列表

players = ['charles', 'martina', 'michael', 'florence', 'eli']
ourplayers = players[:]
print("Players are:")
print(players)
print("Ourplayers are:")
print(ourplayers)

所谓的列表,就是一个序列,其中的元素有序,可变,不限类型

你可能感兴趣的:(python,python,前端,javascript)