好的,在 Python 中我有列表:
flowers = ["rose", "bougainvillea", "yucca", "marigold", "daylilly", "lilley of the valley"]
现在,我只想将列表 flowers 的最后一个对象分配给一个名为 poisonous 的新列表。
我试过:
poisonous=flowers[-1]
然而,此语句使字符串而不是列表成为有毒的。
请您参考如下方法:
>>> poisonous=[flowers[-1],] #take the last element and put it in a list
>>> poisonous
['lilley of the valley']
>>> poisonous=flowers[-1] #take the last element, which is a string
>>> poisonous
'lilley of the valley'
>>> poisonous=flowers[-1:] #take a slice of the original list. The slice is also a list.
>>> poisonous
['lilley of the valley']
