我有一本字典如下:
'give': (('VBP', 6), ('VB', 15)),
'discounts': ('NNS', 1),
'maintaining': ('VBG', 4),
'increasing': ('VBG', 18),
'spending': (('NN', 24), ('VBG', 2)),
'become': ((('VBN', 7), ('VB', 15)), ('VBP', 1)),
'permanent': ('JJ', 2),
'fixtures': ('NNS', 1),
'news': ('NN', 24),
'weeklies': ('NNS', 2),
'underscore': ('VBP', 1),
'fierce': ('JJ', 2),
'competition': ('NN', 10)
我正在编写列表理解如下:
result = [x for x in mydict.items() if type(x[1][0]) == 'str']
但这导致一个空列表,而如果我看到字典中有许多元素,这个条件满足。
答案 0 :(得分:3)
您可以将'str'
更改为str
,即
result = [x for x in mydict.items() if type(x[1][0]) == str]
或者您可以尝试使用isinstance
方法检查它是否是string
(details)的实例:
result = [x for x,value in mydict.items() if isinstance(x[1][0],str)]
print(result)
结果:
['increasing', 'maintaining', 'fierce', 'permanent', 'fixtures', 'underscore', 'news', 'weeklies', 'discounts', 'become', 'give', 'competition', 'spending']