我有一个给定的数组:
A = [9, 8, 7, 6, 5, 4]
我正在实现计数算法以在Python中对此数组进行排序。数组中的数字始终在范围内。
我得到了每个元素的计数和列表的排序。
count = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
sorted = [4, 5, 6, 7, 8, 9]
现在,我想输出每个元素的计数:例如:
4 1
5 1
6 1
7 1
8 1
9 1
我如何将这些与列表进行比较[count&数组]?
代码:
N = int(input())
A = list(map(int, input().split(" ")))
print(A)
m = N + 1
count = [0] * m
for a in A:
# count occurences
count[a] += 1
print(count)
i = 0
for a in range(m):
for c in range(count[a]):
A[i] = a
i += 1
print(A, count)
print([i,j for i,j in zip(A,count)])
我试图拉链,但效果不佳。我应该怎么做?
这有效:
count = [0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
sorted = [4, 5, 6, 7, 8, 9]
for elem in sorted:
print(elem, count[elem])