【问题标题】:Split a word and keep it in a dictionary in python [closed]拆分一个单词并将其保存在python的字典中[关闭]
【发布时间】:2014-08-05 04:37:47
【问题描述】:

我有一组带有词性标签的单词,我想将其拆分并保存在字典中。以键作为单词,将其值作为词性标签。 eg: He_PRP买了_VBD it_PRP

【问题讨论】:

  • 在哪里拆分它们?使用什么作为键和值保存在字典中?
  • 你应该从写一些代码开始。
  • 尝试使用谷歌进行此操作。如果您有具体问题,请发布。
  • 您希望我们猜到这里的POS 是什么意思?一块东西?
  • 给出一些示例数据。还提供您尝试过的代码。

标签: python dictionary split


【解决方案1】:

这就是你要找的吗?

text = "He_PRP bought_VBD it_PRP"
text1 = text.split(' ')
for names in text1:
    words = names.split('_')
    print words
    dictionary[words[0]] = words[1]
print dictionary

The output for this will be
['He', 'PRP']
['bought', 'VBD']
['it', 'PRP']
The dictionary will be 
{'bought': 'VBD', 'it': 'PRP', 'He': 'PRP'}

【讨论】:

    【解决方案2】:
    >>> wordset = {'He_PRP', 'bought_VBD', 'it_PRP'}
    >>> dict(x.split("_") for x in wordset)
    {'bought': 'VBD', 'it': 'PRP', 'He': 'PRP'}
    

    如果同一个词有两个不同的词性,会发生什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-14
      • 2016-10-08
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多