【问题标题】:change sentence into dictionary in Python在 Python 中将句子转换为字典
【发布时间】:2012-11-28 04:50:36
【问题描述】:

我有以下字符串:

>>>sentence='No, I shouldn't be glad, YOU should be glad.'

而我想要的是制作一个字典,以句子中的一个词为键,下一个词为值。

>>>dict(sentence)
{('No,'): ['I'], ('I'): ['shouldn't'], ('shouldn't'): ['be'], ('be'): ['glad,', 'glad.'], ('glad,'): ['YOU'], ('YOU'): ['should'], ('should'): ['be']} 
                                                                 ^        ^       ^             
                                                                 |        |       |

正如你所见,如果一个词在一个句子中出现多次,它就会得到多个值。如果它是最后一个单词,它将不会被添加到字典中。 'glad' 不会获得多个值,因为单词以 ',' 或 '.' 结尾这使它成为一个不同的字符串。

【问题讨论】:

  • 那么这里的预期输出是什么?
  • 太好了,你试过什么?你遇到了什么问题?
  • 喜剧单行答案:print (lambda words: {key: value for key, value in {word:[nextWord for curWord, nextWord in zip(words, words[1:]) if curWord == word] for word in set(words)}.iteritems() if len(value) > 0})("No, I shouldn't be glad, YOU should be glad.".split())(仅在全球换行短缺危机的情况下使用)

标签: python string dictionary add


【解决方案1】:
import collections

sentence = "No, I shouldn't be glad, YOU should be glad."

d = collections.defaultdict(list)
words = sentence.split()
for k, v in zip(words[:-1], words[1:]):
   d[k].append(v)
print(d)

这会产生

defaultdict(<type 'list'>, {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']})

【讨论】:

  • 然后,如果您将d.default_factory 设置为None,则在大多数其他情况下您将有常规的字典行为:)
  • 我的问题是一个更复杂的程序的一部分,是否可以更改类型,所以我没有以下问题:'TypeError: expected {(, , ): []}'
  • 问题已解决。我需要做的就是:plain_dict = dict(d) 谢谢你的方法!
【解决方案2】:

使用dict.setdefault():

In [9]: strs = "No, I shouldn't be glad, YOU should be glad."


In [19]: dic = {}

In [20]: for x, y in zip(words, words[1:]):
      dic.setdefault(x, []).append(y)     
   ....:     

In [21]: dic
Out[21]: 
{'I': ["shouldn't"],
 'No,': ['I'],
 'YOU': ['should'],
 'be': ['glad,', 'glad.'],
 'glad,': ['YOU'],
 'should': ['be'],
 "shouldn't": ['be']}

【讨论】:

    【解决方案3】:

    这未经测试,但应该很接近。

    words = sentence.split()
    sentenceDict = {}
    for index in xrange(len(words)-1):
        if words[index] in sentenceDict:
            sentenceDict[words[index].append(words[index+1])
        else
            sentenceDict[words[index]] = [words[index+1]]
    

    【讨论】:

    • 它应该可以工作,但是所有的代码重复都不是很好,特别是因为有一个很好的.setdefault() 方法随时可用。
    【解决方案4】:
    import collections
    
    sentence = "No, I shouldn't be glad, YOU should be glad."
    
    d = collections.defaultdict(list)
    words = sentence.split()
    for k, v in zip(words[:-1], words[1:]):
       d[k].append(v)
    print(d)
    

    这会产生

    defaultdict(<type 'list'>, {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']})
    

    @NLS:我只是想在此添加一些内容。 “d = collections.defaultdict(list)”,就像 dict 对象不保留单词的顺序,所以如果我们必须保留句子的顺序,我们可能必须使用元组。

    【讨论】:

      【解决方案5】:

      如果顺序不重要,只是另一种方法

      sentence="No, I shouldn't be glad, YOU should be glad."
      #Split the string into words
      sentence = sentence.split()
      #Create pairs of consecutive words
      sentence = zip(sentence,sentence[1:])
      from itertools import groupby
      from operator import itemgetter
      #group the sorted pairs based on the key
      sentence = groupby(sorted(sentence, key = itemgetter(0)), key = itemgetter(0))
      #finally create a dictionary of the groups
      {k:[v for _,v in  g] for k, g in sentence}
      {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 2016-10-16
        • 1970-01-01
        • 2020-03-10
        • 2021-01-17
        • 2021-12-23
        • 1970-01-01
        相关资源
        最近更新 更多