1

如何使此图垂直而不是水平打印?请尝试使用我在代码中使用的相同概念(循环、列表),以便我了解更改。谢谢您的帮助!

这是代码:

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
4

1 回答 1

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 等。可能有一个更优雅的解决方案,但这有效。

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) 
于 2013-04-20T07:23:37.607 回答