2014-12-04 107 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

相關問題