小编Che*_*ese的帖子

python排序函数中的关键参数如何工作?

说我有

votes = {'Charlie': 20, 'Able': 10, 'Baker': 20, 'Dog': 15}
Run Code Online (Sandbox Code Playgroud)

我明白

print(sorted(votes.items(), key = lambda x: x[1]))
Run Code Online (Sandbox Code Playgroud)

会导致

[('Able',10),('Dog',15),('Baker',20),('Charlie',20)]`

但这是如何工作的?

python sorting dictionary

8
推荐指数
3
解决办法
4309
查看次数

检查变量是否是几种类型?(str或list)

我知道如果我想检查例如变量x是否是我将要做的str

if type(x) is str:
Run Code Online (Sandbox Code Playgroud)

但是如何检查x是字符串还是列表?所以我可以这样做,而无需添加另一个if分支

if type(x) is (str, list):
     pass
else:
     raise SomeError
Run Code Online (Sandbox Code Playgroud)

python types if-statement boolean

1
推荐指数
1
解决办法
248
查看次数

标签 统计

python ×2

boolean ×1

dictionary ×1

if-statement ×1

sorting ×1

types ×1