如何使_ in _在函数中起作用

时间:2019-05-27 12:41:24

标签: python json

我正在使用将json列表与python文件中的列表进行比较的代码,但是for循环即时消息使用的类型会返回TypeError:“函数”对象不可迭代。我不确定该如何解决

我尝试将“乘员中的k2:”更改为“乘员中的k2 :():”和“乘员中k2:[:]:”),但是每一个都有问题

url = 'http://api.open-notify.org/astros.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
people = result["people"]
people_with_exist_state = []

occupants = [
{
    'name': "Oleg Kononenko",
    'bio': 'Oleg Dmitriyevich Kononenko is a Russian cosmonaut. He has flown to the International Space Station four times as a flight engineer and commander. Kononenko accumulated over 533 days in orbit during his first three long duration flights to ISS.',
    'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Kononenko.jpg/220px-Kononenko.jpg',
},  
{

    'name': "David Saint-Jacques",
    'bio': "David Saint-Jacques (born January 6, 1970) is a Canadian astronaut with the Canadian Space Agency (CSA). He is also an astrophysicist, engineer, and a physician.",
    'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/David_Saint-Jacques_official_portrait.jpg/440px-David_Saint-Jacques_official_portrait.jpg',
}, #countinues for all occupants of the ISS

@app.route("/occupants")
def occupants():
 for k in people:
  is_here = 0
  for k2 in occupants:
    if k['name'] == k2['name']:
      is_here = 1
  if is_here == 0:
     #name does not exist
     k['exist'] = 0
  else:
     # name exist
     k['exist'] = 1
  people_with_exist_state.append(k)
 return render_template('occupants.html', people=people_with_exist_state)

1 个答案:

答案 0 :(得分:2)

假设peopleoccupants是代码中的全局变量,则不能像命名变量def occupants():那样命名函数occupants