嗨的功能,我需要用一個列表理解改寫單列表理解
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