迭代列出的嵌套来获取键值对

时间:2017-06-01 10:14:46

标签: python

我真的很难为这个问题编写代码:

我的数据如下:

all_records = {u'ResourceRecordSets': [{u'Name': 'dev1.abc.com.',
                                        u'ResourceRecords': [{u'Value': '10.0.3.214'}],
                                        u'TTL': 300,
                                        u'Type': 'A'},
                                       {u'Name': 'dev2.abc.com',
                                        u'ResourceRecords': [{u'Value': '10.0.3.67'}],
                                        u'TTL': 300,
                                        u'Type': 'A'}]}

我希望遍历此列表以在名称为dev1.abc.com

时查找值

我不想定义一个新函数,我只想在几个for循环中执行此操作。

6 个答案:

答案 0 :(得分:2)

这应该有用,也许你想要以不同的方式格式化值:

desired_name = 'dev1.abc.com'
values = []
for resource in all_records['ResourceRecordSets']:
    if resource['Name'] != desired_name:
        continue
    values = [record['Value'] for record in resource['ResourceRecords']]
    break

print(values)

相同的逻辑,但@ quamrana的建议:

desired_name = 'dev1.abc.com'
values = []
for resource in all_records['ResourceRecordSets']:
    if resource['Name'] == desired_name:
        values = [record['Value'] for record in resource['ResourceRecords']]
        break

print(values)

答案 1 :(得分:2)

尝试列表理解:

level=1

all_records = {u'ResourceRecordSets': [{u'Name': 'dev1.abc.com.', u'ResourceRecords': [{u'Value': '10.0.3.214'}], u'TTL': 300, u'Type': 'A'}, {u'Name': 'dev2.abc.com', u'ResourceRecords': [{u'Value': '10.0.3.67'}], u'TTL': 300, u'Type': 'A'}]} value = [e['ResourceRecords'][0]['Value'] for e in all_records['ResourceRecordSets'] if e['Name']=="dev1.abc.com."] value 将是:

value

答案 2 :(得分:0)

我不知道这是不是一个错字,但你实际上在dev1.abc.com.

结束时有一个点
all_records = {u'ResourceRecordSets': [{u'ResourceRecords': [{u'Value': '10.0.3.214'}], u'Type': 'A', u'Name': 'dev1.abc.com.', u'TTL': 300}, {u'ResourceRecords': [{u'Value': '10.0.3.67'}], u'Type': 'A', u'Name': 'dev2.abc.com', u'TTL': 300}]}

name = 'dev1.abc.com.'
for x in all_records['ResourceRecordSets']:
    if x['Name'] == name:
        value = x['ResourceRecords'][0]['Value']

print(value)
# 10.0.3.214

答案 3 :(得分:0)

这简单得多。不需要休息声明。

for i in range(len(all_records['ResourceRecordSets'])): if(all_records['ResourceRecordSets'][i]['Name']== 'dev1.abc.com.'): print all_records['ResourceRecordSets'][i]['ResourceRecords'][0]['Value']

答案 4 :(得分:0)

In [10]: new = list(filter(lambda x: x[u'Name'] == 'dev1.abc.com.', all_records[u'ResourceRecordSets']))[0]['ResourceRecords']

In [11]: new
Out[11]: [{'Value': '10.0.3.214'}]

答案 5 :(得分:-1)

如果您想使用高级功能:

  • 假设"Value"内可能有多个"ResourceRecords"词典,并且可能会发生多次"Name"次。

    records = {u'ResourceRecordSets': [{u'ResourceRecords': [{u'Value': '10.0.3.214'}], u'Type': 'A', u'Name': 'dev1.abc.com.', u'TTL': 300}, {u'ResourceRecords': [{u'Value': '10.0.3.67'}], u'Type': 'A', u'Name': 'dev2.abc.com', u'TTL': 300}]}
    
    name_to_find = "dev1.abc.com."
    return_values = map(lambda sub_dict: [sub_list["Value"] for sub_list in sub_dict["ResourceRecords"]],
                        filter(lambda sub_dict: sub_dict["Name"] == name_to_find,
                               records["ResourceRecordSets"]))
    
    print(list(return_dicts))
    
  • 如果"Value"内只有一个"ResourceRecords"字典,并且可能会有多个"Name"次出现。

    records = {u'ResourceRecordSets': [{u'ResourceRecords': [{u'Value': '10.0.3.214'}], u'Type': 'A', u'Name': 'dev1.abc.com.', u'TTL': 300}, {u'ResourceRecords': [{u'Value': '10.0.3.67'}], u'Type': 'A', u'Name': 'dev2.abc.com', u'TTL': 300}]}
    
    name_to_find = "dev1.abc.com."
    return_values = map(lambda sub_dict: sub_dict["ResourceRecords"][0]["Value"],
                        filter(lambda sub_dict: sub_dict["Name"] == name_to_find,
                               records["ResourceRecordSets"]))
    
    print(list(return_dicts))
    
  • 如果只有"Name"次出现,您最好使用带有休息的for each循环:

    records = {u'ResourceRecordSets': [{u'ResourceRecords': [{u'Value': '10.0.3.214'}], u'Type': 'A', u'Name': 'dev1.abc.com.', u'TTL': 300}, {u'ResourceRecords': [{u'Value': '10.0.3.67'}], u'Type': 'A', u'Name': 'dev2.abc.com', u'TTL': 300}]}
    
    name_to_find = 'dev1.abc.com.'
    return_values = []
    for sub_dict in records['ResourceRecordSets']:
        if sub_dict['Name'] == name_to_find:
            return_values = [record['Value'] for record in sub_dict['ResourceRecords']]
            break