我正在建造一个不和谐的经济机器人,并试图创造一个领头羊。但我不知道如何获得钱包、银行和金库的合并值,以及链接到它的用户ID。我已经知道了如何按照最大净值到最小净值进行排序,但我也想不出如何获得链接到这些值的用户ID。用户ID最好是一个单独的变量,因为我需要将它转换为用户名。下面是我的JSON文件当前的结构。
{"theUserID": {"wallet": 0, "bank": 0, "vault": 0},"theUserID2": {"wallet": 0, "bank": 0, "vault": 0}}如果需要,这是我的代码,寻找最大的钱包数量。
jsonFilePath = '/the/file/path'
with open(jsonFilePath, 'r') as jsonFile:
data = json.load(jsonFile)
theList = []
for key, value in data.items():
wallet = value["wallet"]
(theList.append(wallet))
final_list = []
for i in range(0, 5):
max1 = 0
for j in range(len(theList)):
if theList[j] > max1:
max1 = theList[j];
theList.remove(max1);
final_list.append(max1)
print(final_list)发布于 2022-05-14 16:18:19
简单的方法是用拥有数据的对象创建新的列表,然后比较钱包的值和排序。
theList = list()
for val in data:
theList.append(
{
'ID': val,
'wallet': data[val]['wallet'],
}
)
i = 0
while (i <= len(theList)-2):
if (theList[i]['wallet'] < theList[i+1]['wallet']):
theList[i], theList[i+1] = theList[i+1], theList[i]
if (i > 0):
i -= 1
else:
i += 1如果速度不是问题的话,排序算法并不重要。当然,以后还可以添加不同的字段:
for val in data:
theList.append(
{
'ID': val,
'wallet': data[val]['wallet'],
'bank': data[val]['bank'],
'vault': data[val]['vault']
}
)然后计算出用于比较所需数量的值。
您将以这种方式访问排序数据:
theList[0]['ID'] #gets the ID of a user with highest x valuehttps://stackoverflow.com/questions/72241751
复制相似问题