求助
['be', 'May', 'and', 'your', 'short', 'coffee', 'strong', 'Monday']请问上面这种怎么写 然后变成下面这种呢?
my_dict = {6:['monday', 'coffee', 'strong'], 5:['short'], 3:['may', 'and'],2:['be']}
谢谢!!!
这样即可:
temp = ['be', 'May', 'and', 'your', 'short', 'coffee', 'strong', 'Monday']
my_dict = {}
for i in temp:
if len(i) not in my_dict:
my_dict =
else:
my_dict.append(i)
print(my_dict) def function(list, /) -> dict:
result = {}
for i in list:
result.setdefault(len(i), []).append(i)
return result 麻烦你下次写明是怎么转换的{:10_306:}我花了好长时间才搞明白
页:
[1]