如何使此图形垂直打印而不是水平打印?请尝试使用我在代码中使用的相同概念(循环,列表),以便了解更改。谢谢你的帮助!
以下是代码:
SIGN='x'
test_list=[500000,5000000,7000000]
test_calc_list=[]
test_sum=sum(test_list)
test_string_list=['Test1','Test2','Test3']
signs_list=[]
for x in test_list:
test_calc=round((x/float(test_sum)*10))
test_calc_list.append(test_calc)
for y in test_calc_list:
y=int(y)
signs=y*SIGN
signs_list.append(signs)
for z in range(len(test_string_list)):
print "%8s: %-6s %7i"% (test_string_list[z],signs_list[z],test_list[z])
这是我得到的输出:
Test1: 500000
Test2: xxxx 5000000
Test3: xxxxxx 7000000
这是我想要的输出:
x
x x
x x
x x
x x
Test1 Test2 Test3
500000 5000000 7000000
答案 0 :(得分:1)
假设这不是作业(你知道有软件来检测抄袭吗?)这里有可能的解决方案。
最简单的方法是假设列表是固定大小的:
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等。可能有更优雅的解决方案,但这很有效。
编辑:
或者在Michael0x2a的评论中提出没有eval
过度杀伤的更优雅的解决方案。
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)