简体   繁体   中英

how to get the following result

these are the index:

Index= [2, 3, 4, 6]

these are the frequency of the index, this two arrays are related by the position for instance the first element of the array Index is 2 and has frequency 2 since the element of the position 2 of the array Frequency is 2.

Frequency=[2, 2, 2, 2, 2, 1, 1]

I need to get the following array labels:

labels=[2, 2, 3, 3, 4, 4, 6]

In order to get it I did the following code:

labels=[]

for index in Index:
    Counter=Frequency[index]
    for i in range(Counter):
        labels.append(index)

print(labels)

labels=[2, 2, 3, 3, 4, 4, 6]

are there any other form to optimize this process ?

Assuming the frecuency list is the same length as the TrainIndex list:

frecuency = [2,2,2,1]
TrainIndex = [9,4,5,8]
[g for sublist in [[i]*f for (i,f) in zip(TrainIndex,frecuency)] for g in sublist]

[9, 9, 4, 4, 5, 5, 8]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM