Skip to main content
 首页 » 编程设计

python之将另一个列表的最后一个对象分配给新列表之Python

2025年05月04日16lvdongjie

好的,在 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']