您的位置:首页 > 编程语言 > Python开发

把python 数组,量化为从1开始的数组

2014-09-09 15:11 155 查看
问题描述:

比方我有一个数组:a=[10,12,3,4,9]

我最终想要的是:[4,5,1,2,3]

就是按照大小将数组a中的元素转换为其对应的大小排序值。

思路:1,先用字典保存位置和值

2,按照值对字典进行排序

3,对排序后的值进行量化:量化为:1,2,3,4,5

4,在对tuple按照位置进行排序

5,导出排序后的值即可

dict={}
for i in xrange(len(FINAL_STATISTICS)):
dict.setdefault(i,FINAL_STATISTICS[i])
sorted_tuple=sorted(dict.iteritems(),key=lambda dict:dict[1]) #对字典按照key值进行排序

temp_list=[]
temp_count=1
for i in xrange(len(sorted_tuple)):
if i==0:
temp_list.append([sorted_tuple[i][0],temp_count])
#temp_count+=1
continue
if sorted_tuple[i][1]==sorted_tuple[i-1][1] :
temp_list.append([sorted_tuple[i][0],temp_count])
else :
temp_count+=1<span style="white-space:pre">					</span>#对tuple的值进行量化
temp_list.append([sorted_tuple[i][0],temp_count])

temp_list  =sorted(temp_list)

final_sorted=[]
for item in temp_list:
final_sorted.append(item[1])      #输出tuple的第二个元素
print final_sorted
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: