dict-list-tuple-string

python中dict、list、tuple、string几种数据类型的一些用法。

字典按value排序

保存为字典后,按字典value值大小排序

1
2
3
4
5
6
7
8
9
10
11
12
13
a = ["a", "b", "a", "c", "a", "c", "b", "d", "e", "c", "a", "c"]

# set集合去重
s = set(a)

# 保存为dict
d = {}
for i in s:
d[i] = a.count(i)

# 对字典排序
a = sorted(d.items(), key=lambda x: x[1], reverse=True)
print(a)

reference

lambda用法说明

reference 1
reference 2

list转字符串

1
2
3
list=[1,2,3,4,5]
''.join(list) 为:12345
','.join(list) 为:1,2,3,4,5

根据路径获取文件名

1
os.path.dirname(path)
Contents
  1. 1. 字典按value排序
  2. 2. lambda用法说明
  3. 3. list转字符串
  4. 4. 根据路径获取文件名
|