【发布时间】:2014-12-10 21:22:39
【问题描述】:
我有一个这样的列表:
self.thislist = ['Name', 13, 'Name1', 160, 'Name2', 394]
基本上,列表后面有一个名称和一个数字,我试图找出列表中最大的数字,即394。但由于某种原因,它选择了一个名称为this。
if max(self.thislist) > 150:
this = max(self.thislist) # so this should be 394
position = self.thislist.index(this) # The index of it
temponary = position - 1 # This is so we can find the name that is associated with it
name = self.thislist[temponary] #and this retrieves the name
它检索例如'Name',它应该是394。
所以关键是要检索一个名称和一个与该名称关联的数字。有什么想法吗?
【问题讨论】:
-
您为什么要使用列表来显示映射?
self.thisdict = { 'Name': 13, 'Name1': 160, 'Name2': 394 }。然后name = max(self.thisdict, key=thisdict.get). -
我不知道,真的。我从未研究过python或任何语言,所以我不知道什么是执行什么功能的最佳方法。所以在这种情况下,我只是决定使用列表。另一个函数将名称和值附加到列表中,并且该函数正确读取它。它有效,这对我来说已经足够了。不过,我会查找映射。谢谢。
标签: python string list int max