我期望实现的是以特定模式在列表中排列项目。我说,我有以下的解释:按特定模式排列列表项
>>>dict_a = {
'north' : 'N',
'south' : 'S',
'east' : 'E',
'west' : 'W',
'north east' : 'NE',
'north west' : 'NW'
}
我们检查,如果一个字符串包含从上述词典中的任何项目我做的:
>>>string_a = 'North East Asia'
>>>list_a = []
>>>for item in dict_a:
if item in string_a.lower():
list_a.append(item)
,它给我的结果如下,这是有道理的
>>>['north', 'north east', 'east']
但我希望得到的是['north east']
而忽略north
和east
。我如何实现这个目标?
检查的前两个单词'string_a'建立在字典 – gefei
单独的字典的关键一分为二,并与各一通。 – StoryTeller