new_sysadmin
new_sysadmin

Reputation: 165

How to a turn a list of strings into complex numbers in python?

I'm trying to write code which imports and exports lists of complex numbers in Python. So far I'm attempting this using the csv module. I've exported the data to a file using:

spamWriter = csv.writer(open('data.csv', 'wb')
spamWriter.writerow(complex_data)

Where complex data is a list numbers generated by the complex(re,im) function. Ex:

print complex_data
[(37470-880j),(35093-791j),(33920-981j),(28579-789j),(48002-574j),(46607-2317j),(42353-1557j),(45166-2520j),(45594-232j),(41149+561j)]

To then import this at a later time, I try the following:

mycsv = csv.reader(open('data.csv', 'rb'))
out = list(mycsv)
print out
[['(37470-880j)','(35093-791j)','(33920-981j)','(28579-789j)','(48002-574j)','(46607-2317j)','(42353-1557j)','(45166-2520j)','(45594-232j)','(41149+561j)']]

(Note that this is a list of lists, I just happened to use only one row for the example.)

I now need to turn this into complex numbers rather than strings. I think there should be a way to do this with mapping as in this question, but I couldn't figure out how to make it work. Any help would be appreciated!

Alternatively, if there's any easier way to import/export complex-valued data that I don't know of, I'd be happy to try something else entirely.

Upvotes: 4

Views: 8579

Answers (5)

Anurag Misra
Anurag Misra

Reputation: 1544

I think each method above is bit complex

Easiest Way is this

In [1]: complex_num = '-2+3j'

In [2]: complex(complex_num)
Out[2]: (-2+3j)

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1123042

Just pass the string to complex():

>>> complex('(37470-880j)')
(37470-880j)

Like int() it takes a string representation of a complex number and parses that. You can use map() to do so for a list:

map(complex, row)

Upvotes: 9

applicative_functor
applicative_functor

Reputation: 4976

>>> c = ['(37470-880j)','(35093-791j)','(33920-981j)']
>>> map(complex, c)
[(37470-880j), (35093-791j), (33920-981j)]

Upvotes: 8

Pyrce
Pyrce

Reputation: 8571

CSV docs say:

Note that complex numbers are written out surrounded by parens. This may cause some problems for other programs which read CSV files (assuming they support complex numbers at all).

This should convert elements in 'out' to complex numbers from the string types, which is the simplest solution given your existing code with ease of handling non-complex entries.

 for i,row in enumerate(out):
      j,entry in enumerate(row):
          try:
               out[i][j] = complex(out[i][entry])
          except ValueError:
               # Print here if you want to know something bad happened
               pass

Otherwise using map(complex, row) on each row takes fewer lines.

 for i,row in enumerate(out):
      out[i] = map(complex, row)

Upvotes: 0

Guy Adini
Guy Adini

Reputation: 5494

complex_out = []
for row in out:
    comp_row = [complex(x) for x in row]
    complex_out.append(comp_row)

Upvotes: 1

Related Questions