lis = [[('iphone', 'ITEM'),
('69', 'X'),
('pixel', 'ITEM'),
('45.91', 'X')], [('xbox', 'ITEM'),
('8989', 'X'),
('ps4', 'ITEM'),
('211.91', 'X')]]
print(id(lis),[id(z) for z in lis])
# creating a new list by list comprehension instruction,
# then assigning the new list to the same identifier lis :
# address of lis is modified
lis = [[(a,b),(c,d)] for (a,_),(b,_),(c,_),(d,_) in lis]
print("lis =",lis)
print(id(lis),[id(z) for z in lis])
。
del lis
las = [[('iphone', 'ITEM'),
('69', 'X'),
('pixel', 'ITEM'),
('45.91', 'X')], [('xbox', 'ITEM'),
('8989', 'X'),
('ps4', 'ITEM'),
('211.91', 'X')]]
print(id(las),[id(z) for z in las])
# modifiying in-place the list (keeps the same address),
# but addresses of the elements of the list are changed
las[:] = [[(a,b),(c,d)] for (a,_),(b,_),(c,_),(d,_) in las]
print("las =",las)
print(id(las),[id(z) for z in las])
。
del las
lus = [[('iphone', 'ITEM'),
('69', 'X'),
('pixel', 'ITEM'),
('45.91', 'X')], [('xbox', 'ITEM'),
('8989', 'X'),
('ps4', 'ITEM'),
('211.91', 'X')]]
print(id(lus),[id(z) for z in lus])
# adresses of the list and its elements remain the same ones
[ H.extend(((H.pop(0)[0],H.pop(0)[0]),(H.pop(0)[0],H.pop(0)[0])))
for H in lus]
print("lus =",lus)
print(id(lus),[id(z) for z in lus])
結果
56212872 [55694088, 18466760]
lis = [[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]]
55630088 [55693832, 55628552]
======================
55628552 [55630088, 55693832]
las = [[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]]
55628552 [55694280, 56212872]
======================
56212872 [55628552, 55694280]
lus = [[('iphone', '69'), ('pixel', '45.91')], [('xbox', '8989'), ('ps4', '211.91')]]
56212872 [55628552, 55694280]
如果這幫助下,你可以打 「正確的」。謝謝! –
是的,這實際上是非常有幫助的。等一下! –