加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 将两个循环合并为一个

发布时间:2020-12-20 11:51:14 所属栏目:Python 来源:网络整理
导读:我的代码中有三个不同的循环,但我想知道是否有办法将它们中的两个组合在一起只循环一次.我的代码如下: for x in groups: A bunch of code that produces the following... frames[x] highcs[x] 然后我循环通过下面的两个循环.这些是我想要结合的. for x,fra
我的代码中有三个不同的循环,但我想知道是否有办法将它们中的两个组合在一起只循环一次.我的代码如下:

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

然后我循环通过下面的两个循环.这些是我想要结合的.

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

for x,highc in highcs.items():

最后两个循环实际上只是使用xlsxwriter创建excel工作簿.第一个循环创建工作簿并将帧[x]中的数据帧输入到工作表中.然后highc返回相同的工作表并添加新的数据帧.

解决方法

frames和highcs的长度相同,你可以使用zip()一次迭代它们:

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

压缩()

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读