【发布时间】:2018-01-31 16:18:53
【问题描述】:
import io
f = io.StringIO('''130,266.07
46,174.14
169,187.01
179,488.69
53,401.53
128,106.88
97,398.33
152,493.87
20,205.43
94,248.14''')
x=0
y=0
for line in f:
x +=1
alist=x*[0]
for i in range(x):
alist[i]=[]
f.close()
g= io.StringIO('''130,266.07
46,174.14
169,187.01
179,488.69
53,401.53
128,106.88
97,398.33
152,493.87
20,205.43
94,248.14''')
for line in g:
s=line.split(",")
alist[y].append(int(s[0]))
alist[y].append(float(s[1]))
y +=1
def printlist():
h=0
while h <x :
blist=alist[h]
h += 1
print(str(blist[0])+" Kms, "+",$"+str(blist[1]))
for index in range(x):
value=alist[index]
j = index-1
while j>=0:
if value < alist[j]:
alist[j+1] = alist[j]
alist[j] = value
j -= 1
else:
break
printlist()
问题是输入值类似于[[a, b], [c, d], ...],现在它会根据第一个元素(例如a 或c)而不是第二个元素(@987654325)对其进行排序@ 和 d) 我需要按 _the 第二个值对其进行排序。如果每个列表,输出将不是关于第一个元素的排序数据,但是我需要对每个列表的第二个值进行排序,这也是一个浮点数,不能与整数进行比较。谢谢
【问题讨论】:
-
您能否为此输入提供所需的输出?
-
这是python2吗?因为否则比较列表会抛出错误。
-
我正在寻找按第二个元素排序的输出,但我得到的是按第一个元素排序的。
-
不,我使用的是 python 3.5
-
我可以建议:
sorted(a_list,key = lambda x:x[1])吗?
标签: python list sorting linked-list