Python不常用用法

Python 日常使用时的一些方法或技巧,怕忘记怕麻烦,记录一下

通过值获取键


1
2
3
4
5
>>> dct = {'Name': 'Alice', 'Age': 18, 'uid': 1001, 'id': 1001}
>>> def get_key1(dct, value):
... return list(filter(lambda k:dct[k] == value, dct))
>>> get_key1(dct, 1001)
['id', 'uid']
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
# 某函数返回的结果(参数 md5dict)
{
'/root/MyDocs/Python/02-PhotosMD5/76478297_p0.jpg': '6beb7af8d37e26eb88bb1b75c8e51474',
'/root/MyDocs/Python/02-PhotosMD5/76478297_p0_copy.jpg': '6beb7af8d37e26eb88bb1b75c8e51474',
'/root/MyDocs/Python/02-PhotosMD5/561284.jpg': '4f9d1c75bd62b0d7e3fdd0abd810ce34',
'/root/MyDocs/Python/02-PhotosMD5/561271.jpg': 'bb60b6e6626968cde17cad0a8f18290b',
'/root/MyDocs/Python/02-PhotosMD5/561279.jpg': 'e5af4c3275dafbef5932ab69c446374c'
}

# 变量 cp的内容
('6beb7af8d37e26eb88bb1b75c8e51474', 2)
('4f9d1c75bd62b0d7e3fdd0abd810ce34', 1)
('bb60b6e6626968cde17cad0a8f18290b', 1)
('e5af4c3275dafbef5932ab69c446374c', 1)


def analysedDict(md5dict):
cp = Counter(md5dict.values()) # 只取 MD5值

def filterFun(dicts, value):
bad = list(filter(lambda k:dicts[k] == value, dicts))
print(bad)

for i in cp.items():
if i[1] > 1:
print("\033[0;31m{0}\033[0m".format(i))
filterFun(md5dict, i[0])

# ---- 结果 ------
# ('6beb7af8d37e26eb88bb1b75c8e51474', 2)
# ['/root/MyDocs/Python/02-PhotosMD5/76478297_p0.jpg', '/root/MyDocs/Python/02-PhotosMD5/76478297_p0_copy.jpg']

快速启动 http服务器


1
2
# python2
python -m SimpleHTTPServer