-1

I am new to python, i will re-edit and make it clear :) thanks for investing your time.

I have two different lists with string values (as shown below):

  1. A list with 1600 real numbers: ['-1,03E+01' '-1,26E+01' .......]

  2. Another list with 1600 imaginary values: [ '-1,25E+01' '-1,02E+01' .... ]

These are directly imported from touchstone file. so, i do not see letter j for imaginary. i don't know y.

  • First i have to change the type of the value from 'str' to float for further calculation.
  • Then append them into a list as complex value like [[-1,03E+01 -1,25E+01 j] [-1,26E+01-1,02E+01j].......]
K V
  • 13
  • 4
  • I don't get how the first description of values relates to the example. What do you mean with the "'-1,03E+01' '-1,26E+01'" and "'-1,25E+01' '-1,02E+01'"? Does the first have *real* (description) or *integer* (example) values? Does the second have *str type* or *complex type* values? – MisterMiyagi Mar 16 '22 at 15:13
  • "Note: all are str" Then please [edit] the question to provide an *accurate* example. Don't leave people guessing what is or isn't actually there. – MisterMiyagi Mar 16 '22 at 15:18
  • Please take the time to properly format your code. Use code formatting and make sure to provide valid literals. ``['-1,03E+01' '-1,26E+01' .......]`` isn't anything, and ``['-1,03E+01' '-1,26E+01']`` is very likely not the list you intended. – MisterMiyagi Mar 17 '22 at 08:30
  • okay will check it and try your method – K V Mar 17 '22 at 08:42

1 Answers1

2

Since you have edited your question to say that your numbers are actually represented by strings of float values (your second list of your second example of [7j, 8j, et.c] makes this very confusing, though):

l1 = ['-1,03E+01', '-1,26E+01']

l2 = ['-1,25E+01', '-1,02E+01']

[complex(float(a), float(b)) for a, b in zip(l1, l2)]

Original answer:

For two lists l1 containing the real values and l2 containing the imaginary values (as you describe them, but I think you mean coefficients):

[complex(a,b) for a, b in zip(l1, l2)] 

This answer will get you a list of complex values:

[(1+7j), (2+8j), (3+9j), (4+10j), (5+11j), (6+12j)]. 

As you indicated in your comment below, if you want it to be:

[ [(1+7j)] [(2+8j)] [(3+9j)] [(4+10j)] [(5+11j)] [(6+12j)] ] 

as indicated in your question, then change it to:

[[complex(a, b)] for a, b in zip(l1,l2)]
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
jonsca
  • 10,218
  • 26
  • 54
  • 62
  • thank for your answer. i would like to add one more error. my values are in this type -1,02E+01. But all of the values of these are in str type. so getting a error to use the answer u mention . TypeError: complex() can't take second arg if first is a string – K V Mar 16 '22 at 14:54
  • These reason everything was confusing because the str values has commas for numbers. The issue here was because, the regional format in my PC is in Germany so also imported as such. So had changed regional format and it worked. In germany for numbers we use ',' instead of '.' like 1.000 for 1,000 and 140,0 for 140.0. Answer: [complex(a,b) for a, b in zip(l1, l2)] . This worked for me. Thanks:) – K V Mar 17 '22 at 09:11
  • @KV Thanks for accepting the answer. It was all a bit confusing but I think we've got it mostly figured out. Why did it work without them being casted into floats? I thought they were strings? – jonsca Mar 17 '22 at 15:48
  • @MisterMiyagi Ack, I see what you mean. I think it's a locale issue. – jonsca Mar 17 '22 at 15:54
  • https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python will work for the float conversion if you were presented with the data in a locale with comma used as decimal point and trying to do the conversion to float in another locale where the decimal point is represented by a period. – jonsca Mar 17 '22 at 16:07