python - Conditionally replacing items in nested list based on values in dictionary -
right have nested list , dictionary:
phrases = [['lionhearted', 'baby preach'], ['lionhearted baby', 'preach'], ['lionhearted', 'baby', 'preach']] artist_info = {u'baby': u'baby justin bieber', u'lionhearted': u'lionhearted porter robinson', u'preach': u'preach drake', u'baby preach': u:'baby preach pop singer'}
basically, try replace values in phrases values artist_info if there match, other wise omit particular nested list. since there dictionary entry 'lionhearted'
, 'baby'
, 'baby preach' , 'preach'
, output read this:
phrases2 = [['lionhearted porter robinson','baby preach pop singer'] ['lionhearted porter robinson', 'baby justin bieber', 'preach drake']]
is there way this? i've been @ hours. it's important 3 phrases result in order of "lionhearted baby preach". i'm bit of python noob, , haven't been able find comparable example.
is looking for? example looks want omit sublist within phrases
if item of sublist not present in dictionary artist_info
. code below before doing replacements mentioned.
phrases = [['lionhearted', 'baby preach'], ['lionhearted baby', 'preach'], ['lionhearted', 'baby', 'preach']] artist_info = {u'baby': u'baby justin bieber', u'lionhearted': u'lionhearted porter robinson', u'preach': u'preach drake', u'baby preach': u'baby preach pop singer'} lst = filter(lambda x: all(y in artist_info y in x), phrases) phrases2 = map(lambda x: [artist_info[y] y in x], lst) print phrases2
Comments
Post a Comment