因此,我试图从以特定格式制作的字典列表中提取信息,但是为了我的生命,我无法创建一个循环来从所需的确切位置提取信息,它不会打印任何内容,但不会收到任何错误。这是一个例子:
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']))
有人可以帮助我显示一种使该循环弹出的相关值为[9, 3, 2]
的方法吗?
我也认为格式化的dog_cur
变量不起作用...还有另一种方法来格式化该变量,使其等于与f'Animal.{Global.cur_word}'
相同的结果。我相信,如果您看一下我的循环,您将能够看到我试图从字典中的列表中拉出值。在我的实际程序中,全局值不断变化,所以这就是为什么我需要一个可以找到这些值的循环的原因。帮助将不胜感激!谢谢
参考方案
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())
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python ThreadPoolExecutor抑制异常 - pythonfrom concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…
如何用'-'解析字符串到节点js本地脚本? - python我正在使用本地节点js脚本来处理字符串。我陷入了将'-'字符串解析为本地节点js脚本的问题。render.js:#! /usr/bin/env -S node -r esm let argv = require('yargs') .usage('$0 [string]') .argv; console.log(argv…