numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(*numbers)
0 1 2 3 4 5 6 7 8 9
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for number in numbers: print(number)
0
1
2
3
4
5
6
7
8
9
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
double = [number * 2 for number in numbers]
print(double)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reverse = list(reversed(numbers))
print(reverse)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
reverse2 = numbers[::-1]
print(reverse2)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
indexes = [2, 4, 3]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
for index in sorted(indexes, reverse=True): del numbers[index]
print(numbers)
[0, 1, 5, 6, 7, 8, 9]
numbers = [5, 2, 1, 6, 4]
index_min = min(range(len(numbers)), key = numbers.__getitem__)
index_max = max(range(len(numbers)), key = numbers.__getitem__)
print(index_min, numbers[index_min])
print(index_max, numbers[index_max])
2 1
3 6
numbers = [2, 2, 3, 3, 4, 1]
new = list(set(numbers))
print(new)
[1, 2, 3, 4]
from collections import OrderedDict
numbers = [2, 2, 3, 3, 4, 1]
new = list(OrderedDict.fromkeys(numbers).keys())
print(new)
[2, 3, 4, 1]
from collections import Counter
numbers = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 1]
print(Counter(numbers))
Counter({2: 4, 1: 3, 3: 3, 4: 1, 5: 1})
numbers = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 1]
print(max(set(numbers), key = numbers.count))
2
from collections import Counter
numbers = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 1]
cnt = Counter(numbers)
print(cnt.most_common(1))
[(2, 4)]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
inserts = [2.3, 2.5]
numbers[3:3] = inserts
print(numbers)
[0, 1, 2, 2.3, 2.5, 3, 4, 5, 6, 7, 8, 9]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
_in = [5, 12]
_not_in = [111, 222]
print(any(_ in numbers for _ in _in))
print(any(_ in numbers for _ in _not_in))
True
False
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd = [_ for index,_ in enumerate(numbers) if index % 2]
print(odd)
[1, 3, 5, 7, 9]
import itertools
numbers = [[0], [1, 2], ['3', 4, 5, 6], ['7', '8'], [9]]
list(itertools.chain.from_iterable(numbers))
[0, 1, 2, '3', 4, 5, 6, '7', '8', 9]
numbers = [1, 2, 3]
chars = ['a', 'b', 'c']
z = zip(numbers, chars)
list(z)
[(1, 'a'), (2, 'b'), (3, 'c')]
numbers = [1, 2, 3]
chars = ['a', 'b', 'c']
z = zip(numbers, chars)
z2 = zip(*z)
list(z2)
[(1, 2, 3), ('a', 'b', 'c')]
string = ['Python', 'is', 'YYDS']
' '.join(string)
'Python is YYDS'
a = [1, 2, 3]
b = a
b[0] = 0
print(id(a), a)
print(id(b), b)
print('---')
a = [1, 2, 3]
b = a[:]
b[0] = 0
print(id(a), a)
print(id(b), b)
print('---')
a = [1, 2, 3]
b = a.copy()
print(id(a), a)
print(id(b), b)
b[0] = 7
print(id(a), a)
print(id(b), b)
print('---')
from copy import deepcopy
a = [1, 2, 3]
b = deepcopy(a)
print(id(a), a)
print(id(b), b)
b[0] = 8
print(id(a), a)
print(id(b), b)
1896156101440 [0, 2, 3]
1896156101440 [0, 2, 3]
---
1896156101696 [1, 2, 3]
1896156101120 [0, 2, 3]
---
1896156102528 [1, 2, 3]
1896156102080 [1, 2, 3]
1896156102528 [1, 2, 3]
1896156102080 [7, 2, 3]
---
1896156101440 [1, 2, 3]
1896156101632 [1, 2, 3]
1896156101440 [1, 2, 3]
1896156101632 [8, 2, 3]
欢迎交流
-- EOF --