【发布时间】:2019-12-23 22:35:37
【问题描述】:
我有一个字符串,我希望通过在我的列表理解中使用元组(单词,长度)来使用列表理解来打印具有偶数个字母的单词。我尝试了这样的方法来返回包含偶数个字母的单词元组列表:
sentence = 'If you dont actually care about collecting all the strings in a list using a list comprehension doesnt make much sense'
word_with_even_letters = [(word,len(word)) in sentence.split() if len(word)==0]
实际结果:
文件“”,第 2 行 word_with_even_letters = [(word,len(word)) in sentence.split() if len(word)==0] ^ SyntaxError: 无效语法
预期结果:列表 word_with_even_letters 应包含以下元组:
(If,2)
(dont,4)
(actually,8)
(care,4)
(collecting,10)
(in,2)
(list,4)
(doesnt,6)
(make,4)
(much,4)
【问题讨论】:
标签: python-3.x list tuples list-comprehension