如何在列表推导中打印多个值?

时间:2016-12-11 21:55:26

标签: python list-comprehension

helper :checks

我想打印每个发送的sent = "this is a fun day" sent = sent.split() newList = [ch for ch in sent] print(newList) output = ["this" "is","a","fun","day"] ,每个单词大写和输出应

len

2 个答案:

答案 0 :(得分:3)

您的列表理解应该只生成一个列表,每个迭代包含三个项目:

output = [[word, len(word), word.upper()] for word in sent]

演示:

>>> sent = "this is a fun day"
>>> sent = sent.split()
>>> [[word, len(word), word.upper()] for word in sent]
[['this', 4, 'THIS'], ['is', 2, 'IS'], ['a', 1, 'A'], ['fun', 3, 'FUN'], ['day', 3, 'DAY']]

答案 1 :(得分:1)

您可以在列表推导中使用所需的任何表达式。在这种情况下,列表:

newList = [[ch, len(ch), ch.upper()] for ch in sent]