1
我試圖插入一個元素到下面的列表(c5)並獲得新的列表(c6)當列表中的連續元素不相同,我已經嘗試下面的腳本。這種插入以某種方式帶我到無限循環,我不得不手動殺死程序來停止程序。任何人都可以幫助我理解Python中這種奇怪的行爲?python插入元素到列表奇怪的行爲的無限循環
預先感謝
c5=['03-03-2017 00:00', '03-03-2017 00:00', '03-03-2017 00:00', '03-03-2017 00:00', '03-03-2017 00:00', '04-03-2017 00:00', '04-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '06-03-2017 00:00', '07-03-2017 00:00', '07-03-2017 00:00', '07-03-2017 00:00', '07-03-2017 00:00', '07-03-2017 00:00', '07-03-2017 00:00', '07-03-2017 00:00', '08-03-2017 00:00', '08-03-2017 00:00', '08-03-2017 00:00', '09-03-2017 00:00', '09-03-2017 00:00', '09-03-2017 00:00', '09-03-2017 00:00', '09-03-2017 00:00', '09-03-2017 00:00', '10-03-2017 00:00']
c6=c5
x=0
for d in c5:
if(x<(len(c5)-1)):
if(d != c5[x+1]):
c6.insert(x+1,'Hurray')
print(x)
x+=1
print(c6)
'c6 = c5'不能複製。請參閱https://nedbatchelder.com/text/names.html – user2357112
[如何克隆或複製列表?]的可能重複(http://stackoverflow.com/questions/2612802/how-to-clone-or-copy -a-list) – TemporalWolf