1
我有下面的代碼,它不會進入循環,我不明白爲什麼。迭代超出範圍(0,len(temp), - 1)不起作用
python file.py 1111100000000
代碼:
import argparse
p = argparse.ArgumentParser()
p.add_argument("bits", help = "array_of_bits")
args = p.parse_args()
bits = args.bits
temp = []
for i in bits:
temp.append([int(i)])
print (temp)
fin = []
j = 0
for i in range(0,len(temp),(-1)):
if ((temp[i] == 0)):
fin.extend(temp[j].append(temp[i]))
if (len(temp[i]) != 1):
fin.extend(temp[j].append(temp[i]))
j = j + 1
print (fin)
我想要得到的是:
[[1], [1], [1], [1], [1], [0], [0], [0], [0], [0], [0], [0], [0]]
[[10010], [10010], [100]
而是我得到這個:
[[1], [1], [1], [1], [1], [0], [0], [0], [0], [0], [0], [0], [0]]
[[]]
爲什麼你將'(-1)'傳遞給'range'? –
你的代碼相當混亂。您的縮進已關閉,您正在將'extend'與'append'混合在一起,您的'for i in range'將無法工作。 –