What is the most efficient way to match keys from a dictionary to data in text file

2024/10/7 2:26:18

Say I have the following dictionary:

data=[a 1 : A, b 2 : B, c 3 : C, d 4 : D]

and a .txt file which reads:

Key      a 1  b 2  c 3  d 4
Word     as   box  cow  dig

(note values are seperated by \t TAB character)

How can I use the keys from the data dictionary to find the corresponding word from the .txt file? Ideally I would like to output a dictionary like:

data=[a 1 : as, b 2 : box, c 3 : cow, d 4 : dig]

Please ask for more info. if needed.

Thanks,

Alex

Answer

Something like this:

with open('abc') as f:keys = map(str.strip, next(f).split('Key      ')[1].split('  '))vals = map(str.strip, next(f).split('Word     ')[1].split('\t'))print dict(zip(keys,vals))
...     
{'d 4': 'dig', 'b 2': 'box', 'a 1': 'as', 'c 3': 'cow'}
https://en.xdnf.cn/q/118873.html

Related Q&A

Converting an excel file to a specific Json in python using openpyxl library

I have the Excel data with the format shown in the image preview. How can I convert it into a JSON using Python? Expected Output: file_name = [ { A: Measurement( time=10, X1=1, X2=4 ), B: Measurement(…

Why this algorithm can sort data in descending order

I study python programming and try to sort data in descending order.#sort1 below is successfully sorted but I cannot understand why this happen. Also, data[i], data[data.index(mn)] = data[data.index(m…

Processing.py - Unknown Error on Class Definition

I have no idea how to fix this error. Maybe theres an open parenthesis or quotation mark somewhere before this line?What is wrong with this code?Class Ribbon: # I got an error on this line! def __ini…

How can I implement a stopwatch in my socket Python program

I am creating a very simple ping program in Python that will send 16- 64 bytes of information to a local server, once the server has received all the bytes, it will send back a 1 Byte message back to t…

Making a quiz with shuffled questions

I am wanting to join the Royal Air Force and thought as a good way to prepare I should code myself a quiz about their aircraft.There are 28 aircraft that I have added to the quiz. For example - Where i…

How to create a timer that resets for quiz games?

I trying to create a timer for my quiz game. It should reset after every right question. But problem with my code is that it keeps increasing speed after every time it resets. timeCount = 30 def countd…

Stucking of python multiprocessing.pool

i have multiprocessing script with "pool.imap_unordered". I ran into a problem when the script got stuck but i check CPU usage — nothing happening (by "top" command on ubuntu). Goi…

python: merge two csv files

I have a problem while Im doing my assignment with python. Im new to python so I am a complete beginner.Question: How can I merge two files below?s555555,7 s333333,10 s666666,9 s111111,10 s999999,9and…

Generate a custom formated string with python

I have a Javascript code that generates a string (similar to uuid) stringHere it is the js code: var t = "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxx", i = (new Date).getTime(); return e = t.repla…

PySide-6.6: clicked signal sends extra boolean argument to slot

I just upgraded PySide6 (PySide6==6.6.0 from 6.2.2) and a new behavior is wreaking havoc with my GUI program. Every place where I have a clicked signal, the hooked up slot is receiving an extra bool==F…