2017-08-05 17 views
1

我正在開發一個側面項目,並且遇到了此問題。基本上,我處理的輸入列表,其中內部列表看起來像這樣的列表:獲取所有以多個列表中的同一個字母開始的單詞列表

- ['operating', 'alive', 'effective', 'rapid', 'progressive', 'working', 'mobile'] 
- ['enjoyable', 'pleasant', 'entertaining', 'amusing', 'lively', 'boisterous', 'convivial', 'merry', 'witty'] 

可以有任意數量的內列出的(但我認爲創建一個限制)。我想要實現的是從每個以相同字母開頭的列表中返回單詞列表。例如,從上面,我們會得到這樣的東西:

[alive, amusing], [effective, enjoyable], [effective, entertaining], [progressive, pleasant] ... 

我的問題是,什麼是一個好方法?我考慮過整個字母表,並使用布爾數組來跟蹤每個列表中哪些字母在該字母開頭有一個單詞,但它看起來效率低下,並且我對該方法不滿意。

例如(不完整,但僅做參考..):

d = dict.fromkeys(ascii_lowercase, False)  
for c in ascii_lowercase: 
    found = False 
    for item in description: 
     for syn in item: 
      if syn.startswith(c): 
       found = True 
     d[c] = found 

然後就可以抓取開始以字母的話標誌着從每個列表構建輸出列表「真」。

我錯過了一個更簡單的方法嗎?我是Python的新手,所以我不確定是否缺少一個內置函數,在這種情況下可能會有所幫助。

感謝您的閱讀!

+1

爲什麼在您的示例中有兩個以'E'開頭的單詞列表? – arsho

回答

0

我會使用一個字典「字符」:listOfWords [],並填寫在迭代你的列表...

對於所有列出的每個列表元素:

if dictionary contains the "char" with whom the element starts with 

你加入元素的關鍵「字符」

else 

創建與新原料碳字典中的新元素的列表,他的初始化列表和元素添加到新的列表。

產生的字典將是這樣的:

"a":[alive, amusing],"b":[boisterous],"c":[convivial], ... 
0

使用,每個字母單詞的列表,映射的字典。這是一些示例代碼:

from collections import defaultdict 

letterWordsDict = defaultdict(lambda: []) 

# Let ls contain sub-lists of words. 
for subls in ls: 
    for word in subls: 
     letterWordsDict[word[0]].append(word) 

groupedWords = letterWordsDict.values() 
0

如果要列出以相同字符開頭的單詞,則可以使用以下代碼段。

Python 3中(假設你只有小寫字母)

import string 

outer = [ 
    ['operating', 'alive', 'effective', 'rapid', 'progressive', 'working', 'mobile'], 
    ['enjoyable', 'pleasant', 'entertaining', 'amusing', 'lively', 'boisterous', 'convivial', 'merry', 'witty'] 
] 

lowercase = string.ascii_lowercase 
data = {lowercase[i]:[] for i in range(26)} 
for inner in outer: 
    for word in inner: 
     data[word[0]].append(word) 

flat_list = [] 
for character in sorted(data.keys()): 
    if len(data[character])!=0: 
     flat_list.append(sorted(data[character])) 

print(flat_list) 

輸出:

[['alive', 'amusing'], ['boisterous'], ['convivial'], ['effective', 'enjoyable', 'entertaining'], ['lively'], ['merry', 'mobile'], ['operating'], ['pleasant', 'progressive'], ['rapid'], ['witty', 'working']] 
2

一種選擇是排序列表的平鋪版本,然後使用groupby用自定義鍵將不同的第一個字母作爲組。

[list(grp) for _,grp in groupby(sorted(chain.from_iterable(li)), key=itemgetter(0))] 

>>> from itertools import groupby, chain 
>>> from operator import itemgetter 

>>> li = [['operating', 'alive', 'effective', 
      'rapid', 'progressive', 'working', 'mobile'], 
      ['enjoyable', 'pleasant', 'entertaining', 'amusing', 
      'lively', 'boisterous', 'convivial', 'merry', 'witty']] 

>>> [list(grp) for _,grp in 
    groupby(sorted(chain.from_iterable(li)), key=itemgetter(0))] 
[['alive', 'amusing'], 
['boisterous'], 
['convivial'], 
['effective', 'enjoyable', 'entertaining'], 
['lively'], 
['merry', 'mobile'], 
['operating'], 
['pleasant', 'progressive'], 
['rapid'], 
['witty', 'working']] 
+0

我的投票是這個答案,一個純pythonic的方式。你可以請一步一步解釋清單理解嗎?非常感謝。 – Ajay2588

+0

@ Ajay2588查看['groupby()'](https://docs.python.org/3/library/itertools.html#itertools.groupby),['chain.from_iterable()'](https:// docs.python.org/3/library/itertools.html#itertools.chain.from_iterable),['itemgetter()'](https://docs.python.org/3/library/operator.html#operator.itemgetter )和['sorted()'](https://docs.python.org/3/library/functions.html#sorted) - 如果您有任何具體問題,請告訴我! – miradulo

0

列表解析會令工作更簡單!

您需要在手,i通過第一內部列表l[0]迭代,與通過每個元素在第二內部列表進行迭代,l[1]j。如果你的情況滿足,然後將它們添加到列表中!

>>> l 
[['operating', 'alive', 'effective', 'rapid', 'progressive', 'working', 'mobile'], ['enjoyable', 'pleasant', 'entertaining', 'amusing', 'lively', 'boisterous', 'convivial', 'merry', 'witty']] 

>>> [[i,j] for j in l[1] for i in l[0] if j.startswith(i[0])] 
[['effective', 'enjoyable'], ['progressive', 'pleasant'], ['effective', 'entertaining'], ['alive', 'amusing'], ['mobile', 'merry'], ['working', 'witty']] 
0

我扁平列表的列表中第一個,然後我排序由所述第一信組通過該鍵,最後我已經提取出的組的值到一個列表中再包整成列表作爲結果。

>>> from operator import itemgetter 
>>> from itertools import chain 

>>> items = [['operating', 'alive', 'effective', 'rapid', 'progressive', 'working', 'mobile'], ['enjoyable', 'pleasant', 'entertaining', 'amusing', 'lively', 'boisterous', 'convivial', 'merry', 'witty']] 


>>> first_item = itemgetter (0) 

>>> flattened_items = chain.from_iterable (items) 

>>> list (list (gitems) for _, gitems in groupby (sorted (flattened_items, key = first_item), key = first_item)) 

[['alive', 'amusing'], ['boisterous'], ['convivial'], ['effective', 'enjoyable', 'entertaining'], ['lively'], ['mobile', 'merry'], ['operating'], ['progressive', 'pleasant'], ['rapid'], ['working', 'witty']] 
相關問題