我正在尝试将此数组拆分为RLE格式,这意味着我需要用逗号分隔数字和字母。有人有什么想法吗?我已经尝试过list.split,但是不能让它与数组一起工作
list.split 痛苦和痛苦
linesofRLE = raw_input("How Many Lines Of Code Do You Wish To enter:")
if linesofRLE < 2:
print 'sorry needs more than 2 lines'
break
else:
print 'ok'
linesofRLE = int(linesofRLE)
userRLE = []
for d in range (linesofRLE):
enterRLE = raw_input("enter your line of RLE")
brokenRLE = ([enterRLE[i:i+3] for i in range(0, len(enterRLE), 3)])
userRLE.append(brokenRLE)
print userRLE
userRLE = [userRLE]
print userRLE
此代码应输出如下数组:
[[[['09g']], [3], ['90h']], [3], ['79g']]
答案 0 :(得分:0)
尝试使用以下少量代码:
from itertools import groupby
def openingthefile():
myfile=open('LogoRLE.txt','r')
details=myfile.read()
myfile.close()
#print details
return details
def encode(input_string):
#print
return [(len(list(g)), k) for k,g in groupby(input_string)]
file_contents = openingthefile()
file_contents.split()
print file_contents
def decode(lst):
#print lst
return ''.join(c * n for n,c in lst)
res_array = encode(file_contents)
variable2=""
for i in res_array:
if i[1]=="\n":
variable2+= i[1]
else:
variable2+=str('%02d' % i[0])+i[1]
#print variable2
print "pppppppppppp"
#file_contents = openingthefile()
#print file_contents
res_string = decode(res_array)
#res_array = decode(file_contents)
print "pppppppppppp"
print res_string