Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何组合两个文本文档_Python 2.7 - Fatal编程技术网

Python 2.7 如何组合两个文本文档

Python 2.7 如何组合两个文本文档,python-2.7,Python 2.7,你好,我一直找不到问题的确切答案 我需要以这种方式组合两个文本文档,请参见示例 文本文件1 糖, 咖啡 水,, 豪斯 文本文件2 阿苏卡尔, 咖啡馆 阿古阿, 卡萨 我需要像这样组合它,它需要在一个递减列表中 糖 阿苏卡尔 咖啡 咖啡馆 水 阿瓜 房子 卡萨 就这些。简单对吗 提前谢谢你。。。我已经查看了很多关于如何组合到列表的内容,但是没有一个完全像这样。如果您将文档中的单词分成两个数组(基于空格),那么您将能够同时循环两个数组,一个接一个地添加单词。例如: public String com

你好,我一直找不到问题的确切答案

我需要以这种方式组合两个文本文档,请参见示例

文本文件1

糖, 咖啡 水,, 豪斯

文本文件2

阿苏卡尔, 咖啡馆 阿古阿, 卡萨

我需要像这样组合它,它需要在一个递减列表中

糖 阿苏卡尔 咖啡 咖啡馆 水 阿瓜 房子 卡萨

就这些。简单对吗


提前谢谢你。。。我已经查看了很多关于如何组合到列表的内容,但是没有一个完全像这样。

如果您将文档中的单词分成两个数组(基于空格),那么您将能够同时循环两个数组,一个接一个地添加单词。例如:

public String combineDocuments(String[] firstDocument, String[] secondDocument) {
  StringBuilder newDocument = new StringBuilder();

  for (int i = 0; i < firstDocument.length && i < secondDocument.length; i++) {
    newDocument.append(firstDocument[i]);
    newDocument.append(" ");
    newDocument.append(secondDocument[i]);
  }
  return newDocument.toString();
}
公共字符串组合文档(字符串[]第一个文档,字符串[]第二个文档){
StringBuilder newDocument=新StringBuilder();
对于(int i=0;i
您可以打开文本文件,拆分它们(通过逗号、制表符或任何其他字符),然后将它们合并:

#read the text files
with open("data1.txt") as myfile:
   data1_txt="".join(line.rstrip() for line in myfile)
with open("data2.txt") as myfile:
   data2_txt="".join(line.rstrip() for line in myfile)

#get the data
data1=data1_txt.split(',')
data2=data2_txt.split(',')

#join the data
joined = [(data1[i],data2[i]) for i in range(len(data1)]

#now sort it
joinedSorted = sorted(joined,reverse=True)

你试了什么?你在这里称之为“降序”列表是什么?