您好我需要使用单个列表解析重写此代码
result = []
for word in words:
wordlenpair = (word, len(word))
result.append(wordlenpair)
return result
但我堆满了:
result = [wordlenpair for word in words]
我不知道该怎么办这条线:
wordlenpair = (word, len(word))
答案 0 :(得分:1)
如果你想要一个元组列表,你可以在列表理解中使用你想要的元组:
result = [(word, len(word)) for word in words]