在Python 3.3中将while循环转换为for循环

时间:2014-01-29 22:03:12

标签: python for-loop python-3.x while-loop

我正在尝试将此循环转换为for循环,但我不知道如何去做。

output = ''
i = 0
while i <len(MyArray):
    C = chr(int(MyArray[i],2))
    output = output + C
    i = i + 1

print(output)

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:3)

output = ''
for i in range(len(MyArray)):
    C = chr(int(MyArray[i],2))
    output = output + C

print(output)

output = ''
for element in MyArray:
    C = chr(int(element,2))
    output = output + C

print(output)

output = [chr(int(element,2)) for element in MyArray]
print ''.join(output)