简体   繁体   English

在文件中逐行打印项目列表

[英]Print list of items line by line in a file

Is it possible to Print the below Result line by line?是否可以逐行打印以下结果?

I tried to add \n on print(final \n, file=f) .我试图在print(final \n, file=f)上添加 \n 。 But that's not working.但这行不通。

Here is the Code:这是代码:

def calcperm(arr, size):
    result = set([()])
    for dummy_idx in range(size):
        temp = set()
        for dummy_lst in result:
            for dummy_outcome in arr:
                if dummy_outcome not in dummy_lst:
                    new_seq = list(dummy_lst)
                    new_seq.append(dummy_outcome)
                    temp.add(tuple(new_seq))
        result = temp
    return result

lst = [1, 2, 3, 4]
seq = 3
final = calcperm(lst, seq)
print(len(final))
with open('file.txt', 'w') as f:
    print(final, file=f)

Replace your last two lines (the with structure) with this:用以下代码替换最后两行( with结构):

with open('file.txt', 'w') as f:
    for item in final:
        print(item, file=f)

This will print all 24 tuples in final to your file, line by line.这会将final的所有 24 个元组逐行打印到您的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM