假设这不是家庭作业(你知道有软件可以检测抄袭吗?)这里有可能的解决方案。
最简单的方法是假设列表是固定大小的:
for i in range(len(max(signs_list)), 0, -1):
print ('{0:<8} {1:<8} {2}').format(
'x' if len(signs_list[0]) >= i else '',
'x' if len(signs_list[1]) >= i else '',
'x' if len(signs_list[2]) >= i else '')
print ('{0:<8} {1:<8} {2}').format(test_string_list[0], test_string_list[1], test_string_list[2])
print ('{0:<8} {1:<8} {2}').format(test_list[0], test_list[1], test_list[2])
Trickier 正在处理任意大小的列表,这需要eval
for i in range(len(max(signs_list)), 0, -1):
template_string = ""
input_string = ""
for z in range(len(test_string_list)):
string_part = "{" + str(z) + ":<8} "
template_string += string_part
input_part = "'x' if len(signs_list[" \
+ str(z) + "]) >= i else '',"
input_string += input_part
statement = "('" + template_string + "')"
statement += ".format"
statement += "(" + input_string[:-1] + ")"
print eval(statement)
template_string = ""
input_string = ""
for z in range(len(test_string_list)):
string_part = "{" + str(z) + ":<8} "
template_string += string_part
input_part = "test_list[" + str(z) + "],"
input_string += input_part
statement = "('" + template_string + "')"
statement += ".format"
statement += "(" + input_string[:-1] + ")"
print eval(statement)
template_string = ""
input_string = ""
for z in range(len(test_string_list)):
string_part = "{" + str(z) + ":<8} "
template_string += string_part
input_part = "test_string_list[" + str(z) + "],"
input_string += input_part
statement = "('" + template_string + "')"
statement += ".format"
statement += "(" + input_string[:-1] + ")"
print eval(statement)
可以重构上述怪物以删除语句构造:
def statement_constructor(list_type, list_size):
template_string = ""
input_string = ""
for z in range(list_size):
string_part = "{" + str(z) + ":<8} "
template_string += string_part
if list_type == "signs_list":
input_part = "'x' if len(signs_list[" \
+ str(z) + "]) >= i else '',"
else:
input_part = list_type + "[" + str(z) + "],"
input_string += input_part
statement = "('" + template_string + "')"
statement += ".format"
statement += "(" + input_string[:-1] + ")"
return statement
length = len(test_string_list)
for i in range(len(max(signs_list)), 0, -1):
print eval(statement_constructor('signs_list', length))
print eval(statement_constructor('test_list', length))
print eval(statement_constructor('test_string_list', length))
所有三个产生相同的输出:
x
x
x x
x x
x x
x x
500000 5000000 7000000
Test1 Test2 Test3
虽然使用的解决方案eval
可以处理 Test4、Test5 等。可能有一个更优雅的解决方案,但这有效。
eval
编辑:或者在Michael0x2a 的评论中建议的更优雅的解决方案,没有过度杀伤力。
def statement_constructor(list_type, list_size):
template_string = ""
input_string = ""
array = []
for z in range(list_size):
string_part = "{:<8}"
template_string += string_part
if list_type == "signs_list":
array.append('x' if len(signs_list[z]) >= i else '')
elif list_type == 'test_list':
array.append(test_list[z])
else:
array.append(test_string_list[z])
print (template_string).format(*array)
length = len(test_string_list)
for i in range(len(max(signs_list)), 0, -1):
statement_constructor('signs_list', length)
statement_constructor('test_list', length)
statement_constructor('test_string_list', length)