0

I have a list of list of coordinate with data type of float for example something like this

[[106.4372634887695, -6.3303128514375], [106.4372634887695, -6.3299716218919], [106.4376068115234, -6.3299716218919]]

and I want to convert it so the inside bracket would be gone and replaced only by comma. and the comma inside the deepest bracket would be replaced too by space

and the final format would be something like

((106.4372634887695 -6.3303128514375, 106.4372634887695 -6.3299716218919, 106.4376068115234 -6.3299716218919))

Is there a good way to solve this?

I tried to use map and join but i didn't succeeded

Ariq Hadi
  • 3
  • 1
  • 2
  • 1
    Does this answer your question? [How to make a flat list out of list of lists?](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists) – Mike Scotty Feb 16 '21 at 11:40
  • Probably this might help you: https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists – saimOneFcs Feb 16 '21 at 11:43

2 Answers2

2

You can flatten your array by using:

flatten = [elm for bracket in data for elm in bracket]

It's like:

arr = []
for bracket in data:
    for x in bracket:
        arr.append(x) #or arr += [x]

If you just want to sum brackets, do:

final = [a + b for a,b in arr]#b is -|b|, it's negative, then a+b is the same as 106.xxx - 6.xxx
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
K V
  • 61
  • 8
  • but how do you remove the comma inside the bracket though? from [106.4372634887695, -6.3303128514375], [106.4372634887695, -6.3299716218919] to 106.8365478515625 -6.32537841796875, 106.8365478515625 -6.324005126953125 – Ariq Hadi Feb 16 '21 at 11:52
  • check your results, there are not same – K V Feb 16 '21 at 12:02
  • sorry, I mean from [106.4372634887695, -6.3303128514375], [106.4372634887695, -6.3299716218919] to (106.4372634887695 -6.3303128514375 , 106.4372634887695, -6.3299716218919) – Ariq Hadi Feb 16 '21 at 12:12
  • ```finnal = [a + b for a,b in arr]#b is -|b|, it's negativ, then a+b is the same as 106.xxx - 6.xxx``` – K V Feb 16 '21 at 12:19
  • 1
    sorry again, but the data should be presented as-is because it is a coordinate location, no calculation is needed here, in that case how to approach this problem? – Ariq Hadi Feb 16 '21 at 12:32
0

The format you want is not the representation of any type which exists in Python. If you want it as a string, then, turn to string processing:

pairs = [[106.4372634887695, -6.3303128514375], [106.4372634887695, -6.3299716218919], [106.4376068115234, -6.3299716218919]]

stringified = ', '.join(' '.join(map(str,pair)) for pair in pairs)

parenthesized = f'(({stringified}))'