我有以下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
循环在preface方面不是一个好主意。我应该做些什么来获得相同的输出?你知道吗
我查看了以下链接:
Iterate over two lists with different lengths
Python merging two lists with all possible permutations
但我不确定这是不是正确的方法。permutations
是这个任务的合适工具吗?你知道吗
即使没有2个显式for循环,任何解决方案(无论是置换还是其他)都将导致隐式在引擎盖下的两个列表上循环。你知道吗
由于您询问的是代码的性能,因此有两个角度:
您的解决方案是(
n*m
)在两个列表长度(n
和m
)的函数中的Big-O表示法。这并不妨碍它在两个列表都很短(在您的示例中它们很短)并且n
和m
都很小的情况下获得良好的性能。你知道吗相关问题 更多 >
编程相关推荐