我正在编写一个处理文件中字符串的程序。我只想将SUB =X'1D'
上方的文字(字符串,例如=X'1D' BYTE X'1D'
组合成' LTORG'
)添加到我的测试文件中。
问题是我收集了每个LTORG上方的文字作为列表,并将它们作为列表插入。我想一次插入一个文字。
我的输出是:
[' START 100', " SUB =X'1D'", ' LTORG', '["=X\'1D\' BYTE X\'1D\'"]', ' RESW
20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", '
LTORG', '["=X\'0259\' BYTE X\'0259\'", "=C\'12345\' BYTE C\'12345\'",
"=X\'4356\' BYTE X\'4356\'", "=X\'69\' BYTE X\'69\'"]', " ADD =C'05'", '
END EXA']
def handle_LTORG(self, testfile):
myfile.testfile = testfile
for index, line in enumerate(myfile.testfile):
line = line.split(" ", 3)
if len(line) > 2:
if line[2].startswith("=X") or line[2].startswith("=C"):
raw_literal = line[2]
instruction = 'BYTE'
operand = line[2][1:]
literal = [raw_literal, instruction, operand]
literal = ' '.join(literal)
myfile.literals.append(literal)
if line[1] == 'LTORG':
if myfile.literals is not None:
myfile.testfile.insert(index + 1, str(myfile.literals))
myfile.literals.pop(0)
倒数第二行主要产生此问题。它将收集到的文字添加到列表中,并将其作为打包列表插入,而不是每行一个字符串。
我希望它看起来像这样:
[' START 100', " SUB =X'1D'", ' LTORG', '"=X'1D' BYTE X'1D'"', ' RESW 20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", ' LTORG', '"=X'0259' BYTE X'0259'", "=C'12345' BYTE C'12345'", "=X'4356' BYTE X'4356'", "=X'69' BYTE X'69'", " ADD =C'05'", ' END EXA']
答案 0 :(得分:1)
我会在How to make a flat list out of list of lists?
中尝试使用最常用的评论list = ['whatever',['1','2','3'],'3er']
flat_list = []
for member in list:
if type(member) is list:
for item in member:
flat_list.append(item)
else:
flat_list.append(member)
答案 1 :(得分:1)
您要执行的操作是两个操作的组合:
首先,您需要将字符串文字中的所有列表转换为
literal_eval
模块中的ast
使用实际列表。
然后,您需要整理列表。
下面是使过程降级的代码:
AlertDialog.Builder builder = new AlertDialog.Builder(getMyActivity());
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setLayout(MATCH_PARENT, WRAP_CONTENT);
dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
dialog.getWindow().setGravity(Gravity.BOTTOM);
wmlp.gravity = Gravity.BOTTOM ;
dialog.show();
输出:
from ast import literal_eval
inlist = [' START 100', " SUB =X'1D'", ' LTORG', '["=X\'1D\' BYTE X\'1D\'"]', ' RESW 20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", ' LTORG', '["=X\'0259\' BYTE X\'0259\'", "=C\'12345\' BYTE C\'12345\'", "=X\'4356\' BYTE X\'4356\'", "=X\'69\' BYTE X\'69\'"]', " ADD =C'05'", ' END EXA']
inlist = [literal_eval(elem) if elem[0] == '[' and elem[-1] == ']' else elem for elem in inlist]
outlist = []
for elem in inlist:
if isinstance(elem,list):
for item in elem:
outlist.append(item)
else:
outlist.append(elem)
print(outlist)
答案 2 :(得分:0)
更新:解决了while循环的问题。随时发布建议!
def handle_LTORG(self, testfile):
myfile.testfile = testfile
for index, line in enumerate(myfile.testfile):
line = line.split(" ", 3)
if len(line) > 2:
if line[2].startswith("=X") or line[2].startswith("=C"):
raw_literal = line[2]
instruction = 'BYTE'
operand = line[2][1:]
literal = [raw_literal, instruction, operand]
literal = ' '.join(literal)
myfile.literals.append(literal)
if line[1] == 'LTORG':
if myfile.literals:
i = 'hi'
while len(i) > 0:
i = myfile.literals[-1]
myfile.testfile.insert(index+1, str(i))
myfile.literals.pop()
if len(myfile.literals) == 0:
break
return myfile.testfile