2014-12-04 49 views
1

我有一个任务,我必须按照单词长度打印单词。 例如:Python - 按长度打印单词

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) 

这由它们的长度排序的话,但我不知道如何从排序的单词中获得正确的输出。任何帮助赞赏。我也明白字典是需要的,但我不确定。 在此先感谢。

回答

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.stripstring.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'] 
+0

我会等待看看是否有人可以提供使用字典的答案。如果没有,那么我会接受你的答案。 – user3036519 2014-12-04 08:25:27

+0

@ user3036519我的第二个答案是只使用字典。 – 2014-12-04 08:26:56

+0

你可以在sort(d.values())中使用第二种方法,如v_list:print''.join(v_list) – thiruvenkadam 2014-12-04 08:30:41

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'] 
+1

这不回答这个问题......这是一个良好的开端,但。 – rnevius 2014-12-04 08:15:26

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]) 

这实际上消除在句子中的逗号,分号或句号。

2

这可以在O(N)时间使用defaultdict(或正则字典)完成。排序+ GROUPBY为O(N日志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 

+0

所以,'sorted(D.items())'是'O(N)'在这里? – 2014-12-04 08:47:22

+0

@AshwiniChaudhary,它是O(M log M)其中M是不同长度的数量。 M通常比N小得多,但最坏的情况是M == N – 2014-12-04 08:55:03

相关问题