Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python打印列中的列表_Python - Fatal编程技术网

Python打印列中的列表

Python打印列中的列表,python,Python,如何从列中的列表中打印Python值 我把它们分类了,但我不知道如何用两种方式打印它们 例如: list=['apricot','banana','apple','car','coconut','baloon','bubble'] 第一个: apricot bubble ... apple car baloon coconut 第二种方式: apricot apple baloon bubble car

如何从列中的列表中打印Python值

我把它们分类了,但我不知道如何用两种方式打印它们 例如:

list=['apricot','banana','apple','car','coconut','baloon','bubble']
第一个:

apricot      bubble     ...
apple        car        
baloon       coconut
第二种方式:

apricot    apple     baloon   
bubble     car      coconut
我还想将所有内容与ljust/rjust对齐

我试过这样的方法:

print " ".join(word.ljust(8) for word in list)
但它的显示方式与第一个示例相同。我不知道这样做是否合适

the_list = ['apricot','banana','apple','car','coconut','baloon','bubble']
num_columns = 3

for count, item in enumerate(sorted(the_list), 1):
    print item.ljust(10),
    if count % num_columns == 0:
        print
输出:

apple      apricot    baloon    
banana     bubble     car       
coconut
更新: 下面是一个全面的解决方案,它解决了您给出的两个示例。我已经为此创建了一个函数,并对代码进行了注释,以便清楚地了解它正在做什么

def print_sorted_list(data, rows=0, columns=0, ljust=10):
    """
    Prints sorted item of the list data structure formated using
    the rows and columns parameters
    """

    if not data:
        return

    if rows:
        # column-wise sorting
        # we must know the number of rows to print on each column
        # before we print the next column. But since we cannot
        # move the cursor backwards (unless using ncurses library)
        # we have to know what each row with look like upfront
        # so we are basically printing the rows line by line instead
        # of printing column by column
        lines = {}
        for count, item in enumerate(sorted(data)):
            lines.setdefault(count % rows, []).append(item)
        for key, value in sorted(lines.items()):
            for item in value:
                print item.ljust(ljust),
            print
    elif columns:
        # row-wise sorting
        # we just need to know how many columns should a row have
        # before we print the next row on the next line.
        for count, item in enumerate(sorted(data), 1):
            print item.ljust(ljust),
            if count % columns == 0:
                print
    else:
        print sorted(data)  # the default print behaviour


if __name__ == '__main__':
    the_list = ['apricot','banana','apple','car','coconut','baloon','bubble']
    print_sorted_list(the_list)
    print_sorted_list(the_list, rows=3)
    print_sorted_list(the_list, columns=3)

没有内置的方法可以做到这一点,您必须自己编程。您试图解决这个问题吗?我尝试了类似这样的方法:print”“。join(word.ljust(8)表示列表中的word),但它只显示与第一个示例中相同的内容。我不知道这样做是否合适。你有什么要求?函数应该自己计算列数,还是作为函数的第二个参数?我不介意将其作为第二个参数。非常感谢:)你能告诉我,如果有一种方法可以像我在第一个例子中展示的那样打印出来?@bartekshadow我已经更新了我的答案,加入了一个同样适合你的第一个例子的解决方案。