Skip to main content
provided more complete answer
Source Link
Edward
  • 66.6k
  • 4
  • 118
  • 282

A more pythonic method for that last for loop would be to use nested list comprehensions. It's likely faster, as well:

[item for sublist in [[x.real, x.imag] for x in answer] for item in sublist]

See this question for details about what's happening here, if you're not already familiar with list comprehensions.

In other words, instead of these four lines:

export[q-1, 0] = q
for _ in range(len(answer)):
      export[q-1, 2*_+1] = answer[_].real
      export[q-1, 2*(_+1)] = answer[_].imag

You could write this:

line = [q]
line.extend([item for sublist in [[x.real, x.imag] for x in answer] for item in sublist])
export[q-1] = line

A more pythonic method for that last for loop would be to use nested list comprehensions. It's likely faster, as well:

[item for sublist in [[x.real, x.imag] for x in answer] for item in sublist]

See this question for details about what's happening here, if you're not already familiar with list comprehensions.

A more pythonic method for that last for loop would be to use nested list comprehensions. It's likely faster, as well:

[item for sublist in [[x.real, x.imag] for x in answer] for item in sublist]

See this question for details about what's happening here, if you're not already familiar with list comprehensions.

In other words, instead of these four lines:

export[q-1, 0] = q
for _ in range(len(answer)):
      export[q-1, 2*_+1] = answer[_].real
      export[q-1, 2*(_+1)] = answer[_].imag

You could write this:

line = [q]
line.extend([item for sublist in [[x.real, x.imag] for x in answer] for item in sublist])
export[q-1] = line
Source Link
Edward
  • 66.6k
  • 4
  • 118
  • 282

A more pythonic method for that last for loop would be to use nested list comprehensions. It's likely faster, as well:

[item for sublist in [[x.real, x.imag] for x in answer] for item in sublist]

See this question for details about what's happening here, if you're not already familiar with list comprehensions.