2

I have a 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 to modify this list to 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 is my attempt:

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

which produces the 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, but when I print query_var it was 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 you can see my code does not produce the desired output. Is there a better way, for example with a list comprehension?

1
  • You need to assign res value to query_var in order to change it. Commented Jan 27, 2016 at 12:37

1 Answer 1

3

Heres a regex/list comprehension solution:

>>> [re.sub('(\w+)\s*(==|>=)', r"df['\1'] \2", s) for 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 it as needed for more general data, i.e. permitting '<=', for example.

edit in response to the comment:

[re.sub('(\w+)(\s*(==|>=).*?)(\s*&|$)', r"(df['\1']\2)\4", s) for s in query_var]
Sign up to request clarification or add additional context in comments.

3 Comments

@timgeb-just one more small help....i want to include () for each segment sep by & symbol...ex.["(df['VENUE_CITY_NAME'] == 'Bangalore') & (df['EVENT_GENRE'] == 'ROMANCE') & (df['count_EVENT_GENRE'] >= 1)"
as for this i can't raise one more question.i can do this after running the reg exp.but i am just not getting how to do that inside the reg exp as suggested by you...
@Satya updated, please provide the correct/full specifications in the original question next time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.