如何匹配列表和字典相应的Python

时间:2017-01-05 03:04:48

标签: python list dictionary

我有1个列表和1个字典。 我想找到包含mykeys list中单词的句子。

mykeys=['city', 'salon', 'last', 'website', 'car', 'offense', 'open', 'day', 'apple','tree']
mydict={u'00:01:00,880 --> 00:01:34,550': u'created by at the insistence of open and opened, 'u'the little boys, to cater last object to the fact that the mantle Arnold fall with an eye and water on the map and at the salon of the damage of a now will forgive if Mr. Odd enough for the rest of the fat of the day last of the best place to run they saw finances of a sense of hope that they say the.',
        u'00:01:34,710 --> 00:02:00,460': u'lot at the at the end of that call of the key to a loss of the holder of the offense of the offense of the season sat, a lot to the city to city that the staff at the fourth of that.',
        u'00:00:30,080 --> 00:01:00,710': u'of the game is at a Sonoma world of cities wants any site at gained a son, some crying again and again and they will be if not as a starter for that CNS that and I and far made for the man at the one that ended up by a wall in java battled head up, finished 10 a week of school full time at Seattle king eight you did.',
        u'00:00:01,310 --> 00:00:30,080': u'The list of the din of the dossier scene of the site is the issues of stocks and sacked for you to close at least a minute, that uses of the Cincinnati at all of the east of science of the insisted a lot of the senate committees that king case out kingpins in Natal and they all died in Lima and begin and died of.'}

mydict包含{speechtime:speech sentences}

mykeys包含[10个关键字]

我想找到包含mykey list中关键字的seech语句。

例如: 匹配的语音句子如下:

因为它包含:城市,沙龙,最后,进攻,开放,日。

{u'00:01:00,880 --> 00:01:34,550': u'created by at the insistence of open and opened, 'u'the little boys, to cater last object to the fact that the mantle Arnold fall with an eye and water on the map and at the salon of the damage of a now will forgive if Mr. Odd enough for the rest of the fat of the day last of the best place to run they saw finances of a sense of hope that they say the.',
 u'00:01:34,710 --> 00:02:00,460': u'lot at the at the end of that call of the key to a loss of the holder of the offense of the offense of the season sat, a lot to the city to city that the staff at the fourth of that.',}

其他人无法找到关键字。

所以如何生成匹配的结果,如newdic

newdic={u'00:01:00,880 --> 00:01:34,550': u'created by at the insistence of open and opened, 'u'the little boys, to cater last object to the fact that the mantle Arnold fall with an eye and water on the map and at the salon of the damage of a now will forgive if Mr. Odd enough for the rest of the fat of the day last of the best place to run they saw finances of a sense of hope that they say the.',
 u'00:01:34,710 --> 00:02:00,460': u'lot at the at the end of that call of the key to a loss of the holder of the offense of the offense of the season sat, a lot to the city to city that the staff at the fourth of that.',}

4 个答案:

答案 0 :(得分:0)

您可以使用for循环使用iteritems来迭代字典,然后使用另一个for循环来迭代您的密钥,搜索每个句子中的每个密钥。

mykeys = [ ... ]
mydict = { ... }

newdic = {}
for time,sentence in mydict.iteritems():
    for word in mykeys:
        if word in sentence:
            newdic[time] = sentence
            break

print(newdic)

如果您使用的是Python 3,请使用items而不是iteritems

答案 1 :(得分:0)

newdic = {key:value for key, value in mydict.items() \
              if any(map(lambda x: x in mykeys, value.split(' ')))}

如果使用Python2,请使用itertitems()而不是items()。 ; - )

答案 2 :(得分:0)

我认为您想要的是将句子转换为一组单词,并检查单词集是否与关键字集相交(&):

>>> for k, v in mydict.items():
...  words = set(v.split())
...  if words & set(mykeys):
...   print(k)
... 
00:01:00,880 --> 00:01:34,550
00:01:34,710 --> 00:02:00,460
>>> 

答案 3 :(得分:0)

在python 3中,你可以遍历字典值,然后对每个值测试mykeys中的每个单词,如下所示:

for v in mydict.values():
    for k in mykeys:
        if k in v:
            (k, v)

或作为列表理解:

[(k, v) for v in mydict.values() for k in mykeys if k in v]

要获得发言时间,请尝试:

[(k, key, v) for key, v in mydict.items() for k in mykeys if k in v]

如果您正在使用python 2,我认为您需要使用itervalues或iteritems而不是值或项目。