So I am trying to pull information from a dictionary list that I made in a specific format but for the life of me I cannot create a loop that pulls the information from exactly where I need it at all, it prints nothing but I receive no errors. Here's an example:
class Global():
prv_word = 'dumb'
cur_word = 'dog'
nxt_word = 'head'
class Animal():
dog = [
{'head': {'funny': [8 , 7 , 1],'dumb': [9 , 3 , 2],'poofy': [18 , 4 , 11]}},
{'tail': {'funny': [12, 2 , 4], 'dumb': [3 , 9 , 7],'poofy':[28 , 5 , 60]}}]
dog_cur = f'Animal.{Global.cur_word}'
if hasattr(Animal, Global.cur_word):
for list in dog_cur:
if Global.nxt_word in list:
adj = Global.nxt_word
index = list.index(Global.nxt_word)
for lis in list:
if Global.prv_word in lis:
adj2 = Global.prv_word
index2 = lis.index(Global.prv)
end = dog_cur[index][adj][adj2]
print(end)
##### TROUBLESHOOT #####
##### This works! But how do I format the loop to generate this result? #####
(print(Animal.dog[0]['head']['dumb']))
can someone help show me a way to make this loop pop up with a relevant value of [9, 3, 2]
. I also don't think the formatted dog_cur
variable will work... Is there another way to format that variable to equal the same result as f'Animal.{Global.cur_word}'
I believe if you look at my loop, you'll be able to see I'm trying to pull the values from a list within a dictionary. In my actual program, the Global values are constantly changing so that's why I need a loop that can find those values. Help will be greatly appreciated! Thank you
Why not put that in dicts all the way?
class Animals():
animals = {
"dog" : {
'head': {'funny': [8 , 7 , 1], 'dumb': [9 , 3 , 2], 'poofy': [18 , 4 , 11]},
'tail': {'funny': [12, 2 , 4], 'dumb': [3 , 9 , 7], 'poofy': [28 , 5 , 60]}},
"cat" : { "head": {"mysterious": [1,1,1] }}}
@classmethod
def gett(cls, name, part_name = None, style_name = None):
"""Provide getter on inner dict. Returns None if any key given
is invalid. Allows to specify keys partly, to get more infos on
inner dict"""
if name is None:
raise ValueError("name can not be None")
try:
if part_name and style_name :
return cls.animals.get(name).get(part_name).get(style_name)
elif part_name:
return cls.animals.get(name).get(part_name)
else:
return cls.animals.get(name)
except:
return None
print(Animals.gett("dog","head","dumb"))
print(Animals.gett("cat","head","mysterious"))
print(Animals.gett("donkey","whatever","yeah"))
print(Animals.gett("dog"))
Output:
[9, 3, 2]
[1, 1, 1]
None
{'head': {'funny': [8, 7, 1], 'dumb': [9, 3, 2], 'poofy': [18, 4, 11]},
'tail': {'funny': [12, 2, 4], 'dumb': [3, 9, 7], 'poofy': [28, 5, 60]}}
I dont know if you want to put more effort in it, if so you could mimic a dict
: see How to "perfectly" override a dict?
class Global():
prv_word = 'dumb'
cur_word = 'dog'
nxt_word = 'head'
class Animal():
dog = [
{'head': {'funny': [8 , 7 , 1],'dumb': [9 , 3 , 2],'poofy': [18 , 4 , 11]}},
{'tail': {'funny': [12, 2 , 4], 'dumb': [3 , 9 , 7],'poofy':[28 , 5 , 60]}}]
def find():
animal_list = getattr(Animal,Global.cur_word, None)
if animal_list is None: return None
animal_part = next(filter(lambda pdct: Global.nxt_word in pdct, animal_list), None)
if animal_part is None: return None
if Global.nxt_word in animal_part and Global.prv_word in animal_part[Global.nxt_word]:
animal_part_data = animal_part[Global.nxt_word][Global.prv_word]
return animal_part_data
else:
return None
print(find())
I had to add another answer that used classes in an object-oriented fashion. This is the way I would go about setting up the structure of your solution.
class Selector(object):
def __init__(self, animal="", part="", descriptor=""):
super(Selector, self).__init__()
self.animal = animal
self.part = part
self.descriptor = descriptor
current_selector = Selector("dog", "head", "dumb")
class Animal(object):
def __init__(self, kind="", parts=[]):
super(Animal, self).__init__()
self.kind = kind
self.parts = parts
def find_part(self, part_name):
return next(filter(lambda p: p.name == part_name, self.parts), None)
class AnimalPart(object):
def __init__(self, name="", descriptors={}):
super(AnimalPart, self).__init__()
self.name = name
self.descriptors = descriptors
def find_descriptor(self, desc_name):
return self.descriptors[desc_name]
animals = {}
def init():
# create dog
dog = Animal("dog", [ AnimalPart('head', {'funny': [8 , 7 , 1],'dumb': [9 , 3 , 2],'poofy': [18 , 4 , 11]}),
AnimalPart('tail', {'funny': [12, 2 , 4], 'dumb': [3 , 9 , 7],'poofy':[28 , 5 , 60]})])
animals['dog'] = dog
def find():
sel = current_selector
return animals[sel.animal].find_part(sel.part).find_descriptor(sel.descriptor)
init()
print(find())
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.