如何在有组织的集合中调用变量,以便在其他地方使用它?(Python)

如何在有组织的集合中调用变量,以便在其他地方使用它?(Python),python,tkinter,set,Python,Tkinter,Set,我正在做一个排行榜应用程序项目,我被卡在了地方的组织上。代码的工作原理是输入使用的数量,并将其记录到一个变量中,然后将该变量放入一个集合中并进行组织。但是我找不到一种方法,可以先拨打电话集中的第一个号码,然后在另一个地方使用。下面是一段代码 global one global two global three global four one = player1Entry.get() two = pl

我正在做一个排行榜应用程序项目,我被卡在了地方的组织上。代码的工作原理是输入使用的数量,并将其记录到一个变量中,然后将该变量放入一个集合中并进行组织。但是我找不到一种方法,可以先拨打电话集中的第一个号码,然后在另一个地方使用。下面是一段代码

        global one
        global two
        global three
        global four

        one = player1Entry.get()
        two = player2Entry.get()
        three = player3Entry.get()
        four = player4Entry.get()


        places = [one, two, three, four]
        places = sorted(places, reverse = True)

如果您只是对与玩家相关的值进行排序,则很难获得给定(例如最佳)值的实际玩家。相反,您可以将玩家编号的元组与这些值一起排序:

one   = (1, player1Entry.get())
two   = (2, player2Entry.get())
three = (3, player3Entry.get())
four  = (4, player4Entry.get())

places = [one, two, three, four]
places = sorted(places, key=lambda t: t[1], reverse=True)

也可以考虑使用<代码>列表<代码> >代码> PraveServer < /C>。即使你知道你只会有四个玩家(直到有一天你需要五个…),这可能会让很多地方的事情变得更简单

places = sorted([(i, e.get()) for i, e in enumerate(playerEntries, start=1)],
                key=lambda t: t[1], reverse=True)

如果我理解正确的话,在列表排序后,您将尝试跟踪变量的名称,以便知道玩家的排名顺序。您可以通过创建一个球员姓名列表来实现这一点,这样一个列表中球员姓名的索引与另一个列表中该球员的分数相同。然后,使用zip()对列表进行排序,并返回玩家的姓名

global one
global two
global three
global four

one = player1Entry.get()
two = player2Entry.get()
three = player3Entry.get()
four = player4Entry.get()

scores = [one, two, three, four]
players = ['player1', 'player2', 'player3', 'player4']
places = [player for score, player in sorted(zip(scores, players), reverse=True)]

问题描述令人困惑,因为您的代码片段并没有真正解释“player1Entry”实际上是什么。您还错误地使用了单词“set”

在任何情况下,下面的代码片段展示了如何将对象放入列表并对其进行排序

class Player():
    def __init__(self):
        pass

    def get(self):
        return 99

    def __lt__(self, other):
        # allows us to sort player objects.
        return self.get() < other.get()

one = Player()
one_pos = one.get()

two = Player()
two_pos = two.get()


places = [one, two]
places = sorted(places, reverse = True)

print(places) # [<__main__.Player object at 0x000002509B466548>, <__main__.Player object at 0x000002509B466708>]

# You could also do this:
places2 = [one_pos, two_pos]
places2 = sorted(places2)

print(places2) # [99, 99]
class Player():
定义初始化(自):
通过
def get(自我):
返回99
定义(自身、其他):
#允许我们对玩家对象进行排序。
返回self.get()
你的问题很难理解。你说呼叫第一个号码,但通常不呼叫号码。你在集合中说
,但你没有使用集合。