So I have this Json file which I’m using to extract and output the following inf
ID: 3908488 • Letter: S
Question
So I have this Json file which I’m using to extract and output the following info in a python program:
Output:
DEN-ORL
ORL-DEN
LAX-DEN
DEN-LAX
BOS-AUS
AUS-BOS
MIA-LAS
LAS-MIA
And so on..
Someone give me this answer:
Which is good for displaying all the cities but I need the reverse of each city also
displayed
for example:
DEN-ORL
ORL-DEN
What should be the correct code in python3 do it ?
{
"cities": [{
"name": ["DEN","ORL"],
"seats": [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
],
"price": 450
}, {
"name": ["ORL","LAG"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 500
}, {
"name": ["LAX","DEN"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 150
}, {
"name": ["BOS","AUS"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 500
}, {
"name": ["MIA","LAS"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 400
}, {
"name": ["MCI","CLT"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 340
}]
}
{
"cities": [{
"name": ["DEN","ORL"],
"seats": [
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]
],
"price": 450
}, {
"name": ["ORL","LAG"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 500
}, {
"name": ["LAX","DEN"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 150
}, {
"name": ["BOS","AUS"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 500
}, {
"name": ["MIA","LAS"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 400
}, {
"name": ["MCI","CLT"],
"seats": [
[0, 1],
[1, 0],
[0, 0],
[0, 1],
[0, 1],
[0, 0],
[0, 1],
[1, 1],
[1, 0],
[0, 0],
[0, 1],
[1, 1],
[1, 1],
[0, 0],
[1, 1],
[1, 0],
[0, 1],
[0, 0],
[0, 0],
[0, 0],
[1, 0],
[1, 1],
[1, 1],
[1, 0]
],
"price": 340
}]
}
Explanation / Answer
import json def main(): filename = input('Enter file name: ') cities = json.load(open(filename, 'r'))['cities'] for city in cities: print('-'.join(reversed(city['name']))) main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.