Dict and List Manipulation Python

2024/11/20 21:24:51

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 defaultdictedges = {}
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
Answer

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, valuek = " ".join(i.strip("'") for i in k.strip("(").split(", ")) # clean unwanted symbol and merge togetherv = v.strip(" []").split(", ") # get list valueedges[k] = vwith open("Input_2", "r") as classified_data:for row in classified_data:k = row.strip();for v in edges.get(k, []):print k, v
https://en.xdnf.cn/q/118371.html

Related Q&A

Python - input of file path

this code works fine when I put the path of the file myself. but when I want to get it from users raw_input() it doesnt work. what can I do?import string import randomprint "enter number between …

libmproxy and mitmproxy documentation

I am new to the mitmproxy world. I need to write a python script that would log all the requests made from a certain app on Genymotion emulator. Now, I learned that mitmproxy can be helpful for my requ…

Printing mutiple HTML tables using tabulate in python

I want to produce two HTML tables using tabulate package, but I am only able to produce one table and send mail.Is it possible to put more than one html table into a message sent with smtplib and email…

Why am I getting presentation error in python list?

My code is following: d = [int(d) for d in input().split()]l = [] c = 1 for i in range(len(d)):if d[i] not in l:l.append(d[i])c += 1for i in range(len(l)):print(l[i], end=" ")Evaluation: Inpu…

Calculating BLEU score between two sumamries in python

prediction = "I am ABC. I have completed my bachelors degree in computer application at XYZ University and I am currently pursuing my masters degree in computer application through distance educat…

Pivot a dataframe with duplicate values in Index

I have a pandas dataframe like thissnapDate instance waitEvent AvgWaitInMs 0 2015-Jul-03 XX gc cr block 3-way 1 1 2015-Jun-29 YY gc current b…

Concatenate .txt files with same names in different folders with python

I have two folders containing many text files with matching file names. So I am concatenating folder1/file1.txt with folder2.file1.txt. My current code appends data from folder2/file1 to folder2/file1 …

Importing module via another module

In module A, I import module B. Then, in module C, I import module A. In module C, will I be able to use the content of module B implicitly via the import of module A, or will I have to explicitly impo…

Why time.time() gives 0.0?

I have a python program defined by a function myFunc(m,n)Basically, the function contains two for loops.def myFunc(m, n) : for i in range(m) : for j in range(n) : # do it ...return I would like to calc…

Function reads np.array - produces the mean for k nn to number p in np.array

I need to defina a function which reads a numpy array and produces the mean for k nearest points to number p in the array. Example: array= np.array([1, 2, 3, 4, 5, 6, 7, 50, 24, 32, 9, 11, 12, 10]) p= …