简体   繁体   中英

Updating text file from the python dictionary

Hello Community Members,

Suppose that i am having a dictionary in python:

dict = {'fresh air', 'entertainment system', 'ice cream', 'milk', 'dog', 'blood pressure'}

and a list of texts like:

text_file = ['is vitamin d in milk enough', 'try to improve quality level by automatic intake of fresh air', 'turn on the tv or entertainment system based on that individual preferences', 'blood pressure monitor', 'I buy more ice cream', 'proper method to add frozen wild blueberries in ice cream']

I want to display each occurrence of phrase belong to dictionary (say fresh air) as #fresh_air# in all the occurrences of the text file whereas for each single word of the dictionary (say milk ), the output should display as #milk# , ie appending special characters at the start and end in all the occurrences of the text_file.

Output which I want should be in the following form (lists of lists):

[[is vitamin d in #milk# enough], [try to improve quality level by automatic intake of #fresh_air#], [turn on the tv or #entertainment_system# based on the individual preferences], [#blood_pressure# monitor], [I buy more #ice_cream#], [proper method to add frozen wild blueberries in #ice_cream# with #milk#]]

Does there exist any standard way to achieve this in an time-efficient manner?

I am a newbie to lists and text processing using python, I have tried using list comprehension but failed to achieve the desired results. Any help is deeply appreciated.

Using Regex.

Ex:

import re
data = {'fresh air', 'entertainment system', 'ice cream', 'milk', 'dog', 'blood pressure'}
pattern = re.compile("("+"|".join(data)+")")
text_file = ['is vitamin d in milk enough', 'try to improve quality level by automatic intake of fresh air', 'turn on the tv or entertainment system based on that individual preferences', 'blood pressure monitor', 'I buy more ice cream', 'proper method to add frozen wild blueberries in ice cream']

result = [pattern.sub(r"#\1#", i) for i in text_file]
print(result)

Output:

['is vitamin d in #milk# enough',
 'try to improve quality level by automatic intake of #fresh air#',
 'turn on the tv or #entertainment system# based on that individual preferences',
 '#blood pressure# monitor',
 'I buy more #ice cream#',
 'proper method to add frozen wild blueberries in #ice cream#']

Note your dict variable is a set object.


Updated snippet as requested in comment.

Demo:

import re
data = {'fresh air', 'entertainment system', 'ice cream', 'milk', 'dog', 'blood pressure'}
data = {i: i.replace(" ", "_") for i in data}
#pattern = re.compile("("+"|".join(data)+")")
pattern = re.compile(r"\b("+"|".join(data)+r")\b")
text_file = ['is vitamin d in milk enough', 'try to improve quality level by automatic intake of fresh air', 'turn on the tv or entertainment system based on that individual preferences', 'blood pressure monitor', 'I buy more ice cream', 'proper method to add frozen wild blueberries in ice cream']

result = [pattern.sub(lambda x: "#{}#".format(data[x.group()]), i) for i in text_file]
print(result)

Output:

['is vitamin d in #milk# enough',
 'try to improve quality level by automatic intake of #fresh_air#',
 'turn on the tv or #entertainment_system# based on that individual preferences',
 '#blood_pressure# monitor',
 'I buy more #ice_cream#',
 'proper method to add frozen wild blueberries in #ice_cream#']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM