过滤器对象python 3.3后打印变量

时间:2013-04-30 11:01:33

标签: python python-3.3

我正在创建一个由过滤器对象组成的列表,该过滤器对象查询变量并忽略错误的变量并打印结果列表,然后是同一行上的变量,该变量不包含在过滤器中。例如:

nature = "cow"
creator = ""
minor = ""
item = "hammer"
NAMEPROPERTIES = [nature, creator, minor]
propertiestrue = (filter(None, NAMEPROPERTIES))

然后我尝试:

print (*propertiestrue)
cow

哪个有效,因为输出是牛,但是:

print (*propertiestrue, item)
SyntaxError: only named arguments may follow *expression

我还尝试完全分离过滤器部分:

print ((*filter(None, NAMEPROPERTIES)), nature)
  File "<stdin>", line 1
SyntaxError: can use starred expression only as assignment target

我已经尝试过直接打印过滤器,而不是首先使用相同的结果制作一个列表。我的问题是,如何让'item'在与(* propertiestrue)的输出相同的行上打印?

1 个答案:

答案 0 :(得分:5)

from itertools import chain

nature = "cow"
creator = ""
minor = ""
item = "hammer"
NAMEPROPERTIES = [nature, creator, minor]
propertiestrue = filter(None, NAMEPROPERTIES)

print(*chain(propertiestrue , [item]))

cow hammer