在python中排序两个列表?

时间:2015-05-13 10:40:56

标签: python list sorting

我正在计算文本中出现的一些单词,我有两个列表:第一个包含单词,第二个包含出现的单词。

所以在分析结束时我有类似

的东西
listWords : ["go", "make", "do", "some", "lot"]
listOccurrences: [2, 4, 8, 1, 5]

我想在listOccurrences DESC之后对这两个列表进行排序,所以我会:

listWords : ["do", "lot", "make", "go", "some"]
listOccurrences: [8, 5, 4, 2, 1]

我有什么方法可以做到这一点?或者您是否知道任何其他方式比两个列表更“自然”? (就像单个“列表”,其中每个事件都由一个词引用)

4 个答案:

答案 0 :(得分:8)

>>> listWords = ["go", "make", "do", "some", "lot"]
>>> listOccurrences = [2, 4, 8, 1, 5]
>>> listTmp = zip(listOccurrences, listWords)
>>> listTmp
[(2, 'go'), (4, 'make'), (8, 'do'), (1, 'some'), (5, 'lot')]
>>> listTmp.sort(reverse=True)
>>> listTmp
[(8, 'do'), (5, 'lot'), (4, 'make'), (2, 'go'), (1, 'some')]
>>> zip(*listTmp)
[(8, 5, 4, 2, 1), ('do', 'lot', 'make', 'go', 'some')]
>>> listOccurrences, listWord = zip(*listTmp)

请注意,键的明显数据类型:值对(此处:word:count)是dict。 FWIW你可能想看看collections.Counter

编辑:为了完整起见:如果你想在一行语句中填写所有这些,你也可以使用内置sorted()函数而不是list.sort()(这可能不是很好想法/可读性,但这是另一个故事):

>>> listWords = ["go", "make", "do", "some", "lot"]
>>> listOccurrences = [2, 4, 8, 1, 5]
>>> listOccurrences, listWords = zip(*sorted(zip(listOccurrences, listWords), reverse=True))
>>> listWords
('do', 'lot', 'make', 'go', 'some')
>>> listOccurrences
(8, 5, 4, 2, 1)

答案 1 :(得分:2)

另一种方法是将数据放入字典中。当你计算单词的出现时,列表单词将具有唯一的单词,并且使用它可以将其用作字典键。您可以使用python排序方法以相同的顺序对键和值进行排序。

listWords = ["go", "make", "do", "some", "lot"]

listOccurrences = [2, 4, 8, 1, 5]

dict = {}

i=0

while(len(listWords) > i):

    dict[listWords[i]] = listOccurrences[i];

    i = i + 1


print sorted(dict, key=dict.get, reverse=True)

print sorted(dict.values(), reverse=True)

答案 2 :(得分:1)

我使用Counter。这是毫无意义的单行内容:)

from collections import Counter

listWords, listOccurences = map(list, zip(*Counter(dict(zip(listWords, listOccurrences))).most_common()))

作为可读代码,您应该使用:

from collections import Counter

listWords = ["go", "make", "do", "some", "lot"]
listOccurrences = [2, 4, 8, 1, 5]

counter = Counter(dict(zip(listWords, listOccurrences)))

print(str(counter))
# Counter({'do': 8, 'lot': 5, 'make': 4, 'go': 2, 'some': 1})

# Want lists again?

listWords, listOccurences = map(list, zip(*counter.most_common()))

print(listWords)
# ['do', 'lot', 'make', 'go', 'some']

print(listOccurrences)
# [8, 5, 4, 2, 1]

感谢Jon Clements提供的整齐转换回列表。

此外,您可能希望首先使用Counter收集频率数据(来自here):

import collections

c = collections.Counter()
with open('/home/me/my_big_file_o_words') as f:
    for line in f:
        c.update(line.rstrip().lower())

print('Words ordered by most common:')
for letter, count in c.most_common():
    print(letter + ": " + count)

最后:在Python中使用变量名称中的下划线,而不是camelCase,它被认为是时尚的。也许更改为list_wordslist_occurrences? :)

答案 3 :(得分:0)

一衬垫:

In [62]: from operator import itemgetter
In [63]: listWords = ["go", "make", "do", "some", "lot"]
In [64]: listOccurrences = [2, 4, 8, 1, 5]
In [65]: [listWords[i] for i, k in sorted(enumerate(listOccurrences), key=itemgetter(1), reverse=True)]
Out[65]: ['do', 'lot', 'make', 'go', 'some']

即:

ListView