How do I print this list vertically?

后端 未结 5 1483
猫巷女王i
猫巷女王i 2020-12-11 19:21

Let\'s say I have this list of asterisks, and I say it to print this way:

list = [\'* *\', \'*\', \'* * *\', \'* * * * *\', \'* * * * * *\', \'* * * *\']
for         


        
5条回答
  •  天涯浪人
    2020-12-11 19:49

    How about this, for a version that mostly uses basic Python operations:

    data = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
    max_len = max(len(x) for x in data)  # find the longest string
    
    for i in range(0, max_len, 2):       # iterate on even numbered indexes (to get the *'s)
        for column in data:              # iterate over the list of strings
            if i < len(column):
                print column[i],         # the comma means no newline will be printed
            else:
                print " ",               # put spaces in for missing values
        print                            # print a newline at the end of each row
    

    Example output:

    * * * * * *
    *   * * * *
        * * * *
          * * *
          * *  
            *  
    

提交回复
热议问题