[英]How to combine two lists to get the following desired result containing tuples?
[英]how to get the following result
這些是索引:
Index= [2, 3, 4, 6]
這些是索引的頻率,這兩個數組與位置相關,例如數組索引的第一個元素是 2,並且頻率為 2,因為數組頻率的位置 2 的元素是 2。
Frequency=[2, 2, 2, 2, 2, 1, 1]
我需要獲得以下數組標簽:
labels=[2, 2, 3, 3, 4, 4, 6]
為了得到它,我做了以下代碼:
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]
有沒有其他形式可以優化這個過程?
假設頻率列表與 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.