I have a program that asks for the User to input a Name(eg Lisbon) and any other users that falls under that has the same Country as what the User Inputs(eg Lisbon) will be printed out (eg Jade, John). Here's my JSON file:
{
"user1":{
"Country":[
"China",
"USA",
"Nepal"
],
"Name":[
"Lisbon"
]
},
"user2":{
"Country":[
"Sweden",
"China",
"USA"
],
"Name":[
"Jade"
]
},
"user3":{
"Country":[
"India",
"China",
"USA"
],
"Name":[
"John"
]
}
}
For example, if the User Input's Lisbon, this will be the output:
Lisbon
Jade
John
I would like to have what the User Inputs removed from the Output so the expected results would be:
Jade
John
Here's what my code looks like:
def Country():
userName = raw_input("Enter user's name: ")
with open('listOfUsers.json') as f:
data = json.load(f)
def getId(name):
for userId, v in data.items():
if v['Name'][0] == name:
return userId;
id = getId(userName)
for k, v in data.items():
if any(x in data[id]['Country'] for x in v['Country']):
print (v['Name'][0])
The following worked for me
def Country():
userName = raw_input("Enter user's name: ")
with open('listOfUsers.json') as f:
data = json.load(f)
def getId(name):
for userId, v in data.items():
if v['Name'][0] == name:
return userId;
id = getId(userName)
for k, v in data.items():
if any(x in data[id]['Country'] for x in v['Country']):
if v['Name'][0] != userName:
print (v['Name'][0])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.