adjList = [[1,2,3,7], [2,1,3,7], [3,1,2,4], [4,3,5,6], [5,4,6], [6,4,5], [7,1,2]]
adjListCopy= adjList[:]
v=int(raw_input("Enter the node v to be deleted along with its neighbourhood"))
copy = adjList[v-1]
print(copy)
del adjList[v-1]
print adjList
print adjListCopy
输出:
Enter the node v to be deleted along with its neighbourhood2
[2, 1, 3, 7]
[[1, 2, 3, 7], [3, 1, 2, 4], [4, 3, 5, 6], [5, 4, 6], [6, 4, 5], [7, 1, 2]]
[[1, 2, 3, 7], [2, 1, 3, 7], [3, 1, 2, 4], [4, 3, 5, 6], [5, 4, 6], [6, 4, 5], [7, 1, 2]]
print adjListCopy
for i in range(len(copy)):
print(copy[i])
for j in range(len(adjListCopy)):
if copy[i]==adjListCopy[j][0]:
print adjListCopy[j] ***************
print adjListCopyFor1
在我制作 ********* 的地方,我想写 del 而不是 print 来解决我的目的,但它会引发错误。
我现在在我的手机旁。但是我可以告诉您,在迭代或循环列表时不能删除索引。您可以获取copy.deepcopy
列表的副本 ( ),遍历副本并按索引从原始副本中删除。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句