【问题标题】:Percentage Change in Json DictionaryJson 字典中的百分比变化
【发布时间】:2018-05-31 19:08:14
【问题描述】:

我正在学习 python,我想了解如何分配 升序和降序到2个不同的值 变量。

在这种情况下,我将使用两个变量,其中一个获胜次数最多 获胜率最低的另一个获得百分比变化:

api 列表内容如下:

[
  {
    "team_id": "FALCONS",
    "time_data": "2018-05-06T15:25:36.9524691Z",
    "wins": 11,
    "crow_size": 19563,
    "last_game": {
      "time_data": "2018-05-06T15:17:16.0000000Z",
      "data": "4e7a-bf0c-a97e5f32f58b",
      "take_side": "NA"
    }
  },
  {
    "team_id": "CARDINALS",
    "time_data": "2018-05-06T15:25:36.9212380Z",
    "wins": 14,
    "crow_size": 26000,
    "last_game": {
      "time_data": "2018-05-06T15:25:25.6850000Z",
      "data": "42ec-8326-445a22ea460f",
      "take_side": "NA"
    }
  },
  {
    "team_id": "BUFFALO",
    "time_data": "2018-05-06T15:25:36.7961978Z",
    "wins": 16,
    "crow_size": 34200,
    "last_game": {
      "time_data": "2018-05-06T15:07:50.0000000Z",
      "data": "44b7-86c1-6b969fccbe2e",
      "take_side": "NA"
    }
  },
  {
    "team_id": "COWBOYS",
    "time_data": "2018-05-06T15:25:36.9524691Z",
    "wins": 21,
    "crow_size": 15563,
    "last_game": {
      "time_data": "2018-05-06T15:17:16.0000000Z",
      "data": "4e7a-bf0c-a97e5f32f58b",
      "take_side": "NA"
    }
  },
  {
    "team_id": "DETROIT-LIONS",
    "time_data": "2018-05-06T15:25:36.9212380Z",
    "wins": 17,
    "crow_size": 23000,
    "last_game": {
      "time_data": "2018-05-06T15:25:25.6850000Z",
      "data": "42ec-8326-445a22ea460f",
      "take_side": "NA"
    }
  },
  {
    "team_id": "DOLPHINS",
    "time_data": "2018-05-06T15:25:36.7961978Z",
    "wins": 22,
    "crow_size": 24200,
    "last_game": {
      "time_data": "2018-05-06T15:07:50.0000000Z",
      "data": "44b7-86c1-6b969fccbe2e",
      "take_side": "NA"
    }
  }
]

这是一个非常大的列表,所以我只想要特定数量的团队 不是全部,所以我可以做我的百分比变化数学

import requests
import json
import time
import datetime
import heapq
from pprint import pprint
from operator import itemgetter


url = 'api url'
data = requests.get(url)
data = json.loads(data.content)

data.sort(key=operator.itemgetter('wins'), reverse=True)


lookup = ["CARDINALS", "BUFFALO", "DOLPHINS", "COWBOYS"]



for item in data:
    teamname = item["team_id"]
    winstreak = item["wins"]
    for ex in lookup:
        if ex == teamname:
            print(teamname, winstreak)

到目前为止,我能做的只是打印出键和值 用上面的公式,谁能帮我得到百分比变化 从最高的胜利到最低的胜利?

【问题讨论】:

  • 在任何团队词典中都没有 score 键。您是指与wins 键关联的值吗?
  • 是的,得分意味着获胜,对此感到抱歉
  • 对于问题中的示例值,“最高胜率与最低胜率的百分比变化”的输出是什么?
  • @JrDavid,我想为您最近提出的一个问题提出一个可能的答案,该问题使用 lomond 包被删除,您提出的“Json 字典中的 Python 循环”已被删除。不知道你为什么删除它,或者你可能已经让它工作了,但这是你的代码的一个可能的工作版本:repl.it/@downshift/CircularTomatoDecimal。不确定是否产生您想要的输出。只是想我会和你分享。

标签: python json python-3.x list dictionary


【解决方案1】:

根据您的团队范围,您可以使用minmax

import json
lookup = ["CARDINALS", "BUFFALO", "DOLPHINS", "COWBOYS"]
url = 'api url'
data = requests.get(url)
team_data = json.loads(data.content)
new_teams = [i for i in team_data if i['team_id'] in lookup]
min_win = min(new_teams, key=lambda x:x['wins'])['wins']
max_win = max(new_teams, key=lambda x:x['wins'])['wins']
change_win = (max_win-min_win)/float(min_win+max_win)

【讨论】:

    【解决方案2】:
    from __future__ import division # for python 2
    
    hWin = None
    for item in data:
        teamname = item["team_id"]
        winstreak = item["wins"]
        if teamname in lookup:
            if not hWin:
                hWin = winstreak
            percentChange = (hWin - winstreak) / hWin * 100
            print(teamname, winstreak,  '%i%%' % percentChange)
    

    结果

    ('DOLPHINS', 22, '0%')
    ('COWBOYS', 21, '4%')
    ('BUFFALO', 16, '27%')
    ('CARDINALS', 14, '36%')
    

    【讨论】:

      【解决方案3】:

      这样的事情可能会有所帮助:

      lookup = ["CARDINALS", "BUFFALO", "DOLPHINS", "COWBOYS"]
      
      wins = {}  # dictionary for team wins
      
      for item in data:
          teamname = item["team_id"]
          winstreak = item["wins"]
          for ex in lookup:
              if ex == teamname:
                  wins.update({teamname: winstreak}) # add team wins to dictionary
                  print(teamname, winstreak)
      
      low = min(wins.values())   # minimum of team wins
      high = max(wins.values())  # maximum of team wins
      print('low: {}, high: {}'.format(low, high))
      print('percent difference: {}'.format(float(high-low)/low)) # print the percentage
      

      输出:

      CARDINALS 14
      BUFFALO 16
      COWBOYS 21
      DOLPHINS 22
      low: 14, high: 22
      percent difference: 0.571428571429
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 1970-01-01
        相关资源
        最近更新 更多