-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistcomps.py
More file actions
32 lines (23 loc) · 789 Bytes
/
Copy pathlistcomps.py
File metadata and controls
32 lines (23 loc) · 789 Bytes
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
#!/usr/bin/env python3
# ! -*- coding:utf-8 -*-
import os
print(list(range(1, 11)))
L = []
for x in range(1, 11):
L.append(x * x)
print(L)
print([x * x for x in range(1, 11)]) # use the listcomps
print([m+n for m in 'ABC' for n in 'XYZ']) # listcomps双层循环全排列AX,BY,CZ
print([d for d in os.listdir('.')]) # os.listdir可以列出文件和目录
d = {'x': 'A', 'y': 'B', 'z': 'C' }
print([k+'='+v for k,v in d.items()]) # use two var with listcomps
L = ['Hello', 'World', 'IBM', 'Apple']
print([s.lower() for s in L])
L1 = ['Hello', 'World', 18, 'Apple', None]
L2 = [a.lower() for a in L1 if isinstance(a, str)] #use if with listcomps
# 测试:
print(L2)
if L2 == ['hello', 'world', 'apple']:
print('测试通过!')
else:
print('测试失败!')