为什么打印someArray.insert()显示为“none”,我该如何解决?

时间:2017-11-09 16:46:09

标签: python

为什么none会提供我的代码,我该如何解决?

我的代码是:

stores=["meijer", "walmart", "kroger", "trader joes", "dollar general"]
print str(stores.insert(3, "aldi"))+("\nAldi is now in the list:\n")+str((stores)) 

输出:

None
Aldi is now in the list:
['meijer', 'walmart', 'kroger', 'aldi', 'kohls', 'dollar general']

1 个答案:

答案 0 :(得分:1)

如果真的必须有一个单行,这可以用Python 2完成:

stores = ["meijer", "walmart", "kroger", "trader joes", "dollar general"]
print stores.insert(3, "aldi") or "\nAldi is now in the list:\n%s" % stores

<强>输出

Aldi is now in the list:
['meijer', 'walmart', 'kroger', 'aldi', 'trader joes', 'dollar general']

这是有效的,因为stores.insert(3, "aldi")返回NoneNone or thing评估为thing