【问题标题】:Python nested loop from files来自文件的 Python 嵌套循环
【发布时间】:2010-12-16 07:45:35
【问题描述】:

我有以下代码:

inputActionFile = '../action.txt'
inputDaerahFile = '../daerah.txt'
inputStuffFile = '../stuff.txt'
inputTermsFile = '../terms.txt'

outputFile = 'hasil.out'

inputAction = open(inputActionFile, 'r')
inputDaerah = open(inputDaerahFile, 'r')
inputStuff = open(inputStuffFile, 'r')
inputTerms = open(inputTermsFile, 'r')

output = open(outputFile, 'w')

for actionLine in inputAction:
 for daerahLine in inputDaerah:
  for stuffLine in inputStuff:
   for termsLine in inputTerms:
    keyword = actionLine.strip() + ' ' + daerahLine.strip() + ' ' + stuffLine.strip() + ' ' + termsLine
    output.write(keyword)

inputAction.close()
inputDaerah.close()
inputStuff.close()
inputTerms.close()
output.close()

我希望结果会遍历所有这些文件并将它们一一嵌套到输出文件中。但是,它只是迭代第四个循环。我在 BaSH 中做了类似的事情,想看看如何在 Python 中做到这一点。 BaSH代码如下:

#!/bin/sh
input1=$1
input2=$2
input3=$3
input4=$4
output=$5

echo "###START###" > $output
#old_IFS=$IFS
IFS='
'  # new field separator, EOL

for line1 in `cat $input1`;
do
 for line2 in `cat $input2`;
 do
  for line3 in `cat $input3`;
  do
   for line4 in `cat $input4`;
   do
    echo $line1 $line2 $line3 $line4 >> $output;
   done
  done
 done
done

unset IFS;
#IFS=$old_IFS

【问题讨论】:

  • IMO 它记下第一个文件 X2*X3*X4(每个文件中的行数)次的第一行。是你想要的吗?或者也许我错了,如果是这样,请纠正我。

标签: python bash nested-loops


【解决方案1】:

每个循环只会遍历文件一次。成功循环后

for termsLine in inputTerms:

一次,每次到达那里,它都会跳过这个循环,因为你已经到达了 inputTerms 文件的末尾。

您需要在每个循环中重新打开每个文件,(或至少对其进行 seek(0)),或将文件读入内存中的列表。

所以,要么:

inputAction = open(inputActionFile, 'r').readlines()
inputDaerah = open(inputDaerahFile, 'r').readlines()
inputStuff = open(inputStuffFile, 'r').readlines()
inputTerms = open(inputTermsFile, 'r').readlines()

或者:

for actionLine in open(inputActionFile, 'r'):
 for daerahLine in open(inputDaerahFile, 'r'):
  for stuffLine in open(inputStuffFile, 'r'):
   for termsLine in open(inputTermsFile, 'r'):
       etc....

【讨论】:

    【解决方案2】:

    试试:

    inputAction = open(inputActionFile, 'r').readlines()
    inputDaerah = open(inputDaerahFile, 'r').readlines()
    inputStuff = open(inputStuffFile, 'r').readlines()
    inputTerms = open(inputTermsFile, 'r').readlines()
    

    【讨论】:

    • 天哪。你们这么快,这么有帮助!我今天刚刚学习了 Python,发现在 Python 中任务需要 0.52 秒,而在 bash 中需要 14 秒,而带有 for 循环的 BaSH 需要 1:48。这是创建包含 250000 行的文件。蟒蛇摇滚!
    【解决方案3】:

    这是您的 Bash 版本,进行了一些可能会加快速度的更改(以及一些其他更改)。

    #!/bin/bash
    # you had sh, but your question tag says "bash"
    # if you really need Bourne shell portability, you should have tagged your
    # question "sh" and not "bash"
    
    input1=$1
    input2=$2
    input3=$3
    input4=$4
    output=$5
    
    echo "###START###" > $output
    #old_IFS=$IFS
    
    IFS=$'\n'  # new field separator, EOL
    
    while read -r line1
    do
        while read -r line2
        do
            while read -r line3
            do
                echo "$line1 $line2 $line3" | cat - "$input4"
            done < "$input3"
        done < "$input2"
    done < "$input1"   >> "$output"
    

    通过消除内部for 循环,这可能比您的版本快很多,具体取决于input4 文件的大小。最后保存文件写入可能会带来额外的速度优势。

    你可以做while IFS=$'\n' read -r var,你不需要保存和恢复IFS的值(如果有必要这样做的话),但是通过设置一次IFS可以节省一些重复的方式你在原版中所做的(我已经在我的修订版中复制了)。

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 2016-11-15
      • 2021-12-29
      • 1970-01-01
      • 2010-12-21
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多