我有一项任务,我必须在句子中按照长度打印单词。 例如:
Sentence: I like programming in python because it is very fun and simple.
>>> I
>>> in it is
>>> fun and
>>> like very
>>> python simple
>>> because
如果没有重复:
Sentence: Nothing repeated here
>>> here
>>> Nothing
>>> repeated
到目前为止,我到目前为止已经有了这个:
wordsSorted = sorted(sentence, key=len)
按照长度对单词进行排序,但我不知道如何从排序后的单词中获取正确的输出。任何帮助赞赏。我也明白需要词典,但我不确定。 提前谢谢。
答案 0 :(得分:6)
首先根据长度对单词进行排序,然后再次使用itertools.groupby
对其进行分组:
>>> from itertools import groupby
>>> s = 'I like programming in python because it is very fun and simple'
>>> for _, g in groupby(sorted(s.split(), key=len), key=len):
print ' '.join(g)
...
I
in it is
fun and
like very
python simple
because
programming
您也可以使用dict
:
>>> d = {}
>>> for word in s.split():
d.setdefault(len(word), []).append(word)
...
现在d
包含:
>>> d
{1: ['I'], 2: ['in', 'it', 'is'], 3: ['fun', 'and'], 4: ['like', 'very'], 6: ['python', 'simple'], 7: ['because'], 11: ['programming']}
现在我们需要迭代排序的键并获取相关的值:
>>> for _, v in sorted(d.items()):
print ' '.join(v)
...
I
in it is
fun and
like very
python simple
because
programming
如果您想忽略标点符号,则可以使用str.strip
string.punctuation
将其删除:
>>> from string import punctuation
>>> s = 'I like programming in python. Because it is very fun and simple.'
>>> sorted((word.strip(punctuation) for word in s.split()), key=len)
['I', 'in', 'it', 'is', 'fun', 'and', 'like', 'very', 'python', 'simple', 'Because', 'programming']
答案 1 :(得分:2)
这可以在O(N)时间内使用defaultdict
(或常规字典)来完成。 sort + groupby是O(N log N)
words = "I like programming in python because it is very fun and simple".split()
from collections import defaultdict
D = defaultdict(list)
for w in words:
D[len(w)].append(w)
for k in sorted(D):
print " ".join(d[k])
I in it is fun and like very python simple because programming
答案 2 :(得分:0)
试试这个:
str='I like programming in python because it is very fun and simple'
l=str.split(' ')
sorted(l,key=len)
它会返回
['I', 'in', 'it', 'is', 'fun', 'and', 'like', 'very', 'python', 'simple', 'because', 'programming']
答案 3 :(得分:0)
使用字典简化了它
input = "I like programming in python because it is very fun and simple."
output_dict = {}
for word in input.split(" "):
if not word[-1].isalnum():
word = word[:-1]
if len(word) not in output_dict:
output_dict[len(word)] = []
output_dict[len(word)].append(word)
for key in sorted(output_dict.keys()):
print " ".join(output_dict[key])
这实际上删除了句子中的逗号,分号或句号。