关于Python列表resu的困惑

2024-04-24 22:04:03 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
2条回答

你应该复习一下slicing notation。简而言之,您的第一个操作创建了一个列表。第二个操作在索引1处插入了一个元组。第三个操作用元组的内容替换列表片,而第四个操作用另一个列表的内容替换列表片。你知道吗

a=[1,2,3]
print a
a[1]=10,20,30  # add a tuple at position 1
print a  
a[1:2]=10,20,30 #replaces tuple and inserts 10,20,30 at position 1
print a  
a[1:2]=[10,20,30] #now a[1:2] is 10 so this replaces 10 and inserts 10,20,30 at position 1 
print a 

输出

[1, 2, 3]
[1, (10, 20, 30), 3]
[1, 10, 20, 30, 3]
[1, 10, 20, 30, 20, 30, 3]

注意:结尾永远不会有30。 希望这有帮助。你知道吗

相关问题 更多 >