如何组合两个for循环

时间:2017-04-03 05:34:37

标签: python

我的代码下面有两个for循环。第一个将循环通过目录pannam,其中有一些wav文件。

print (f_name)

将产生以下结果:

a.wav
b.wav
c.wav
d.wav

代码将打印所有wav文件名

然后还有另一个循环。这将读取我的文本文件 corpus_text

的行
print (line_strip)

会给出这些结果

Name of first file
Name of second file
Name of Third file
Name of Fourth file

我正在尝试将这两个结果组合成这样的

 Name of first file is a
 Name of second file is b
 Name of Third file is c
 Name of Fourth file is d

这是我的代码,它运行无限循环。我猜它是关于我无法理解的嵌入式循环。

  import os


    rootdir = r'C:\Users\PANNAM\Desktop\Final_Earthquake\pannam'

    for dirpath, dirnames, filenames in os.walk(rootdir):

        for file in filenames:
         filepath = dirpath +os.sep+file
         if filepath.endswith('wav'):
             split_dirpath = dirpath.split(os.sep)
             f_name, f_ext = (os.path.splitext(file))

             print (f_name)

             with open('test_corpus.txt', mode = 'r+', encoding="utf=8") as f:

              for line in f:
                  if line.rstrip():
                      line_strip = line.strip()
                      print(line_strip)

1 个答案:

答案 0 :(得分:0)

现在我明白你想要什么,但我不确定我理解你为什么要这样做。 无论如何,根据您所需的输出,您根本不需要任何嵌入式循环。 你应该使用其中之一 zip, 或者可能 itertools.zip_longest

下面,我假设您已经拥有提供输入字符串的函数或生成器---文件名和文本行。 我在下面对它们进行了硬编码,因此我可以专注于有趣的部分。

import itertools
import os.path


def output(prefix, filename):
    stem, __ = os.path.splitext(filename)
    return prefix + ' is ' + stem + '.'


def main():
    prefixes = ['Name of first file', 'Name of second file']
    filenames = ['a.wav', 'b.wav', 'c.wav', 'd.wav']

    print('zip:')
    for prefix, filename in zip(prefixes, filenames):
        print(output(prefix, filename))
    print()

    print('itertools.zip_longest:')
    for prefix, filename in itertools.zip_longest(
      prefixes,
      filenames,
      fillvalue='[unknown file]'
      ):
        print(output(prefix, filename))
    return


if "__main__" == __name__:
    main()

输出:

zip:
Name of first file is a.
Name of second file is b.

itertools.zip_longest:
Name of first file is a.
Name of second file is b.
[unknown file] is c.
[unknown file] is d.

zipzip_longest之间的区别应该暗示为什么我无法弄清楚你的目标...... 您将文件名列表与文本文件的行组合在一起,其中任何一行都可能超过另一行。 只要其任何输入用完,zip就会停止; zip_longest将一直持续到所有用完为止。 为了确保合理的输出,您必须确保两个输入列表的长度相同,或者确定如果它们不是这样做的话。

更糟的是, os.walk 通过调用列出文件 os.listdir 要么 os.scandir, 这两者都不保证任何特定的顺序,这意味着您无法预测哪一个将与您的语料库文件中的哪一行配对。 解决此问题需要将文件名排序为可预测的顺序 (必须与语料库文件中的行匹配), 或以某种方式确定哪个文件与哪一行相关。