1

编辑:我已经更新了图形以尝试解释我的目标。

4

5 回答 5

4

new_list = range(len(old_list))

于 2013-02-23T10:10:11.137 回答
1

注意:由于 list 是关键字,因此您不应命名变量列表。 enumerate(oldlist)是一个生成器,它从 oldlist 中生成索引元组和列表元素。此列表推导将产生索引的所有值并将它们存储在newlist. 如果您需要元素和索引,这是一个好方法。如果你有很多更大的列表,建议的 range(len(thelist)) 方法要快得多。

newlist = [i for i,_ in enumerate(oldlist)]
于 2013-02-23T10:06:51.217 回答
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]
于 2013-02-23T11:22:11.547 回答
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]
于 2013-02-23T11:15:44.190 回答
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)
于 2013-02-23T11:16:16.697 回答