我正在尝试用2个变量理解一行中的两个循环,但是它总是返回一个变量,而且我似乎不明白其背后的原因。我的代码如下:
text = ['hello, hi', 'goodbye, bye', 'how do you do, howdy']
mapped = {x:y for string in text for x, y in string.split(',')}
我得到的错误:
ValueError:太多值无法解包(预期2)
`
如何调整行,使其返回2个变量而不是1个?还是只是不可能?
我了解扩展后的外观如下:
for string in text:
for x, y in string.split(','):
mapped[x] = y
我不明白我要去哪里错了。
参考方案
仔细查看您真正想要的操作顺序-我认为您只是缺少一些括号:
text = ['hello, hi', 'goodbye, bye', 'how do you do, howdy']
mapped = {x:y for x, y in [string.split(',') for string in text]}
为我工作。
在返回'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…
python JSON对象必须是str,bytes或bytearray,而不是'dict - python在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…
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…