Discuss / Python / 求教如何对二维甚至多维列表进行增、删、改操作?

求教如何对二维甚至多维列表进行增、删、改操作?

Topic source

例如:在子列表 ['Apple', 'Google', 'Microsoft']中Google前面插入Facebook。网上没找到有用信息,求教各位。 如下代码会报错:list index out of range.

-- coding:utf-8 --

L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python', 'Ruby', 'PHP'], ['Adam', 'Bart', 'Lisa'] ] L.insert([0][1],'Facebook') print(L)

L = [ ['Apple', 'Google', 'Microsoft'], ['Java', 'Python', 'Ruby', 'PHP'], ['Adam', 'Bart', 'Lisa'] ] L[0] = ['Apple', 'Google', 'Microsoft'] L[0].insert(1, 'Facebook') print (L)

感谢,这样确实可以实现,只是多了一步有点麻烦,对于多维列表,可能要增加代码量

略略_略l

#4 Created at ... [Delete] [Delete and Lock User]

取决于insert之前的表达,不用多步骤

L = [ ['Apple', 'Google', 'Microsoft'] ['Java', 'Python', 'Ruby', 'PHP'] ['Adam', 'Bart', 'Lisa'] ]

L[0].insert(1,'Facebook')

print(L)

谢谢,理解了


  • 1

Reply