将两个变量转换为1个变量

时间:2018-07-03 20:28:02

标签: python python-3.x

我有两个变量,我想使1个变量变。

import requests
from pprint import pprint

req = requests.get('https://www.fantasylabs.com/api/sportevents/4/02_03_2018/team-ratings/')
data = req.json()

for event in data:
    ed = event['EventDetails']
    prop = ed['Properties']
    home = prop['HomeTeamShort']
    away = prop['VisitorTeamShort']
    teams = home, away

    print(home,away)

结果:

PHI OTT
MTL ANA
WPG COL
NYI CLB
NJD PIT
FLA DET
BUF STL
BOS TOR
NAS NYR
DAL MIN
CGY CHI
VAN TB
LA ARI

在上面带有teams变量的情况下,我想将其设置为1.如何使两个名称都在teams变量中?

3 个答案:

答案 0 :(得分:4)

teams的形状如下:(home, away)

您可以使用join语句将team的元素打印为单个字符串:

print(" ".join(teams))

>>>PHI OTT

答案 1 :(得分:2)

我会为层次结构创建一个字典。

teams = {"home": home, "away": away}

这样,您可以使用相同的变量来引用它们,但它们保持其独特性。

print(teams["home"] + " vs. " + teams["away"])

答案 2 :(得分:1)

如果您只想连接两个值,而不是分别访问它们,那么您可以说

teams = home + ' ' + away