1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| food = ["pizza","hotdog","hamburger"]
food.append("ice cream")
food.remove("hotdog")
food.pop()
food.insert(0, "cake")
food.sort()
food.clear()
print(food)
# 排序
food.sort() # 改变原数组
sorted_food = sorted(food, reverse=True) # 返回新数组
ss = [("Tom","F", 40),
("Sandy","A", 33),
("Jack","B", 20)]
grade = lambda grades:grades[1]
ss.sort(key=grade) #按每个tuple的第二项进行排序
# map(function, iterable), filter(funciton, iterable)
store = [("shirt", 20.00), ("pants", 25.00)]
to_eruos = lambda data: (data[0], data[1]*0.82)
store_euros = list(map(to_eruos, store)) # 返回的都是tuple类型
# 遍历数组可以用for循环,也提供了简化的语法
squares = []
for i in range (1, 11):
squares.append(i * i)
squares = [i*i for i in range(1,11)] # 一行搞定
students = [100,90,80,70,60,50,40]
# 从数组中筛选出及格的可以这样写
passed_students = list(filter(lambda x:x>=60, students))
passed_students = [i for i in students if i >= 60]
passed_students = [i if i >= 60 else "failed" for i in students]
|