[英]Debbugging-For fixing a function
I am debugging the following function: 我正在调试以下功能:
def buggy_dedup_sort_by_len(input):
unique = list(set(input))
return unique.sort(key=len)
the list is sorted but the unique.sort(key=len) is returning nothing. 列表已排序但unique.sort(key = len)没有返回任何内容。 wont the function list.sort return anything.How can I fix it??? 不会函数list.sort返回任何东西。我怎么能修复它???
sort
returns None
because it mutates the list in-place. sort
返回None
因为它会就地改变列表。 Try: 尝试:
def buggy_dedup_sort_by_len(input):
unique = list(set(input))
unique.sort(key=len)
return unique
Alternatively, use sorted
, which does return a list. 或者,使用sorted
,它会返回一个列表。
def buggy_dedup_sort_by_len(input):
unique = list(set(input))
return sorted(unique, key=len)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.