gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:08 24 4
gpt4 key购买 nike

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

关于python - 将另一个列表的最后一个对象分配给新列表 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com