Dict和List操作Python [英] Dict and List Manipulation Python

查看:204
本文介绍了Dict和List操作Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,一个有键,另一个有键和值两个。我必须匹配文件1的键,并从文件二中拉取相应的值。当所有的关键字和值都是纯列格式时,我可以很好地获取关键字和值到一个新的文件。但是,当值为set / array类型时,我不了解如何获取结果。



以列格式输入一个:

  5216 3911 2 761.00 
2503 1417 13 102866.00
5570 50 2 3718.00
5391 1534 3 11958.00
5015 4078 1 817.00
3430 299 1 5119.00
4504 3369 2 3218.00
4069 4020 2 17854.00
5164 4163 1 107.00
3589 3026 1 7363.00

以列格式输入两个。它们是键对,即 col [0] col [1] 都是键对,

  5391 1534 
5015 4078
3430 299
4504 3369

上述输入案例的输出,适合我

  5391 1534 3 11958.00 
5015 4078 1 817.00
3430 299 1 5119.00
4504 3369 2 3218.00

程序

从集合导入defaultdict 

边缘= {}
with open('Input_1.txt','r')as edge_data:
for row in edge_data:
col = row.strip()。split()
边[col [0],col [1]] = col [2],col [3]
#在进行查询之前,先阅读第一个文件并打印出匹配项:
open('Input_2','r')as classified_data:
with open('Output','w')as outfile:
for classified in classified_data:
a,b = row.strip ().split()
c = edges.get((a,b),edges.get((b,a)))

#print a,b,edges.get((a,b),edges.get (b,a)))
#print a,b,c
outfile.write(%s%s%s\\\
%(a,b,c))

上述程序适用于上述给定的输入类型。但是我不知道如何获得下面给出的输入的操作。



我明白我应该从上面的程序改变这个语句,但是我没有得到任何提示应该改变什么?

  edges [col [0],col [1]] = col [2] col [3] 

新输入一个

 ('3350','2542')[6089.0,4315.0] 
('2655','1411')[559.0,1220.0,166.0,25.0,146.0,528.0, 1902.0,880.0,2317.0,2868.0]
('4212','1613')[150.0,14184.0,4249.0,1250.0,10138.0,4281.0,2846.0,2205.0,1651.0,335.0,5233.0,149.0,6816.0]
('4750','2247')[3089.0]
('5305','3341')[13122.0]

新输入二他们是键对,即 col [0] col [1] 都是双键的关键

  3350 2542 
4750 2247
5305 3341

预期输出为

  3350 2542 6089.0 
3350 2542 4315.0
4750 2247 3089.0
5305 3341 13122.0


解决方案

我以为@ three_pineapples的 eval 方式是相当好和辉煌,



这是另一个只能操纵字符串的人:

  edges = {} 
with open(Input_1.txt,r)as edge_data:
for edge in edge_data:
k,v = row.strip( ).split())#split as key,value
k =.join(i.strip(')for i in k.strip(()。split(,) )#清除不需要的符号并合并在一起
v = v.strip([])。split(,)#获取列表值
edges [k] = v

with open(Input_2,r)as classified_data:
for classified in classified_data:
k = row.strip();
for v in edges.get(k,[]):
print k,v


I have two files one has key and other has both key and value. I have to match the key of file one and pull the corresponding value from file two. When all the key and value are in plain column format i can get the key and value to a new file very well. But I am not understanding how to get a result when the value is in set/array type.

Input one in column format:

5216 3911 2 761.00 
2503 1417 13 102866.00
5570 50 2 3718.00 
5391 1534 3 11958.00 
5015 4078 1 817.00 
3430 299 1 5119.00 
4504 3369 2 3218.00  
4069 4020 2 17854.00 
5164 4163 1 107.00 
3589 3026 1 7363.00 

Input two in column format. They are key as pair i.e. col[0] and col[1] both are key as pairs

5391 1534 
5015 4078 
3430 299 
4504 3369  

Output for the above input case, which is right for me

5391 1534 3 11958.00 
5015 4078 1 817.00 
3430 299 1 5119.00 
4504 3369 2 3218.00 

Program

from collections import defaultdict

edges = {}
with open('Input_1.txt', 'r') as edge_data:    
    for row in edge_data:
        col = row.strip().split()
        edges[col[0], col[1]] = col[2], col[3]
#Then to do the queries, read through the first file and print out the matches:
with open('Input_2', 'r') as classified_data:
    with open ('Output', 'w') as outfile:    
    for row in classified_data:
            a,b = row.strip().split()
        c = edges.get((a,b), edges.get((b,a)))

        #print a,b, edges.get((a,b), edges.get((b,a)))
        #print a,b,c        
        outfile.write("%s %s %s\n" % (a,b,c))

The above program works great for the above given input types. But I have no clue how to get the operations for the below given inputs.

I understand I am supposed to change this statement from the above program but I am not getting any clue what should that be changed to ?

edges[col[0], col[1]] = col[2], col[3]

New Input one

('3350', '2542') [6089.0, 4315.0] 
('2655', '1411') [559.0, 1220.0, 166.0, 256.0, 146.0, 528.0, 1902.0, 880.0, 2317.0, 2868.0] 
('4212', '1613') [150.0, 14184.0, 4249.0, 1250.0, 10138.0, 4281.0, 2846.0, 2205.0, 1651.0, 335.0, 5233.0, 149.0, 6816.0] 
('4750', '2247') [3089.0] 
('5305', '3341') [13122.0]

New Input two They are key as pair i.e. col[0] and col[1] both are key as pairs

3350 2542
4750 2247
5305 3341

Expected output is

3350 2542 6089.0
3350 2542 4315.0
4750 2247 3089.0
5305 3341 13122.0

解决方案

I thought @three_pineapples's eval manner is quite good and brilliant,

Here is an alternative one which only manipulate string:

edges = {}
with open("Input_1.txt", "r") as edge_data:
    for row in edge_data:
        k, v = row.strip().split(")") # split as key, value
        k = " ".join(i.strip("'") for i in k.strip("(").split(", ")) # clean unwanted symbol and merge together
        v = v.strip(" []").split(", ") # get list value
        edges[k] = v

with open("Input_2", "r") as classified_data:
    for row in classified_data:
        k = row.strip();
        for v in edges.get(k, []):
            print k, v

这篇关于Dict和List操作Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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