编辑:我已更新图形以尝试解释我的目标。
答案 0 :(得分:4)
new_list = range(len(old_list))
答案 1 :(得分:1)
注意:由于list是关键字,因此您不应将变量列表命名。 enumerate(oldlist)
是一个生成器,它从oldlist中生成索引和列表元素的元组。此列表推导将生成索引的所有值,并将它们存储在newlist
中。如果您需要元素和索引,这是一个很好的方法。如果您有很多较大的列表,建议的范围(len(thelist))方法要快得多。
newlist = [i for i,_ in enumerate(oldlist)]
答案 2 :(得分:1)
检查一下......
In [24]: value = [17, 18, 19, 0, 1]
In [25]: valueIndex = range(len(value))
In [26]: valueIndex
Out[26]: [0, 1, 2, 3, 4]
# Get the valueRemapped in float format
In [27]: valueRemapped = [float(x)/valueIndex[-1] if x > 0 else x for x in valueIndex]
In [28]: valueRemapped
Out[28]: [0, 0.25, 0.5, 0.75, 1.0]
# Zip the two lists
In [29]: tempresult = zip(value, valueRemapped)
In [30]: tempresult
Out[30]: [(17, 0), (18, 0.25), (19, 0.5), (0, 0.75), (1, 1.0)]
# Sort the lists based on value list
In [31]: tempresult.sort(key=operator.itemgetter(0))
In [32]: tempresult
Out[32]: [(0, 0.75), (1, 1.0), (17, 0), (18, 0.25), (19, 0.5)]
# print the corresponding data
In [33]: [x[1] for x in tempresult]
Out[33]: [0.75, 1.0, 0, 0.25, 0.5]
答案 3 :(得分:0)
In [11]: lst = [17,18,19,0,1]
In [12]: p = [i/(len(lst)-1.) for i in range(len(lst))] # [0.0, 0.25, 0.5, 0.75, 1.0]
In [13]: [p[i] for i in sorted(range(len(lst)), key=lst.__getitem__)]
Out[13]: [0.75, 1.0, 0.0, 0.25, 0.5]
答案 4 :(得分:0)
我对我认为问题的解释是:
value = [17, 18, 19, 0, 1]
value_index = range(len(value))
value_remapped = map( lambda x : float(x) / 4.0, value_index)
paired = zip(value, value_remapped)
paired_sort = sorted(paired)
value_sort, value_remapped_sort = zip(*paired_sort)
print "value=", value
print "value_remapped=", value_remapped
print "paired=", paired
print "paired_sort=", paired_sort
print "value_sort=", value_sort
print "value_remapped_sort=", value_remapped_sort
给出了:
value= [17, 18, 19, 0, 1]
value_remapped= [0.0, 0.25, 0.5, 0.75, 1.0]
paired= [(17, 0.0), (18, 0.25), (19, 0.5), (0, 0.75), (1, 1.0)]
paired_sort= [(0, 0.75), (1, 1.0), (17, 0.0), (18, 0.25), (19, 0.5)]
value_sort= (0, 1, 17, 18, 19)
value_remapped_sort= (0.75, 1.0, 0.0, 0.25, 0.5)