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

答 1 个回答|208 次阅读
Coco老师 - 官方公众号:青少儿编程学习网
擅长:编程教育网站:https://kidscodes.cn/

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']

推荐课程 »更多

    推荐知识

    Python精选库大全,青少年Python编程学习总结

    Python最适合青少儿进阶学习的编程语言