Python 如果没有给出预期结果,请列出
我有以下清单:Python 如果没有给出预期结果,请列出,python,list,sorting,if-statement,Python,List,Sorting,If Statement,我有以下清单: [['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]] 请注意,在索引5和索引6中,第二项是相同的。我试图把这个列表中的第二个项目,在每个列表排序成一个排名 我使用以下if-else来更正值相同时的时间: for i in range(len(sortedCounts)): if i == 0:
[['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]]
请注意,在索引5和索引6中,第二项是相同的。我试图把这个列表中的第二个项目,在每个列表排序成一个排名
我使用以下if-else来更正值相同时的时间:
for i in range(len(sortedCounts)):
if i == 0:
sortedCounts[i][1] = 0
elif sortedCounts[i][1] == sortedCounts[i-1][1]:
sortedCounts[i][1] = i-1
else:
sortedCounts[i][1] = i
[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 5], ['1804986', 6]]
然而,当我打印列表时,即使数字相同,我也会得到不同的排名:
for i in range(len(sortedCounts)):
if i == 0:
sortedCounts[i][1] = 0
elif sortedCounts[i][1] == sortedCounts[i-1][1]:
sortedCounts[i][1] = i-1
else:
sortedCounts[i][1] = i
[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 5], ['1804986', 6]]
预期产出为:
[['3', 0], ['2370447', 1], ['5300058', 2], ['81615', 3], ['3294332', 4], ['3078798', 4], ['1804986', 6]]
感谢您的建议,或者如果有更好的方法,请提供建议。我想这就是您所期望的代码
rank, last_value = -1, -1
for i, e in enumerate(sortedCount):
if last_value < e[1]:
rank = i
last_value = e[1]
sortedCount[i][1] = rank
sortedCounts=[['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]]
i=0
while i<len(sortedCounts):
if(sortedCounts[i][1]==sortedCounts[i+1][1]):
sortedCounts[i][1]=sortedCounts[i+1][1]=i
i=i+1
elif(i==len(sortedCounts)):
sortedCounts[i][1]=i
else:
sortedCounts[i][1]=i
i=i+1
按索引1排序,使用已排序的:
又短又甜
[3',0],'2370447',1],'5300058',2],'81615',3],'3294332',4],'3078798',5],'1804986',6]。使用内置排序功能可以轻松解决此问题
input_data = [['3', 2], ['2370447', 282], ['5300058', 610], ['81615', 615], ['3294332', 624], ['3078798', 624], ['1804986', 643]]
input_data.sort(key = lambda x: int(x[0]))
result = input_data.copy()
for i in range(len(result)):
result[i][1] = i
print(result)
"""
output
[['3', 0], ['81615', 1], ['1804986', 2], ['2370447', 3], ['3078798', 4], ['3294332', 5], ['5300058', 6]]
"""
据我所知,在5和6处的元素没有得到相同的索引的主要原因是代码没有进入elif语句,因为在更新列表时
with value
sortedCounts[i][1] = i-1
这会改变列表
因此,您可以创建一个变量来更改列表并将其附加到新列表中,而不是直接更改列表
newList = []
for i in range(len(sortedCounts)):
if i == 0:
variable = 0
elif sortedCounts[i][1] == sortedCounts[i-1][1]:
variable = i-1
else:
variable = i
newList.append([sortedCounts[i][0], variable])
预期的输出是什么?预期的输出是什么?抱歉-updatedsortedCounts[i][1]==sortedCounts[i-1][1]没有做您想做的事情,因为您在循环的上一次迭代中用i或i-1重写了该表达式的右侧。我现在明白您的意思了。我正在尝试一种解决方案,我只是将所有内容移动到一个新的列表中,但在索引方面遇到了困难。这几乎奏效了。我希望在最后一个列表中得到6分,但这个优雅的解决方案给了我5分:[[3',0],'2370447',1],'5300058',2],'81615',3],'3294332',4],'3078798',4],'1804986',5]:对不起,我错过了这一点。我编辑了我的答案,将rank+=1替换为rank=I。现在可能可以了。我在上面的笔记中做了一些类似的事情,但使用了另一个列表。您的代码几乎可以正常工作,[[3',0],'2370447',1],'5300058',2],'81615',3],'3294332',4],'3078798',4],'1804986',643]]但是列表中的最后一项没有改变:我有一些错误,因为它应该是I