将列表拆分为较小的列表

时间:2018-03-08 13:12:39

标签: python

我有一个包含字符串的列表 例如

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']

我想将其列入该列表中的较小列表。每个列表都是具有相同字母的单词,例如:

list_of_words = [['deltas', 'desalt', 'salted'], ['generating', 'greatening']]

我试着考虑一个可以帮助我的代码但是我出来的代码给了我很多错误或者只是没有工作。

4 个答案:

答案 0 :(得分:2)

itertools.groupby的另一个用例:

from itertools import groupby

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 
                 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening',
                 'lasted', 'resmelts']

list_of_words.sort(key=sorted)
output = [list(words) for _, words in groupby(list_of_words, key=sorted)]
print(output)

#  [['deltas', 'desalt', 'slated', 'salted', 'staled', 'lasted'], 
#   ['generating', 'greatening'], ['retainers', 'ternaries'], ['pants'], 
#   ['smelters', 'termless', 'resmelts']]

答案 1 :(得分:2)

当你需要先排序(太高的复杂性)时,不应该使用

groupby,即使创建单行的诱惑很棒。

在这种情况下,更好的方法是使用defaultdict(list),将排序后的字母组作为键。

import collections

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']

d = collections.defaultdict(list)

for l in list_of_words:
    d[tuple(sorted(l))].append(l)

(或d["".join(sorted(l))].append(l)获取密钥字符串)

导致字典:

{('a', 'd', 'e', 'l', 's', 't'): ['deltas',
                                  'desalt',
                                  'slated',
                                  'salted',
                                  'staled',
                                  'lasted'],
 ('a', 'e', 'g', 'i', 'n', 'r', 't'): ['generating', 'greatening'],
 ('a', 'e', 'i', 'n', 'r', 's', 't'): ['retainers', 'ternaries'],
 ('a', 'n', 'p', 's', 't'): ['pants'],
 ('e', 'l', 'm', 'r', 's', 't'): ['smelters', 'termless', 'resmelts']}

答案 2 :(得分:1)

比早期答案稍微臃肿的代码,但也更具可读性:

from collections import defaultdict

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']

def getAnagrams(source):
    d = defaultdict(list)
    for word in source:
        key = "".join(sorted(word))
        d[key].append(word)
    return d

anagrams = getAnagrams(list_of_words)

separated_list = list(anagrams.values())

答案 3 :(得分:0)

list_of_words = ['deltas', 'retainers', 'desalt', 'pants', 'slated', 'generating', 'ternaries', 'smelters', 'termless', 'salted', 'staled', 'greatening', 'lasted', 'resmelts']
print(list_of_words)
srt = []

print("\n-----------------------")

def sorted_gett_letter(word):
    sorted_letters = ''.join(sorted(word))
    return sorted_letters

for word in list_of_words:
    foobar=sorted_gett_letter(word)
    srt.append(foobar)


print(srt)
print("\n-----------------------")

set_srt = set(sorted(srt))
print(set_srt)
list_of_set_srt = list(set_srt)
list_container = []

for subdiv in set_srt:
    print(subdiv)
    list_container.append(list())


print(list_container)


for word in list_of_words:  
    word_index_in_set = list_of_set_srt.index(sorted_gett_letter(word))
    print(word_index_in_set)
    list_container[word_index_in_set].append(word)

print("\n-----------------------")

print(list_container)