0

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

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

我尝试直接打印过滤器,而不是先用相同的结果列出它。我的问题是,如何让“项目”与 (*propertiestrue) 的输出在同一行上打印?

4

1 回答 1

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
于 2013-04-30T11:03:43.180 回答