在python中对两个列表排序?

在python中对两个列表排序?,python,list,sorting,Python,List,Sorting,我正在计算文本中出现的一些单词,我有两个列表:第一个包含单词,第二个包含出现的单词 因此,在分析的最后,我有一些 listWords : ["go", "make", "do", "some", "lot"] listOccurrences: [2, 4, 8, 1, 5] 我想将这两个列表按ListInstances DESC排序,所以我会: listWords : ["do", "lot", "make", "go", "some"] listOccurrences: [8, 5, 4,

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

因此,在分析的最后,我有一些

listWords : ["go", "make", "do", "some", "lot"]
listOccurrences: [2, 4, 8, 1, 5]
我想将这两个列表按ListInstances DESC排序,所以我会:

listWords : ["do", "lot", "make", "go", "some"]
listOccurrences: [8, 5, 4, 2, 1]
我有什么办法可以做到这一点吗?或者你知道有什么比两张单子更“自然”的方法吗?(就像一个单独的“列表”,其中每个事件都由一个单词引用)

请注意,key:values对(此处:word:count)的明显数据类型是
dict
。FWIW您可能想看看
集合。计数器

编辑:为了完整性:如果您想将所有这些都塞进一行语句中(wrt/readability可能不是一个好主意,但那是另一回事),您也可以使用内置的
sorted()
函数代替
list.sort()


另一种方法是将数据放入字典中。由于您正在计算单词的出现次数,所以listwords将具有唯一的单词,use可以将其用作字典键。可以使用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)
我会用一个。下面是毫无意义的一行:)

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

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]
整洁的转换回到列表提供感谢

此外,您可能希望首先使用
计数器
从以下位置收集频率数据:

最后:在Python中,在变量名中使用下划线被认为是时髦的,而不是大小写。可能会更改为
列出单词
列出出现的情况
?:)

一个班轮:

[listWords[i] for i, k in sorted(enumerate(listOccurrences), key=itemgetter(1), reverse=True)]
i、 e:

一种方法是使用。您可能还希望使用一个.short的一行:
list(zip(*排序(zip(listOccurrents,listWords),reverse=True))[1])
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]
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)
[listWords[i] for i, k in sorted(enumerate(listOccurrences), key=itemgetter(1), reverse=True)]
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']