0

I have the following 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]

Is there a way I can convert it into a string and write it in a filename.xml form where filename is these valriables along with their values?

They can be seperated b an underscore if comma isnt allowed

4
  • ",".join(YourList) or "_".join(YourList)? Commented Jul 11, 2018 at 13:17
  • That adds a comma between every character of each element. Commented Jul 11, 2018 at 13:19
  • Can you post expected output? Commented Jul 11, 2018 at 13:20
  • Of the form variablename1_<value>-variablename2_<val>-....-variablename_N_<value>.xml Commented Jul 11, 2018 at 13:22

2 Answers 2

1

You can use zip with list slicing.

Ex:

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)

Output:

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
Sign up to request clarification or add additional context in comments.

1 Comment

I got it. Thanks a lot!
0

Sorry but your question it is not so clear. If I get what you meant with you will be okay:

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(_)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.