Basic Python List Excercise -
how make graph print vertically rather horizontally? please try use same concept(loops, lists) used in code understand changes. thank help!
here code:
sign='x' test_list=[500000,5000000,7000000] test_calc_list=[] test_sum=sum(test_list) test_string_list=['test1','test2','test3'] signs_list=[] x in test_list: test_calc=round((x/float(test_sum)*10)) test_calc_list.append(test_calc) y in test_calc_list: y=int(y) signs=y*sign signs_list.append(signs) z in range(len(test_string_list)): print "%8s: %-6s %7i"% (test_string_list[z],signs_list[z],test_list[z])
this output get:
test1: 500000 test2: xxxx 5000000 test3: xxxxxx 7000000
this output want:
x x x x x x x x x test1 test2 test3 500000 5000000 7000000
assuming isn't homework (you know there software detect plagiarism?) here possible solutions.
easiest assume list fixed size:
for in range(len(max(signs_list)), 0, -1): print ('{0:<8} {1:<8} {2}').format( 'x' if len(signs_list[0]) >= else '', 'x' if len(signs_list[1]) >= else '', 'x' if len(signs_list[2]) >= 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 dealing lists of arbitrary size requires eval
for in range(len(max(signs_list)), 0, -1): template_string = "" input_string = "" 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) + "]) >= else ''," input_string += input_part statement = "('" + template_string + "')" statement += ".format" statement += "(" + input_string[:-1] + ")" print eval(statement) template_string = "" input_string = "" 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 = "" 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)
the above monstrosity can refactored remove statement construction:
def statement_constructor(list_type, list_size): template_string = "" input_string = "" 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) + "]) >= 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) 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))
all 3 produce same output:
x x x x x x x x x x 500000 5000000 7000000 test1 test2 test3
although solution using eval
handle test4, test5, , on. there may more elegant solution, works.
edit: or more elegant solution without eval
overkill suggested in comment michael0x2a.
def statement_constructor(list_type, list_size): template_string = "" input_string = "" array = [] 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]) >= 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) in range(len(max(signs_list)), 0, -1): statement_constructor('signs_list', length) statement_constructor('test_list', length) statement_constructor('test_string_list', length)
Comments
Post a Comment