hellsing48
hellsing48

Reputation: 11

How to convert one element lists to float

I'm trying to extract pressure values from openFoam output log.

I read the lines of file as strings and extract floats using:

pressure = re.findall(r'\d+\.\d+', line)

Then, I'm trying to convert string values to float values with:

press[dummy1] = [float(p1) for p1 in pressure]

I expect this would simply give me an array of floats, however I get something like:

[[], [1502.79], [1016.86], [752.515], [776.874], [877.85], [989.854], [1139.05], [1402.28], [1305.71], [1547.23], [1389.58], [998.685], [799.895], [711.647], [1507.87], [1753.06], [1438.93], [1119.76], [1001.05], [941.412], [676.467], [772.924], [1063.77], [1537.84], [1732.5], [1342.52], [1053.91], [966.436], [1017.93], [1260.55], [1226.71], [1031.25], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

I can't do any mathematical operations with its elements and when I try to convert single element of array with:

something = int(press[3])

I get following error:

TypeError: int() argument must be a string or a number, not 'list'

What does numbers in [] represent? Is it one element list or something else? And what to do with it?

Upvotes: 0

Views: 1354

Answers (3)

Celius Stingher
Celius Stingher

Reputation: 18367

Because using the .findall() function of re returns a list with the matches. When none is found, then the return is an empty list, when there's something, it returns a list with all the results, if you are iterating over values in a list then you will get one of two:

  1. Empty lists when the regex is not present.
  2. 1-value list when the regex is present.

The simplest way to deal with is to add [0] to the result of the .findall() function.

From the documentation:

Return all non-overlapping matches of pattern in string, as a list of string.

Upvotes: 6

Emre Sevinç
Emre Sevinç

Reputation: 8511

Variations on a theme: if you have a "mixed" list of integers, lists, and empty lists, you could process it like:

mixed_list = [[], [1502.79], [1016.86], [752.515], 0 , 0]

result = [item[0] if isinstance(item, list) and len(item) > 0 else item for item in mixed_list]

print(result)

and it would print:

[[], 1502.79, 1016.86, 752.515, 0, 0]

And if you also wanted to get rid of empty lists, you could continue processing:

result_without_empyt_lists = [item for item in result if item]

print(result_without_empyt_lists)

to have:

[1502.79, 1016.86, 752.515]

of course, it's better to construct the list the way you want it, as exemplified in previous answers, but if you don't have control over how the initial list is constructed, the methods above could work, too.

Upvotes: 0

user10703542
user10703542

Reputation:

Try this:

[press.append(float(p1)) for p1 in pressure]

Upvotes: 1

Related Questions