简体   繁体   English

如何得到以下结果

[英]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.这些是索引的频率,这两个数组与位置相关,例如数组索引的第一个元素是 2,并且频率为 2,因为数组频率的位置 2 的元素是 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:假设频率列表与 TrainIndex 列表的长度相同:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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