如何在python中的字典元素列表中访问字典?

时间:2019-04-03 14:33:04

标签: python-3.x

我有一个字典,其值是字典列表。例如-

dj={'101': [{'Name': 'vamc'}, {'House': 'yes'}, {'married': 'yes'}, {'car': '1'}], '102': [{'Name': 'suresh'}, {'House': 'no'}, {'married': 'yes'}, {'car': '0'}]}

我想访问id ='102'的汽车属性。我尝试过类似的方法来解决我的问题。

li=[]
dj={}

def indec():
    di1={}
    di2={}
    di3={}
    di4={}
    di1['Name']=input("Enter the Name")
    di2['House']=input("Enter the House status")
    di3['married']=input("Enter the married status")
    di4['car']=input("Enter no of cars")
    li=[di1,di2,di3,di4]
    return li


x=int(input("Enter How many values:"))
for i in range(x):
    y=input("Enter id")
    dj[y]=indec()

id=input("Enter the id whose no of cars do u want:")
print("No of cars are:",dj[id['car']])

任何更简单的解决方案将不胜感激。

3 个答案:

答案 0 :(得分:1)

@MaximGi提到的一种简单方法是使用OOP。这样会使您的生活更轻松。

但是,如果您坚持不使用OOP来解决此问题,则可以将词典列表转换为单个词典,因为它是单人的属性。

def convert_to_single_record(attributes):
    record = {}
    for attribute in attributes:
        record.update(attribute)
    return record

customer_record = {'101': [{'Name': 'vamc'},
                           {'House': 'yes'},
                           {'married': 'yes'},
                           {'car': '1'}],
                   '102': [{'Name': 'suresh'},
                           {'House': 'no'},
                           {'married': 'yes'},
                           {'car': '0'}]}

records = {}
for id, attributes in customer_record.items():
    records[id] = convert_to_single_record(attributes)

print(records['102']['car'])

答案 1 :(得分:1)

@MaximGi提到的一种面向对象的方法

#Class to encapsulate person
class Person:

  def __init__(self, id, name, house, married, car):
    self.id = id
    self.name = name
    self.house = house
    self.married = married
    self.car = car


li=[]

#Get input values from user
def indec():

    name=input("Enter the Name")
    house=input("Enter the House status")
    married=input("Enter the married status")
    car=input("Enter no of cars")
    return name, house, married, car

#In a for loop, create a person object and append it to list
x=int(input("Enter How many values:"))
for i in range(x):
    y=input("Enter id")
    p = Person(y, *indec())
    li.append(p)


id=input("Enter the id whose no of cars do u want:")
#Loop through person list and print car attribute for which the id matches
for p in li:
  if p.id == id:
    print("No of cars are:",p.car)

输出结果为

Enter How many values:2
Enter id101
Enter the Namevamc
Enter the House statusyes
Enter the married statusyes
Enter no of cars1
Enter id102
Enter the Namesuresh
Enter the House statusyes
Enter the married statusno
Enter no of cars0
Enter the id whose no of cars do u want:102
No of cars are: 0

答案 2 :(得分:0)

关于

to_search = 'car'
index = '102'

dj={'101': [{'Name': 'vamc'}, {'House': 'yes'}, {'married': 'yes'}, {'car': '1'}], '102': [{'Name': 'suresh'}, {'House': 'no'}, {'married': 'yes'}, {'car': '0'}]}

result_list = [ val for val in dj[index] if to_search in val ]

print result_list[0]

希望有帮助。