简体   繁体   English

分隔元组并将其保存到文件

[英]Separating a tuple and saving it to a file

I currently have this code: 我目前有以下代码:

from collections import  OrderedDict
a = [(1), (7), (3), (1)]
b = [(2), (4), (7), (4)]
c = [(8), (1), (3), (3)]
d = (list(OrderedDict((sub[0], sub) for sub in sorted(zip(a, b, c))).values()))
print(d)

Output: 输出:

[(1, 4, 3), (3, 7, 3), (7, 4, 1)]

I am currently trying to save to 3 files. 我目前正在尝试保存到3个文件。 D://a.txt , D://b.txt and D://c.txt D://a.txt D://b.txtD://c.txt

In D://a.txt I want to save: 我要在D://a.txt中保存:

1
3
7

In D://b.txt I want to save: 我要在D://b.txt中保存:

4
7
4

And in D://c.txt I want to save: 然后在D://c.txt中保存:

3
3
1

I know how to save it: 我知道如何保存它:

with open("D:\\a.txt", "a") as myfile1:
   myfile1.write(num1)
   myfile1.write("\n")

Or with b.txt: 或使用b.txt:

with open("D:\\b.txt", "a") as myfile2:
   myfile2.write(num2)
   myfile2.write("\n")

with num1 and num2 in this case to be used like: 与num1和num2在这种情况下的用法如下:

for num1 in a:
   myfile1.write(num1)
   myfile1.write("\n")

My objective is to save the numbers in a.txt , b.txt and c.txt without any ( ) [ ] , 我的目标是将数字保存在a.txtb.txtc.txt而不包含任何( ) [ ] ,

>>> d
[(1, 4, 3), (3, 7, 3), (7, 4, 1)]
>>> with open('a.txt','w') as fa, open('b.txt','w') as fb, open('c.txt','w') as fc:
...     for a,b,c in d:
...         fa.write(str(a) + '\n')
...         fb.write(str(b) + '\n')
...         fc.write(str(c) + '\n')
...         
>>> !cat a.txt
1
3
7

Try this: 尝试这个:

letters = ("a", "b", "c")
for i, nums in enumerate(zip(d)):
    with open("D:\\{}.txt".format(letters[i]), "a") as myfile:
        myfile.write("\n".join(str(num) for num in nums))

Assuming the number of tuples as well as the number of entries in each tuple in d can vary: 假设元组的数量以及d中每个元组的条目数量可以变化:

for i, nums in enumerate(zip(*d)):
    fname = '/tmp/%s.txt' % chr(ord('a') + i)
    with open(fname, 'w') as f:
        f.write('\n'.join('%s' % n for n in nums))

This way you get exactly what you want: 通过这种方式,您可以确切地获得所需的内容:

!cat /tmp/a.txt
1
3
7
!cat /tmp/b.txt
4
7
4
!cat /tmp/c.txt
3
3
1

I'm going to start assuming that the code that gets the list of tuples works and does not need any adjusting. 我将开始假设获取元组列表的代码有效并且不需要任何调整。

since d is a list of tuples, I'm going to assign 3 variables, one to each tuple to make things simpler for now. 因为d是一个元组列表,所以我将分配3个变量,每个元组一个,以使事情现在更简单。

a = d[0] #(1,4,3)
b = d[1] #(3,7,3)
c = d[2] #(7,4,1)

Now, it's time to loop through them, and assign the values to the files. 现在,是时候遍历它们,并将值分配给文件了。

for x in a:
    with open("D:\\a.txt", "a") as file:
        file.write(x)
        file.write("\n")

for y in b:
    with open("D:\\b.txt", "a") as file:
        file.write(y)
        file.write("\n")

for z in c:
    with open("D:\\c.txt", "a") as file:
        file.write(z)
        file.write("\n")

Your output should be free of any type of brackets or parenthesis. 您的输出应没有任何类型的括号或括号。

While I'm not confident that you're doing what you are trying to do, your definition of d could be rewritten in a more simple fashion, and then e could be created to pair up the entries in each resultant tuple. 尽管我不确定您在做什么,但是可以以更简单的方式重写d的定义,然后可以创建e来对每个结果元组中的条目进行配对。

a = [(1), (7), (3), (1)]
b = [(2), (4), (7), (4)]
c = [(8), (1), (3), (3)]
d = sorted(zip(a, b, c))[1:] # [(1, 4, 3), (3, 7, 3), (7, 4, 1)]
e = list(zip(*d)) # [(1, 3, 7), (4, 7, 4), (3, 3, 1)]

Then, you could write to each file as: 然后,您可以按以下方式写入每个文件:

for name, values in zip(['a', 'b', 'c'], e):
    with open('D:\\{}.txt'.format(name), 'w') as myfile:
        myfile.write('\n'.join(map(str,values))

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

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