【发布时间】:2015-09-15 08:14:31
【问题描述】:
我有以下字符串:
S = "to be or not to be, that is the question?"
我希望能够创建一个具有以下输出的字典
{'question': 4, 'is': 1, 'be,': 1, 'or': 1, 'the': 1, 'that': 1, 'be': 1, 'to': 1, 'not': 1}
我得到单词旁边每个单词中元音的数量,而不是每个单词本身的计数。到目前为止,我有:
{x:y for x in S.split() for y in [sum(1 for char in word if char.lower() in set('aeiou')) for word in S.split()]}
输出为:
{'or': 4, 'the': 4, 'question?': 4, 'be,': 4, 'that': 4, 'to': 4, 'be': 4, 'is': 4, 'not': 4}
如何从字符串中获取字典,其中值是每个单词的元音计数?
【问题讨论】:
-
{'tell':1, 'me':1, 'what':1, 'I':1, 'tell':1, 'you,':2, 'to':1, 'you':2}不是有效的字典,因为其中有多次键。 -
Nikki,欢迎来到 StackOverflow 我不认为这是一个 -6 的问题,所以我投了赞成票。以后,尽量把你的问题清楚地分开,并以问题的形式陈述出来,这样你就不会再得到这个接待了。如果您接受答案,它将给您的代表加 2。干杯。我会尽力帮你在这里重述这个问题。
标签: python string dictionary