[英]How do i concatenate and cast a int to a string in python?
我无法弄清楚将字符串连接并转换为以下代码部分的语法
new_prod_list2 = list(map(lambda x:x str(['product_id'])) + x['product_category'] + x['product_name'] , products))
print(new_prod_list2)
Product_category 和 product_name 连接,但是当我尝试使用 str 连接 product_id(它是一个 int)以将其转换为字符串时,我不断收到错误。
有人介意帮我解决这个问题吗,我将不胜感激。
将lambda
表达式中冒号后面的x
带入str()
调用,以便调用['product_id']
项目访问:
new_prod_list2 = list(map(lambda x: str(x['product_id']) + x['product_category'] + x['product_name'] , products))
更 Pythonic 的方法是使用列表理解和字符串格式化:
new_prod_list2 = [f'{x["product_id"]}{x["product_category"]}{x["product_name"]}' for x in products]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.