Sunday 15 February 2015

arrays - Dict and List Manipulation Python -



arrays - Dict and List Manipulation Python -

i have 2 files 1 has key , other has both key , value. have match key of file 1 , pull corresponding value file two. when key , value in plain column format can key , value new file well. not understanding how result when value in set/array type.

input 1 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 2 in column format. key pair i.e. col[0] , col[1] both key pairs

5391 1534 5015 4078 3430 299 4504 3369

output above input case, right 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 = {} open('input_1.txt', 'r') edge_data: row in edge_data: col = row.strip().split() edges[col[0], col[1]] = col[2], col[3] #then queries, read through first file , print out matches: open('input_2', 'r') classified_data: open ('output', 'w') outfile: 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 programme works great above given input types. have no clue how operations below given inputs.

i understand supposed alter statement above programme not getting clue should changed ?

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 2 key pair i.e. col[0] , col[1] both key pairs

3350 2542 4750 2247 5305 3341

expected output

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

i thought @three_pineapples's eval manner quite , brilliant,

here alternative 1 manipulate string:

edges = {} open("input_1.txt", "r") edge_data: row in edge_data: k, v = row.strip().split(")") # split key, value k = " ".join(i.strip("'") in k.strip("(").split(", ")) # clean unwanted symbol , merge v = v.strip(" []").split(", ") # list value edges[k] = v open("input_2", "r") classified_data: row in classified_data: k = row.strip(); v in edges.get(k, []): print k, v

python arrays list dictionary set

No comments:

Post a Comment