我有一个文本文件t1.txt
:
1¶
2¶
3
我有一个文本文件t2.txt
:
»1¶
»2¶
»3
其中»
和¶
分别代表制表符和换行符。
我想将这两者结合起来并生成所有可能的组合:
11¶
12¶
13¶
21¶
22¶
23¶
31¶
32¶
33¶
这是我的代码:
out = 'out.txt'
in1 = 't1.txt'
in2 = 't2.txt'
outFile = open(out,'w')
with open(in1, 'r') as f:
for line1 in f:
for line2 in open(in2, 'r'):
outFile.write(line1+line2)
outFile.close()
但我得到的输出是:
1¶
»1¶
1¶
»2¶
1¶
»32¶
»1¶
2¶
»2¶
2¶
»33»1¶
3»2¶
3»3
我不明白为什么 有人可以帮忙吗?
答案 0 :(得分:2)
你想要product:
private void helmet_MouseDown1(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void helmet_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
}
private void armature_MouseDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void armature_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
}
private void weapons_MouseDown(object sender, MouseButtonEventArgs e)
{
_startPoint = e.GetPosition(null);
}
private void weapons_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point mousePos = e.GetPosition(null);
Vector diff = _startPoint - mousePos;
if (e.LeftButton == MouseButtonState.Pressed &&
(Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
{
var listBox = sender as ListBox;
var listBoxItem = listBox.SelectedItem;
DataObject dragData = new DataObject(_dropIdentifier, listBoxItem);
DragDrop.DoDragDrop(listBox, dragData, DragDropEffects.Move);
}
}
所以对于你的文件:
f1,f2 = "123","123"
from itertools import product
print(list(product(*(f1, f2))))
哪个会给你:
with open("a.txt") as f1, open("b.txt") as f2:
print(list(product(*(map(str.rstrip,f1), map(str.rstrip,f2)))))
加入:
[('1', '1'), ('1', '2'), ('1', '3'), ('2', '1'), ('2', '2'), ('2', '3'), ('3', '1'), ('3', '2'), ('3', '3')]
要写入您的文件::
print(list(map("".join, product(*(map(str.rstrip,f1), map(str.rstrip,f2))))))
['11', '12', '13', '21', '22', '23', '31', '32', '33']
输出:
with open("a.txt") as f1, open("b.txt") as f2, open("out.txt", "w") as out:
for p in product(*(map(str.rstrip,f1), map(str.rstrip, f2))):
out.write("".join(p) + "\n")
对于python2使用11
12
13
21
22
23
31
32
33
:
itertools.imap
答案 1 :(得分:1)
您的行包含换行符和空格。这些在一行的末尾不可见。
您必须清除这些字符:
out = 'out.txt'
in1 = 't1.txt'
in2 = 't2.txt'
with open(in2, 'r') as f:
lines2 = [l.rstrip() for l in f]
with open(out,'w') as outFile:
with open(in1, 'r') as f:
for line1 in f:
line1 = line1.rstrip()
for line2 in lines2:
outFile.write(line1+line2+'\n')
答案 2 :(得分:1)
您的文件中有空格和返回。使用strip()
修剪它们 out = 'out.txt'
in1 = 't1.txt'
in2 = 't2.txt'
outFile = open(out,'w')
with open(in1, 'r') as f:
for line1 in f:
for line2 in open(in2, 'r'):
outFile.write(line1.strip()+line2.strip()+"\n")
outFile.close()