Python如何修改列表中指定的项?

1 个回答|74 次阅读
Coco老师 - 科普专家
擅长:编程教育

1、修改某一项

更改第二项:

fruitlist = ["apple", "banana", "orange"]
fruitlist[1] = "blackcurrant"
print(fruitlist)

执行结果:

['apple', 'blackcurrant', 'orange']


2、修改某个范围内的多项

fruitlist = ["apple", "banana", "cherry", "orange", "mango"]
fruitlist[1:3] = ["blackcurrant", "watermelon"]
print(fruitlist)

执行结果:

['apple', 'blackcurrant', 'watermelon', 'orange', 'mango']