简体   繁体   English

如何将字符串附加到浮点列表

[英]How to append a string to a float list

I have following list 我有以下清单

[163.0, 171.0, 401.0]

And I would like to covert this list into following format 我想将此列表转换为以下格式

[['category_a', 163.0], ['category_b', 171.0], ['chiplet_name', 401.0]]

I have the following data to help to achieve the goal: 我有以下数据来帮助实现目标:

print (float(r[cr]))  --> Which will print all the integer values (ex: 163.0)
print r[c] -->  Which will print all the string values (ex: category_a)

I tried the append function to achieve my goal as follows: 我尝试了append函数来实现我的目标如下:

a= (float(r[cr]))

b= r[c]

print r[c], (float(r[cr]))

Printed chiplet_name 401.0 印刷的chiplet_name 401.0

[['category_a', 163.0], ['category_b', 171.0], ['chiplet_name', 401.0]]

You could use zip() in a list comprehension: 你可以在列表理解中使用zip()

A = ['category_a', 'category_b', 'chiplet_name']
B = [163.0, 171.0, 401.0]

answer = [[x, y] for x, y in zip(A, B)]
print(answer)

Output 产量

[['category_a', 163.0], ['category_b', 171.0], ['chiplet_name', 401.0]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM