如何使用Flask

时间:2016-12-21 18:37:00

标签: python

我最近发布了这个,但是我提交了另一个更新代码的问题(如果我不这样做,请告诉我)

我想知道如何打印两个相同尺寸的列表。

我从嵌套的词典中得到了这些列表。

以下是清单:

[
  [
    "Chelsea", 
    "Liverpool", 
    "ManCity", 
    "Arsenal", 
    "Spurs", 
    "ManU", 
    "Southampton", 
    "West Bromwich", 
    "Everton", 
    "Bournemouth", 
    "Stoke", 
    "Watford", 
    "West Ham", 
    "Middlesbrough", 
    "Foxes", 
    "Burnley", 
    "Crystal", 
    "Sunderland", 
    "Swans", 
    "Hull"
  ], 
  [
    43, 
    37, 
    36, 
    34, 
    33, 
    30, 
    24, 
    23, 
    23, 
    21, 
    21, 
    21, 
    19, 
    18, 
    17, 
    17, 
    15, 
    14, 
    12, 
    12
  ]
]

基本上,我希望列表为:

切尔西:44利物浦:37

等...

这是我的python代码:

from flask import Flask, jsonify, render_template, request
import httplib
import json

app = Flask(__name__)

@app.route('/')
def index1():
    return render_template('league.html')

@app.route('/League Standing', methods=['GET','POST'])
def show_league():
    text = request.form['league']
    connection = httplib.HTTPConnection('api.football-data.org')
    headers = {'X-Auth-Token': 'key', 'X-Response-Control': 'minified'}
    connection.request('GET', '/v1/competitions/'+text+'/leagueTable', None, headers)
    response = json.loads(connection.getresponse().read().decode())
    teamnames = [r['team'] for r in response['standing']]
    points = [r['points'] for r in response['standing']]
    for teamnames, points in zip(response):
        print('{}: {}'.format(teamnames, points))
    #return jsonify(teamnames, points)




if __name__ == '__main__':
    app.run(debug=True)

老实说,我现在不关心代码的格式 - 我会在它工作时解决这个问题!

我试过:

zip(teamname, points)

无济于事!

请帮忙!

编辑:

for t, p in zip(teamnames, points):
    return '{}: {}'.format(t,p)

此代码返回jsut第一个团队!

1 个答案:

答案 0 :(得分:0)

完全不同的方向,但为什么不试试::

results = [{'team': r['team'], 'points': r['points']} for r in response['standing']]

for r in results:
    print "{team}: {points}".format(r)