Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

python - I created a function that groups numbers from a list to a list based on their frequency and I need help optimizing it

UPDATE - I have managed to 'fix' my problem but I'm a 100% sure that this is not the optimal solution. Also I forgot to mention that the order of each frequency group must be ordered from greatest number to least input:

[-1,1,-6,4,5,-6,1,4,1]

output:

[[5, -1], [4, 4, -6, -6], [1, 1, 1]]

This is the updated code that I wrote:

def freqSort(nums)
#This section makes an ordered hash map that is ordered from least to greatest frequency
    hash_map = {x:0 for x in nums}
    hash_lst = []

    for num in nums:
        hash_map[num] += 1
    hash_lst = sorted(hash_map.items(), key=lambda x: x[1], reverse=True)
    hash_lst.reverse()
#This section creates a list of frequencies for each type of number in the list
    key_lst = []
    for key in hash_lst:
        key_lst.append(key[1])
    interval_map = {x:0 for x in key_lst}
    for num in key_lst:
        interval_map[num] += 1
#This section initializes an array based on the number of frequencies
    array_lst = []
    for num in interval_map:
        array_lst.append([])
#This section appends numbers into their corresponding frequencies
    i = 0
    j = 0
    for tup in hash_lst:
        array_lst[i].append(tup[0])
        if j+1 != len(key_lst):
            if key_lst[j] != key_lst[j+1]:
                i += 1
        j += 1
    k = 0
#array_lst at this point looks like [[5, -1],[4, -6],[1]]

#This section multiplies each number in each array based on the frequency it is grouped in
    for interval in interval_map:
        array_lst[k] = np.repeat(array_lst[k], interval)
        k+=1
    result_lst = []
    for array in array_lst:
        result_lst.append(sorted(list(array), reverse=True))
    return result_lst

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've got a function called frequency, which stores the occurrence of each element in a dictionary such that element:occurrence. I use get_output to format it properly by finding the length of the individual elements of the list and grouping them together (by using + operator).

lst = [-1,1,-6,4,5,-6,1,4,1]
def f(r):         #to check if elements in the list are same
    if r.count(r[0])==len(r):
        return True
        

d={}
megalist=[]
def frequency(x):
    for i in x:
        d[i]=x.count(i)
    
for j in d:
    n=d[j]
    if n in d.values():
        megalist.append([j for k in range(d[j])])  

frequency(lst)
def get_output(y):
    for a in y:
        for b in y:
            if a!=b:
                if len(a)==len(b)and f(a) ==True and f(b)==True:
                    y[y.index(a)]=a+b
                    y.remove(b)
    print(y)
get_output(megalist)    
        

output:

[[-1, 5], [1, 1, 1], [-6, -6, 4, 4]]

UPDATE:

For this,

the order of each frequency group must be ordered from greatest number to least input

You could run a for loop and sort the individual elements of the list (using "list".sort(reverse=True))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...