Python Comprehensions
List Comprehension
Practice:
知识点:
list comprehensions 在列表推导式中
for i in list_test
后面不需要加:
常见的使用姿势:1
[ [ i, j] for i in range( x + 1) for j in range( y + 1) if ( ( i + j ) != n )
1 | marksheet = [] |
知识点:
将列表转换为 set 去重
list comprehensions
join 在输出换行等分隔符时的用法
list.sort(key=..., reverse=...)
By default, sort() doesn't require any extra parameters. However, it has two optional parameters: reverse - If true, the sorted list is reversed (or sorted in Descending order) key - function that serves as a key for the sort comparison usage:
1
2
3
4
5
6
7
8
9
10
11
12# take second element for sort
def takeSecond(elem):
return elem[1]
# random list
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# sort list with key
random.sort(key=takeSecond)
# print list
print('Sorted list:', random)