简体   繁体   English

将两个循环合二为一

[英]Combine two loop into one

I have three different loops in my code but I am wondering if there is a way to combine two of them where it only loops through once. 我的代码中有三个不同的循环,但我想知道是否有办法将它们中的两个组合在一起只循环一次。 My code is below: 我的代码如下:

for x in groups:
    A bunch of code that produces the following...
    frames[x]
    highcs[x]

I then loop through two loops below. 然后我循环通过下面的两个循环。 These are the ones I'm trying to combine. 这些是我想要结合的。

for x, frame in frames.items():
    more unrelated code

for x, highc in highcs.items():

The final two loops are really just creating excel workbooks using xlsxwriter. 最后两个循环实际上只是使用xlsxwriter创建excel工作簿。 The first loop creates the workbook and inputs dataframes from frames[x] onto the worksheets. 第一个循环创建工作簿并将帧[x]中的数据帧输入到工作表中。 Then highc just goes back over the same sheets and adds new dataframes. 然后highc返回相同的工作表并添加新的数据帧。

frames and highcs are the same length, you can use zip() to iterate them at once: frameshighcs的长度相同,你可以使用zip()一次迭代它们:

for (f,frame), (h, highc) in zip(frames.items(), highcs.items()):
    # do your work here

zip()

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables.The returned list is truncated in length to the length of the shortest argument sequence. 此函数返回元组列表,其中第i个元组包含来自每个参数序列或可迭代的第i个元素。返回的列表的长度被截断为最短参数序列的长度。

Use izip from itertools 使用itertools中的izip

from itertools import izip
for (x, frame), (x, highc) in izip(frames.items(), highcs.items()):
   ...

https://docs.python.org/2/library/itertools.html#itertools.izip https://docs.python.org/2/library/itertools.html#itertools.izip

Make an iterator that aggregates elements from each of the iterables. 创建一个聚合来自每个迭代的元素的迭代器。 Like zip() except that it returns an iterator instead of a list. 像zip()一样,除了它返回迭代器而不是列表。 Used forlock-step iteration over several iterables at a time. 用于一次迭代几次迭代的迭代步骤。

izip is more efficient than zip because izip returns an iterator instead of a list. izip比zip更有效,因为izip返回迭代器而不是列表。 To make it even more efficient, replace .items() with .iteritems() 为了提高效率,请将.items()替换为.iteritems()

Not sure what you are doing, but maybe iterate over the groups again? 不确定你在做什么,但可能会再次遍历这些组?

for group in groups:
    frame = frames[group]
    highc = highcs[group]
    ...

Dunno how pythonic this approach is, but I can think of two quick and dirty ways to do it. Dunno这种方法是多么pythonic,但我可以想到两种快速而肮脏的方法。

Are they guaranteed to be keyed with the same keys? 他们是否保证使用相同的键进行键控? If so, you could try: 如果是这样,您可以尝试:

for key in frames.keys():
    doSomething(frames[key])
    doSomethingElse(highcs[key])

Or, if not, do they have the same length? 或者,如果没有,它们是否具有相同的长度? You could: 你可以:

for i in range(len(frames.keys())):
    frameKey = frames.keys[i]
    highcsKey = highcs.keys[i]
    doSomething(frames[frameKey])
    doSomethingElse(highcs[highcsKey])

Even if they aren't the same length, you could (and probably should) test the lengths of the keys() lists for both dictionaries anyway, and only index into the longer one once you pass the bound of the shorter one. 即使它们的长度不同,您也可以(并且可能应该)测试两个字典的keys()列表的长度,并且只有在通过较短的字符串时才转换为较长的字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM