【发布时间】:2018-12-01 10:56:31
【问题描述】:
我有以下namedtuple 和List:
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 循环来创建 files List 中每个文件的路径。
示例:
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 循环在性能方面不是一个好主意。我应该怎么做才能获得相同的输出?
我查看了这些链接:
Iterate over two lists with different lengths
Python merging two lists with all possible permutations
但我不确定这是否是正确的方法。 permutations 是完成这项任务的正确工具吗?
【问题讨论】:
-
1.任何方法都将具有两个列表长度乘积的相同的渐近运行时间。 2.找出哪种方法最快的最简单方法是计时:docs.python.org/2/library/timeit.html