【问题标题】:Append or create and new Dictionary追加或创建新字典
【发布时间】:2014-09-17 13:35:49
【问题描述】:

我有两个文件,一个仅包含 Keys,另一个包含 Key and Value。我正在尝试使用相应的值附加密钥文件,或者使用密钥和相应的值创建一个新的输出文件。个人我可以完美地阅读关键和价值。我在将两者合并在一起时遇到了麻烦。它下面的代码一起显示了最终值。我知道第一个 for 循环正在结束,然后第二个 for 循环正在开始。这就是我只从键和值文件中获取最后一项的原因。如何以简单的方式解决这个问题?

from collections import defaultdict

with open('input1', 'r') as classified_data:
    with open('input2', 'r') as edge_data:    
        with open('output', 'w') as outfile:  
        for row in classified_data:
            col = row.strip().split()
            key = col[0], col[1]
            #print key
        for row in edge_data:
            col = row.strip().split()           
            value = col[2], col[3], col[4]
            #print value
        print {key:value}

输入1:

3545 4945
3545 2814
3545 5045
3545 4921
3545 2564
3545 2311
3545 1644
3545 3820
3545 388
3545 928
3545 3626
3545 1191
3545 4243
3545 3867
3545 701

输入2:

4945 3545 57 250848.0 4400.84210526 
3584 292 5 1645.0 329.0 
4824 2283 5 16867.0 3373.4 
1715 55 1 681.0 681.0 
5409 2822 2 3221.0 1610.5 
4955 656 6 3348.0 558.0 
4157 487 1 201.0 201.0 
2628 309 2 2466.0 1233.0 
3929 300 2 1742.0 871.0 
3730 489 12 10706.0 892.166666667 
5474 2336 2 1533.0 766.5 
3877 716 10 45028.0 4502.8 
3058 3045 12 17328.0 1444.0 

【问题讨论】:

  • 目前还不清楚如何将第一个文件的行与第二个文件的行关联起来。第一个文件的第 1 行是否与第二个文件的第 1 行相关,依此类推?
  • @goncalopp 不行。我必须使用files2中的密钥对从file1中搜索密钥对,然后从file2中提取相应的值并打印。

标签: python file dictionary key key-value


【解决方案1】:

在我看来,您想使用从第一个文件中获得的键对第二个文件的数据进行多次查找。如果是这种情况,并假设您可以将第二个文件放入内存中,那么我建议您将第二个文件读入带有您将用于查找的键的字典:

edges = {}
with open('input2', 'r') as edge_data:    
    for row in edge_data:
        col = row.strip().split()
        edges[col[0], col[1]] = col[2], col[3], col[4]

然后进行查询,通读第一个文件并打印出匹配项:

with open('input1', 'r') as classified_data:
    for row in classified_data:
        key = tuple(row.strip().split())
        print key, edges.get(key)

如果您想根据任一数字顺序匹配键,则可以修改最后一段代码以显式尝试两种组合:

with open('input1', 'r') as classified_data:
    for row in classified_data:
        a, b = row.strip().split()
        print a, b, edges.get((a, b), edges.get((b, a)))

【讨论】:

  • 错误:Traceback (most recent call last): File "dict_one.py", line 12, in <module> print key, edges[key] KeyError: ('3545', '4945') 两个文件中都存在密钥
  • 嗯,KeyError 似乎另有说明。您确定“input2”文件中存在该密钥吗?在您在问题中发布的示例输入中,该键实际上不存在于 input2 中。 (实际上 input2 中没有任何以 3545 开头的内容!)
  • 我刚刚更新了答案中的代码以避免抛出 KeyError,但它现在可能只是为您的所有键打印出 None
  • 该文件大约有一百万行,我无法发布所有行 :) 我从每个文件中发布了 15 行。让我更改输入序列,以便至少匹配一对。
  • 是的,这会很有帮助!
【解决方案2】:

如何在元组列表中收集键和值,最后将它们压缩到字典中?

from collections import defaultdict

keys = []
values = []

with open('input1', 'r') as classified_data:
    # with open('output', 'w') as outfile:  
    for row in classified_data:
        col = row.strip().split()
        keys.append((col[0], col[1]))
        # print key
with open('input2', 'r') as edge_data:    
    for row in edge_data:
        col = row.strip().split()           
        values.append((col[2], col[3], col[4]))
        # print value

print dict(zip(keys,values))

【讨论】:

  • 那我没听懂你的问题……我以为你想把 input1 的第一行作为键,把 input2 的第一行作为新字典的值。
猜你喜欢
  • 2018-06-22
  • 2013-04-30
  • 2015-02-04
  • 1970-01-01
  • 2017-01-07
  • 2013-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多