说我有
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)]`
但这是如何工作的?
我知道如果我想检查例如变量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)