将列表元素串联到python中的字符串中 - python

我有三个列表,我想串联每个列表中的每个元素。

这是我尝试过的:

quantity_list = ['6','4',7]
product_list = ['Apple','Orange','Grape']
uom_list = ['kg','kg','kg']
order_list = ""
order_list_temp = []
for ind,product in enumerate(product_list):
    order_list_temp.append(product + "-"+quantity_list[ind])
for ind,uom in enumerate(uom_list):
    order_list += order_list_temp[ind]+uom+"\n"

预期的输出是:

Apple-6kg
Orange-4kg
Grape-7kg

它按我的预期工作,但我想知道还有其他更好的解决方案。

参考方案

使用列表理解:

order_list = '\n'.join(
    [ "{}-{}{}".format(p, q, u) for p, q, u in zip(product_list, quantity_list, uom_list) ]
)

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。