在单个文件中随机播放文本文件列表的记录

sys*_*out 5 python algorithm file

我有一个文本文件列表file1.txt, file2.txt, file3.txt .. filen.txt,我需要随机创建一个单个大文件作为结果*.

要求:
1.给定文件的记录在被洗牌之前需要反转
2.给定文件的记录应该在目标文件中保持相反的顺序
3.我不知道需要多少个文件来洗牌所以代码应尽可能通用(例如,允许在列表中声明文件名)
4.文件可以有不同的大小

例:

File1.txt
---------
File1Record1
File1Record2
File1Record3
File1Record4

File2.txt
---------
File2Record1
File2Record2


File3.txt
---------
File3Record1
File3Record2
File3Record3
File3Record4
File3Record5
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的:

ResultFile.txt
--------------
File3Record5   -|
File2Record2    |
File1Record4    |
File3Record4   -|
File2Record1    |
File1Record3    |-->File3 records are shuffled with the other records and 
File3Record3   -|   are correctly "reversed" and they kept the correct 
File1Record2    |   ordering
File3Record2   -|
File1Record1    |
File3Record1   -|
Run Code Online (Sandbox Code Playgroud)

*我没疯; 我必须使用resultfile.txt作为输入导入这些文件(博客文章)

编辑:
结果可能有你想要的任何种类,完全或部分洗牌,均匀交错,没关系.重要的是第1点和第2点都很荣幸.

Obe*_*nne 2

那这个呢:

>>> l = [["1a","1b","1c","1d"], ["2a","2b"], ["3a","3b","3c","3d","3e"]]
>>> while l:
...     x = random.choice(l)
...     print x.pop(-1) 
...     if not x:
...         l.remove(x)

1d
1c
2b
3e
2a
3d
1b
3c
3b
3a
1a
Run Code Online (Sandbox Code Playgroud)

您可以通过各种方式对其进行优化,但这就是总体思路。如果您无法立即读取文件但由于内存限制而需要迭代它们,这也适用。在这种情况下

  • 从文件中读取一行而不是从列表中弹出
  • 检查 EOF 而不是空列表