Python 中的字典
我有一本字典,它以地名作为键,作为值,它有多个单词列表,例如,1 个条目看起来像:
{'myPlaceName': [u'silly', u'i', u'wish!', u'lol', [u'the', u'lotto'], [, u'me', u'a', u'baby?'], [u'new', u'have', u'new', u'silly', u'new', u'today,', u'cant', u'tell', u'yet,', u'but', u'all', u'guesses', u'silly']]}
如何将它们全部连接起来,以便获得与每个地点相关的常用单词? 我希望我的输出像字典中的常用词一样变得愚蠢和新颖。
I have a dictionary that has a place name as a key and as the value it has several lists of words, eg, 1 entry looks like:
{'myPlaceName': [u'silly', u'i', u'wish!', u'lol', [u'the', u'lotto'], [, u'me', u'a', u'baby?'], [u'new', u'have', u'new', u'silly', u'new', u'today,', u'cant', u'tell', u'yet,', u'but', u'all', u'guesses', u'silly']]}
How can I join them all so I can get the common words associated with each place?
I would like my output to get silly and new as the common words for the dictionary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此函数获取单词列表的列表,并返回在所有列表中找到的所有单词。
例如
get_common_words([['hi', 'hello'], ['bye', 'hi']])
返回['hi']
或巨大的一 - liner:
或者只是采用此答案的评论中建议的方法之一。
This function takes a list of a list of words, and returns all words that are found in all the lists.
eg
get_common_words([['hi', 'hello'], ['bye', 'hi']])
returns['hi']
or the giant one-liner:
or just go with one of the methods suggested in the comments of this answer.
print ', '.join(dictname['myPlaceName'])
这将创建列表中所有条目的逗号分隔列表。将
dictname
替换为您的字典名称。print ', '.join(dictname['myPlaceName'])
This will create a comma-separated list of all the entries in the list. Replace
dictname
with the name of your dict.如果它是仅包含单词的列表,请使用连接。
If it s a list containing only words, use join.