0

I have a string as follows

144.963286 -37.814212 144.964498 -37.813854 144.964962 -37.814806 144.963711, -37.815168

I want to convert above string to an array such as below:

[(144.963286, -37.814212), (144.964498, -37.813854), (144.964962, -37.814806), (144.963711, -37.815168)]
1
  • Array or list? They're very different. Commented Jan 27, 2019 at 6:56

3 Answers 3

4

Assuming that you are using dot for separating decimal part from fractional one (and commas are not needed) we can

  1. Remove commas in string.
  2. Split string by whitespace.
  3. Convert each of substrings to floating number.
  4. zip floats into pairs.

like

>>> string = '144.963286 -37.814212 144.964498 -37.813854 144.964962 -37.814806 144.963711, -37.815168'
>>> floats = map(float, string.replace(',', '').split())  # use `itertools.imap` instead of `map` in Python2
>>> list(zip(floats, floats))
[(144.963286, -37.814212), (144.964498, -37.813854), (144.964962, -37.814806), (144.963711, -37.815168)]

As @AlexanderReynolds suggested we can use itertools.zip_longest function instead of zip for cases with odd number of arguments with some sort of fillvalue (default is None) like

>>> string = '144.963286, -37.814212 42'
>>> floats = map(float, string.replace(',', '').split())
>>> from itertools import zip_longest
>>> list(zip_longest(floats, floats,
                     fillvalue=float('inf')))
[(144.963286, -37.814212), (42.0, inf)]

also we can do it in one (pretty complex though) line with itertools.repeat like

>>> from itertools import repeat
>>> list(zip_longest(*repeat(map(float, string.replace(',', '').split()),
                             times=2),
                     fillvalue=float('inf')))
[(144.963286, -37.814212), (42.0, inf)]
Sign up to request clarification or add additional context in comments.

3 Comments

@zwer: in Python3 map will return iterator, so it should be fine, tested on Python3.6
Ooops, didn't notice the tag, I was looking at marking the question as a duplicate of a similar one tagged with 2.x when your answer popped up. Sorry about that. ⚘
Might be good to suggest zip_longest for a string with an odd number of floats.
2

Use zip with slicing:

s = '144.963286 -37.814212 144.964498 -37.813854 144.964962 -37.814806 144.963711 -37.815168'

splitted = s.split()
result = list(zip(splitted[::2], splitted[1::2]))

# [('144.963286', '-37.814212'), ('144.964498', '-37.813854'), ('144.964962', '-37.814806'), ('144.963711', '-37.815168')]

3 Comments

Why not just list(zip(splitted[::2], splitted[1::2]))?
OP needs floats and not strings
for get float insead of string, you can change splitted = s.split() to splitted = [float(i) for i in s.split()]
0

You can use regex:

import re

s = '144.963286 -37.814212 144.964498 -37.813854 144.964962 -37.814806 144.963711, -37.815168'

pattern = r'(-?\d+\.\d+).+?(-?\d+\.\d+)'
new_s = [(float(i.group(1)), float(i.group(2))) for i in re.finditer(pattern, s)]

# [(144.963286, -37.814212), (144.964498, -37.813854), (144.964962, -37.814806), (144.963711, -37.815168)]

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.