创建了一个名为flowers
的列表
>>> flowers = ['rose','bougainvillea','yucca','marigold','daylilly','lilly of the valley']
接着,
我需要将列表flowers
中前三个对象构成的子列表分配给列表thorny
。
这是我尝试的方法:
>>> thorny = []
>>> thorny = flowers[1-3]
>>> thorny
'daylilly'
>>> thorny = flowers[0-2]
>>> thorny
'daylilly'
>>> flowers[0,1,2]
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
flowers[0,1,2]
TypeError: list indices must be integers, not tuple
>>> thorny = [flowers[0] + ' ,' + flowers[1] + ' ,' + flowers[2]]
>>> thorny
['rose ,bougainvillea ,yucca']
如何仅获取列表flowers的前3个对象,同时保持列表内嵌列表的外观?