从两个列表创建目录

2024-09-28 05:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下namedtupleList

from collections import namedtuple

firefoxprofile = namedtuple("Profile", ["Name", "Path", "isRelative", "Default"])
jb = firefoxprofile(Name='Jason', Path='Profiles/er5rtak4.Jason', isRelative='1', Default=None)
sr = firefoxprofile(Name='Sarah', Path='Profiles/23mvfqcj.Sarah', isRelative='1', Default=None)

files = ["places.sqlite","key4.db", "logins.json"]

firefoxlisttuple = []
firefoxlisttuple.append(jb)
firefoxlisttuple.append(sr)

我使用嵌套for循环来创建filesList中每个文件的路径。你知道吗

示例:

for profile in firefoxlisttuple:
    for file in files:
        print("{0}\{1}".format(profile.Path,file))

输出:

Profiles/er5rtak4.Jason/places.sqlite
Profiles/er5rtak4.Jason/key4.db
Profiles/er5rtak4.Jason/logins.json
Profiles/23mvfqcj.Sarah/places.sqlite
Profiles/23mvfqcj.Sarah/key4.db
Profiles/23mvfqcj.Sarah/logins.json

我知道嵌套的for循环在preface方面不是一个好主意。我应该做些什么来获得相同的输出?你知道吗

我查看了以下链接:

Iterate over two lists with different lengths

Python merging two lists with all possible permutations

但我不确定这是不是正确的方法。permutations是这个任务的合适工具吗?你知道吗


Tags: pathnamedefaultforsqliteprofilesnamedtupleplaces
1条回答
网友
1楼 · 发布于 2024-09-28 05:19:39

即使没有2个显式for循环,任何解决方案(无论是置换还是其他)都将导致隐式在引擎盖下的两个列表上循环。你知道吗

由于您询问的是代码的性能,因此有两个角度:

  • Big-O-这是关于一个非常大的输入数的解决方案的类
  • 评测-这里的^{}库是在python(check here for usage)中度量它的正确方法。你知道吗

您的解决方案是(n*m)在两个列表长度(nm)的函数中的Big-O表示法。这并不妨碍它在两个列表都很短(在您的示例中它们很短)并且nm都很小的情况下获得良好的性能。你知道吗

相关问题 更多 >

    热门问题