python - modify a list of strings via regex -


i have list of strings

query_var = ["venue_city_name == 'bangalore' & event_genre == 'romance' & count_event_genre >= 1","venue_city_name == 'jamshedpur' & event_genre == 'horror' & count_event_genre >= 1"] len(query_var)   #o/p 2 

i want modify list get

 query_var = ["df['venue_city_name'] == 'bangalore' & df['event_genre'] == 'romance' & df['count_event_genre'] >= 1","df['venue_city_name'] == 'jamshedpur' & df['event_genre'] == 'horror' & df['count_event_genre'] >= 1"] 

this attempt:

for res in query_var:     res = [x x in re.split('[&)]',res)]     print(res)     res =  [x.strip() x in res]     print(res)     res = [d.replace(d.split(' ', 1)[0], "df['"+d.split(' ', 1)[0]+"']") d in res]     print(res) 

which produces output:

 ["venue_city_name == 'bangalore' ", " event_genre == 'romance' ", ' count_event_genre >= 1']  ["venue_city_name == 'bangalore'", "event_genre == 'romance'", 'count_event_genre >= 1']  ["df['venue_city_name'] == 'bangalore'", "df['event_genre'] == 'romance'", "df['count_event_genre'] >= 1"]  ["venue_city_name == 'jamshedpur' ", " event_genre == 'horror' ", ' count_event_genre >= 1']  ["venue_city_name == 'jamshedpur'", "event_genre == 'horror'", 'count_event_genre >= 1']  ["df['venue_city_name'] == 'jamshedpur'", "df['event_genre'] == 'horror'", "df['count_event_genre'] >= 1"] 

as expected, when print query_var not changed

query_var out[47]:     ["venue_city_name == 'bangalore' & event_genre == 'romance' & count_event_genre >= 1","venue_city_name == 'jamshedpur' & event_genre == 'horror' & count_event_genre >= 1"] 

as can see code not produce desired output. there better way, example list comprehension?

heres regex/list comprehension solution:

>>> [re.sub('(\w+)\s*(==|>=)', r"df['\1'] \2", s) s in query_var] ["df['venue_city_name'] == 'bangalore' & df['event_genre'] == 'romance' & df['count_event_genre'] >= 1", "df['venue_city_name'] == 'jamshedpur' & df['event_genre'] == 'horror' & df['count_event_genre'] >= 1"] 

adjust needed more general data, i.e. permitting '<=', example.

edit in response comment:

[re.sub('(\w+)(\s*(==|>=).*?)(\s*&|$)', r"(df['\1']\2)\4", s) s in query_var] 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -