简体   繁体   English

如何从函数返回字典?

[英]How do I return a dict from a function?

I have a small piece of code : 我有一小段代码:

def extract_nodes():
    for i in json.load(open('/home/ubuntu/slcakbot_openNMS/CLEAR/out.txt'))["node"]:
        try:
          socket.inet_aton(i["label"])
          print(i["label"])
          print(i["id"])
          #return { 'ip': i["label"], 'id': i["id"]}  #  i need to return these values

        except Exception as e:
          pass

I need to create a dict and return it to the calling function, i am not sure how do i create a dict and return from here. 我需要创建一个字典并将其返回给调用函数,我不确定如何创建字典并从此处返回。 Also once returned how do i use the dictionary value 也一旦返回我如何使用字典值

There may be more than one values for the key "id" and "label",so you should consider use list . 键“ id”和“ label”可能有多个值,因此您应考虑使用list。 Here is my code 这是我的代码

def extract_nodes():
    labels = []
    ids = []
    results = {}
    for i in json.load(open('/home/ubuntu/slcakbot_openNMS/CLEAR/out.txt'))["node"]:
        try:
          socket.inet_aton(i["label"])
          print(i["label"])
          labels.append(i["label"])
          print(i["id"])
          ids.append(i["id"])
          #return { 'ip': i["label"], 'id': i["id"]}  #  i need to return these values

        except Exception as e:
          pass
    results['ip']=labels
    results['id']=ids
    return results

I hope it can work :) 我希望它可以工作:)

You could use a generator, but I'm guessing you are new to python and this will be simpler: 您可以使用一个生成器,但我想您是python的新手,这会更简单:

def extract_nodes():
    return_data = dict()
    for node_datum in json.load(open('/home/ubuntu/slcakbot_openNMS/CLEAR/out.txt'))["node"]:
        try:
          socket.inet_aton(node_datum["label"])
          return_data[node_datum["id"]] = { 'ip': node_datum["label"], 'id': node_datum["id"]}
          print(node_datum["label"])
          print(node_datum["id"])
          #return { 'ip': node_datum["label"], 'id': node_datum["id"]}  #  i need to return these values

        except Exception as err:
            print err
            pass

    return return_data

As for using it, 至于使用它,

node_data = extract_nodes()
for key, node_details in node_data.items():
    print node_details['ip'], node_details['id']
def extract_nodes():
    to_return_dict = dict()
    for i in json.load(open('/home/ubuntu/slcakbot_openNMS/CLEAR/out.txt'))["node"]:
        try:
          socket.inet_aton(i["label"])
          to_return_dict[i['id']] = i['label']
          print(i["label"])
          print(i["id"])
          #return { 'ip': i["label"], 'id': i["id"]}  #  i need to return these values

        except Exception as e:
          pass
   return to_return_dict 

This should do it.... Let me know if it works! 应该这样做。...让我知道它是否有效!

Edit: 编辑:

As for how to use it: 至于如何使用它:

id_label_dict = extract_nodes()
print(id_label_dict['ip']) # should print the label associated with 'ip'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM