我現在有這樣的代碼:分離一個元組並將其保存到一個文件
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)
輸出:
[(1, 4, 3), (3, 7, 3), (7, 4, 1)]
目前我想保存到3個文件。 D://a.txt
,D://b.txt
和D://c.txt
在D://a.txt
我要救:
1
3
7
在D://b.txt
我要救:
4
7
4
而在D://c.txt
我想保存:
3
3
1
我知道如何保存:
with open("D:\\a.txt", "a") as myfile1:
myfile1.write(num1)
myfile1.write("\n")
或者與b.txt:
with open("D:\\b.txt", "a") as myfile2:
myfile2.write(num2)
myfile2.write("\n")
在這種情況下NUM1和NUM2使用,如:
for num1 in a:
myfile1.write(num1)
myfile1.write("\n")
我的目標是將號碼存入a.txt
,b.txt
和c.txt
而不是任何(
)
[
]
,
請描述其輸出的數字應選擇更詳細的,因爲我沒有得到有關任何暗示的標準。 – albert
@albert它只是索引0去索引1去b和索引2去c – SirParselot