我有以下列表:'[TW', 6.5, 'TS', 5.0, 'core_h', 2.0, 'core_tand', 2.0, 'core_er', 2.0, 'trace_height', 5.0, 'trace_er', 5.0, 'trace_tand', 5.0, 'D', 5.0, 'prepreg_h', 14.0, 'prepreg_er', 14.0, 'prepreg_tand', 14.0, 'roughness_level', 22.0]
有没有一种方法可以将其转换为字符串并以filename.xml格式写入 这些变量及其值在哪里文件名?
如果不允许使用逗号,可以将它们用下划线分隔
答案 0 :(得分:1)
您可以将zip
用于列表切片。
例如:
s = ['TW', 6.5, 'TS', 5.0, 'core_h', 2.0, 'core_tand', 2.0, 'core_er', 2.0, 'trace_height', 5.0, 'trace_er', 5.0, 'trace_tand', 5.0, 'D', 5.0, 'prepreg_h', 14.0, 'prepreg_er', 14.0, 'prepreg_tand', 14.0, 'roughness_level', 22.0]
res = ""
for i,v in zip(s[::2], s[1::2]):
res += "{0}_{1}-".format(i, v)
print(res)
输出:
TW_6.5TS_5.0core_h_2.0core_tand_2.0core_er_2.0trace_height_5.0trace_er_5.0trace_tand_5.0D_5.0prepreg_h_14.0prepreg_er_14.0prepreg_tand_14.0roughness_level_22.0
答案 1 :(得分:0)
抱歉,您的问题还不清楚。如果我明白您的意思,您会没事的:
t
his_list = ['TW', 6.5, 'TS', 5.0, 'core_h', 2.0, 'core_tand', 2.0, 'core_er', 2.0, 'trace_height', 5.0, 'trace_er', 5.0, 'trace_tand', 5.0, 'D', 5.0, 'prepreg_h', 14.0, 'prepreg_er', 14.0, 'prepreg_tand', 14.0, 'roughness_level', 22.0]
my_string = ""
for _ in this_list :
my_string += str(_)