Weird list behavior in python -
qtd_packs = 2 size_pack = 16 pasta = [] pasta.append ('packs/krun/') pasta.append ('packs/parting2/') k in range(0, qtd_packs): n in range(1, size_pack+1): samples_in.append (pasta[k]+str(n)+'.wav') samples.append(samples_in) del samples_in[0:len(samples_in)] print(samples)
i'm trying add samples_in
inside samples
list, delete old samples_in
list create new one. happen 2 times, qtd_packs =2
. in end, 2 empty lists:
[[], []]
i've append
'ed samples_in
inside samples
before deleting it. happened?
thank you
in python, lists passed reference. when append samples_in
samples
, python appends reference samples_in
samples
. if want append copy of samples_in
samples
, can do:
samples.append(samples_in[:])
this creates new list items in samples_in
, passes new list samples.append()
. when clear items in samples_in
, you're not clearing items in list appended samples
well.
also, note samples_in[:]
equivalent samples_in[0:len(samples_in)]
.
Comments
Post a Comment