1
json_obj = response.json()
az = []
for item in json_obj['DATA']['data']:
az.append(item['name'])
print (az)
第1号代码结果是这个
['Australia', 'CHINA', 'Combodia', 'EUROPE', 'HONG KONG', 'INDIA',
'INDONESIA', 'JAPAN', 'KOREA', 'MALAYSIA', 'Myanmar', 'New Zealand',
'PHILIPPINES', 'Russia', 'senchilles', 'SINGAPORE', 'Sri Lanka', 'TAIWAN',
'TestCountry', 'THAILAND', 'UNITED KINGDOM', 'USA', 'Vietnam', 'XY Test
Country']
然后我添加此代码
2
for i in az:
print(az.index(i) +1, end=' ')
print(" ",i)
,结果代码2就像这样
1 Australia
2 CHINA
3 Combodia
4 EUROPE
5 HONG KONG
6 INDIA
7 INDONESIA
8 JAPAN
9 KOREA
10 MALAYSIA
11 Myanmar
12 New Zealand
13 PHILIPPINES
14 Russia
15 senchilles
16 SINGAPORE
17 Sri Lanka
18 TAIWAN
19 TestCountry
20 THAILAND
21 UNITED KINGDOM
22 USA
23 Vietnam
24 XY Test Country
我的问题是如何将代码2的结果插入到一个变量中,以便我可以像下面一样调用'az'列表
reply = az
response.status = falcon.HTTP_200
response.body = self.format_message(message,reply)
答案 0 :(得分:1)
az = ['Australia', 'CHINA', 'Combodia', 'EUROPE', 'HONG KONG', 'INDIA',
'INDONESIA', 'JAPAN', 'KOREA', 'MALAYSIA', 'Myanmar', 'New Zealand',
'PHILIPPINES', 'Russia', 'senchilles', 'SINGAPORE', 'Sri Lanka', 'TAIWAN',
'TestCountry', 'THAILAND', 'UNITED KINGDOM', 'USA', 'Vietnam', 'XY Test
Country']
s='\n'.join("{}\t{}".format(*x) for x in enumerate(az, start=1))
print(s)
1 Australia
2 CHINA
3 Combodia
4 EUROPE
5 HONG KONG
6 INDIA
7 INDONESIA
8 JAPAN
9 KOREA
10 MALAYSIA
11 Myanmar
12 New Zealand
13 PHILIPPINES
14 Russia
15 senchilles
16 SINGAPORE
17 Sri Lanka
18 TAIWAN
19 TestCountry
20 THAILAND
21 UNITED KINGDOM
22 USA
23 Vietnam
24 XY Test Country
答案 1 :(得分:0)
字符串连接可以用于此。
x = ''
for i in az:
x += str(az.index(i) + 1)+'\t'+i+'\n'
print(x)
x拥有24个国家的完整字符串。