在Python中同时迭代2个不同的词典 [英] Iterate in 2 different dictionaries simultaneously in Python

查看:156
本文介绍了在Python中同时迭代2个不同的词典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

---编辑2 ---
所以我得到的问题为什么我使用字典?
这个问题是对这个问题的追踪: csv文件压缩而不使用Python中的现有库



我需要压缩一个500k的csv文件(19MB),我选择使用字典将ticks存储在一个csv文件中,并在另一个csv文件中symbs可以解压缩值



QUESTION :如何迭代最优化的方式?这只是一个4行的例子,但是我的真实文件有五十万行,我需要永远的遍历列表。



我有3个字典: / p>

  originalDict = {
0:['6NH8','F','A','0' 60541567','60541567','7878','20'],
1:['6NH8','F','A','0' ,'25'],
2:['6AH8','F','B','0','60541765','60541765','90 .52','1'],
3:['QMH8','F','B','0','60437395','60437395','950.5','1']
}
ticks = {0: 6NH8',1:'6AH8',2:'QMH8'}
symbs = {0:'F,A',1:'F,B'}
/ pre>

我想通过originalDict进行迭代,然后更改ticks,然后在 index 1 index 2 然后删除 index 2



所以,即

  0: ['6NH8','F','A','0','60541567','60541567','78 .78','20'] 

成为:

  [0,'0','0' 60541567','60541567','78 .78','20'] 

我目前有一个for循环在originalDict中执行值,并在另一个for循环内:

  for originalDict.values()中的值:
对于ticksKey,ticksValue在ticks.items()中:
如果值[0] == ticksValue:
值[0] = ticksKey

#Change symbs和删除char组合
为symbsKey,symbsValue在symbs.items()中:
comprComb = values [1] +,+值[2]

如果comprComb == symbsValue:
值[1] = str(symbsKey)
#del值[4]
#del值[4]
del值[2]

其他信息添加:
我把它们作为字典的原因是因为50万行,一些ticks发生不止一次,所以我给他们一个int是dict中的关键,对于symbs字典也是如此。

解决方案

所以首先你想要反向映射,你目前正在看价值,这是错误和缓慢的:

  ticks = {0:'6NH8',1:'6AH8',2:'QMH8' } 
symbs = {0:'F,A',1:'F,B'}

使用 ticks = {v:k for k,v in ticks.items()} (相同于 symbs ):

  {'6NH8':0,'QMH8':2,'6AH8':1}#ticks 

{'F,A':0,'F,B':1}#symbs

现在,您有良好的数据结构,您可以做得相当快。



现在将保存数据的字典转换为列表(不知道为什么一个字典开始):

  originalList = [originalDict [k] for k in range(len(originalDict))] 

并重新映射值:

  for originalList:
line [0 ] = ticks [line [0]]
line [1:3] = [symbs [%s,%s%tuple(line [1:3])]]

结果:

  [[0 ,'0','60541567','60541567','7878','20'],[0,0,'0','60541569' [1,1,'0','60541765','60541765','90.52','1'],[2,1,'0','60437395','60437395','950.5','1' ]] 


---EDIT 2--- So I get the question Why I use dictionaries?, this question is a follow up on this one: csv file compression without using existing libraries in Python

I Need to compress a 500k csv file (19MB), and I chose to use dictionary to store the ticks in one csv file and symbs in another to be able to Decompress the values

QUESTION: How do I iterate the most optimized way? this is just an example of 4 rows, but my real file has 500 000 lines, and takes me for ever to iterate through the list.

I have 3 dictionaries:

originalDict = {
               0: ['6NH8', 'F', 'A', '0', '60541567', '60541567', '78.78', '20'], 
               1: ['6NH8', 'F', 'A', '0', '60541569', '60541569', '78.78', '25'], 
               2: ['6AH8', 'F', 'B', '0', '60541765', '60541765', '90.52', '1'], 
               3: ['QMH8', 'F', 'B', '0', '60437395', '60437395', '950.5', '1']
               }
ticks = {0: '6NH8', 1: '6AH8', 2: 'QMH8'}
symbs = {0: 'F,A', 1: 'F,B'}

I want to iterate through originalDict and change the "ticks" and then the symbs at index 1 and index 2 and then remove index 2

so, i.e.

0: ['6NH8', 'F', 'A', '0', '60541567', '60541567', '78.78', '20']

becomes:

[0, '0', '0', '60541567', '60541567', '78.78', '20']

I have currently a for loop going through values in originalDict, and inside that another for loop:

for values in originalDict.values():
    for ticksKey, ticksValue in ticks.items():
        if values[0] == ticksValue:
            values[0] = ticksKey

    #Change symbs and remove char combination
    for symbsKey, symbsValue in symbs.items():
        comprComb = values[1] + "," + values[2]

        if comprComb == symbsValue:
            values[1] = str(symbsKey)
            #del values[4]
            #del values[4]
            del values[2]

ADDITIONAL INFO ADDED: The reason I have them as dictionary is because the 500 000 lines, some of the ticks occurs more than once, so, I give them a int which is the key in the dict, so goes for the symbs dictionary too.

解决方案

So first of all you want to reverse the mapping, you are currently looking by value, which is wrong and slow:

ticks = {0: '6NH8', 1: '6AH8', 2: 'QMH8'}
symbs = {0: 'F,A', 1: 'F,B'}

Using ticks = {v: k for k, v in ticks.items()} (same for symbs):

{'6NH8': 0, 'QMH8': 2, '6AH8': 1} # ticks

{'F,A': 0, 'F,B': 1} # symbs

Now that you have good data structures you can do this rather fast.

Now transform the dictionary that holds the data to a list (not sure why it is a dictionary to start with):

originalList = [originalDict[k] for k in range(len(originalDict))]

And re-map values:

for line in originalList:
    line[0] = ticks[line[0]]
    line[1:3] = [symbs["%s,%s" % tuple(line[1:3])]]

result:

[[0, 0, '0', '60541567', '60541567', '78.78', '20'], [0, 0, '0', '60541569', '60541569', '78.78', '25'], [1, 1, '0', '60541765', '60541765', '90.52', '1'], [2, 1, '0', '60437395', '60437395', '950.5', '1']]

这篇关于在Python中同时迭代2个不同的词典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆