嗨的功能,我需要用一个列表理解改写单列表理解
result = []
for word in words:
wordlenpair = (word, len(word))
result.append(wordlenpair)
return result
改写这个代码,但我堆放着:
result = [wordlenpair for word in words]
,我不知道该怎么办与此行在这里:
wordlenpair = (word, len(word))
只需用元组替换'wordlenpair'即可。它只是一个变量,将其替换为原始表达式。 –
提示:在转换之前将循环内的代码缩短为一行。 –
非常感谢,现在我明白了! – Hot41