Convert a list of 3D coordinates in string format to a list of floats - python-3.x

I have a list of 3D coordinates in the format as list_X.
list_X =' [43.807 7.064 77.155], [35.099 3.179 82.838], [53.176052 -5.4618497 83.53082 ], [39.75858 1.5679997 74.76174 ], [42.055664 2.459083 80.89183 ]'
I want to convert into floats as below
list_X =[43.807 7.064 77.155], [35.099 3.179 82.838], [53.176052 -5.4618497 83.53082 ], [39.75858 1.5679997 74.76174 ], [42.055664 2.459083 80.89183 ]
I was trying as below which doesn't work
list1=[float(x) for x in list_X]

You can clean up the string to fit in the format of a list (i.e., add surrounding square brackets ([]) to contain all of the 3D coordinates, and separate the values by commas), and then use the json.loads method.
import json
list_X ='[[43.807, 7.064, 77.155], [35.099, 3.179, 82.838], [53.176052, -5.4618497, 83.53082], [39.75858, 1.5679997, 74.76174], [42.055664, 2.459083, 80.89183]]'
print(json.loads(list_X))
# Output
[[43.807, 7.064, 77.155], [35.099, 3.179, 82.838], [53.176052, -5.4618497, 83.53082], [39.75858, 1.5679997, 74.76174], [42.055664, 2.459083, 80.89183]]

Related

Create dict from array or list OR just parse the list for X and Y coordinates

I have 3000 lines of data like this:
['OFFD.271818,271818,"LINESTRING (16.303895355263016 48.18772778239529, 16.304571765172827 48.18758202488568, 16.30482300975865 48.18755484403183, 16.305031079294384 48.187546649202545, 16.30536730486924 48.187533407177206, 16.307523452290432 48.18753396398144, 16.309072536732444 48.18748514596115, 16.312777938045286 48.18734458451529, 16.313426882251083 48.18727411748434, 16.315405366265555 48.186920966444205, 16.316609208646593 48.18670268519608, 16.317260447683868 48.18652861710351, 16.31853471535412 48.186166775088815)",U4,4,U-Bahn,']
I want using matplotlib to create a plot, but I need X and Y coordinates.
The Targe is: U4 (from the line)
Coordinates are:
16.303895355263016 48.18772778239529, 16.304571765172827 48.18758202488568, 16.30482300975865 48.18755484403183, 16.305031079294384 48.187546649202545, 16.30536730486924 48.187533407177206, 16.307523452290432 48.18753396398144, 16.309072536732444 48.18748514596115, 16.312777938045286 48.18734458451529, 16.313426882251083 48.18727411748434, 16.315405366265555 48.186920966444205, 16.316609208646593 48.18670268519608, 16.317260447683868 48.18652861710351, 16.31853471535412 48.186166775088815
I do not get how to parse this string with numpy and create the dataset:
U4: coordinates for X: ... and for Y:....
Any hints?

Convert a deeply nested list to csv in Python

I wanted to write a deep nested list (within list within list) to csv, but it always collapse my list and printed with ..., which im unable to retrieve the hidden values.
List is to store frames of videos, the list goes up to 5 layer ()-len of each layer, Video (no of videos) > 8 frames(8) > width(200) > height(200) > pixel of 3 channel (3)
I tried converting the list to data frame before writing it to csv but still unable to solve this problem.
"[array([[[0.23137255, 0.26666668, 0.27058825],
[0.23921569, 0.27450982, 0.2784314 ],
[0.23529412, 0.27058825, 0.27450982],
...,
[0.25882354, 0.29411766, 0.2901961 ],
[0.25490198, 0.2901961 , 0.28627452],
[0.25490198, 0.2901961 , 0.28627452]],
[[0.20392157, 0.23921569, 0.24313726],
[0.21568628, 0.2509804 , 0.25490198],
[0.21568628, 0.2509804 , 0.25490198],
...,
[0.26666668, 0.3019608 , 0.29803923],
[0.26666668, 0.3019608 , 0.29803923],
[0.2627451 , 0.29803923, 0.29411766]],
[[0.1882353 , 0.22352941, 0.22745098],
[0.2 , 0.23529412, 0.23921569],
[0.20392157, 0.23921569, 0.24313726],
...,
[0.27450982, 0.30980393, 0.30588236],
[0.27058825, 0.30588236, 0.3019608 ],
[0.27058825, 0.30588236, 0.3019608 ]],
...,
I'd try one of the following:
dump the whole object into json:
import json
with open('my_saved_file.json', 'w+') as out_file:
out_file.write(list_of_lists_of_lists, indent=2)
What I'd try is storing all of your images as images and reference them in an index (could be csv)
import numpy as np
from PIL import Image
with open('reference.csv', 'w+') as out_csv:
out_csv.write("video, frame_set, frame1, frame2, frame3, frame4, frame5, frame6, frame7, frame8\n")
for video_no, video in enumerate(list_of_lists_of_lists):
row = [video_no]
for frame_set_no, frames in enumerate(video):
for frame_no, frame in enumerate(frames):
im = Image.fromarray(frame)
frame_name = f"{video_no}-{frame_set_no}-{frame_no}.jpeg"
row.append(frame_name)
im.save(frame_name)
out_csv.write(",".join(row) + "\n")

Write Past Value in data.txt

I want to write some list in data.txt.
The output from program is:
Triangle
('(a1, b1)', '(a2, b2)', '(a3, b3)')
Triangle
('(a4, b4)', '(a5, b5)', '(a6, b6)')
With this lines of code to write in data.txt;
data = {}
data['shapes'] = []
data['shapes'].append({
'name': str(triangle.name),
'Vertices': list(triangle.get_points())
I need output in my data.txt with json format like this:
{"shapes": [{"name": "Triangle", "Vertices": ["(a1, b1)", "(a2, b2)", "(a3, b3)"]}, {"name": "Triangle", "Vertices": ["(a4, b4)", "(a5, b5)", "(a6, b6)"]}]}
But this is what I get:
{"shapes": [{"name": "Triangle", "Vertices": ["(a4, b4)", "(a5, b5)", "(a6, b6)"]}]}
So, how can I write the past value of triangle that have vertices (a1, b1)...(a3, b3)?
This part of your code should be executed only once:
data = {}
data['shapes'] = []
The following part of your code you should execute repeatedly
data['shapes'].append({
'name': str(triangle.name),
'Vertices': list(triangle.get_points())
probably in a loop similar to this one
for triangle in triangles:
data['shapes'].append({
'name': str(triangle.name),
'Vertices': list(triangle.get_points())
It seems like you're overwriting the variable referencing the first triangle object with the next triangle object before appending the first triangle object's information to data['shapes'].
That block of code where you append to your data['shapes'] should be executed twice, once for each triangle object.

how to initialize list of lists of lists with unknown dimensions

Here d is a list of lists of lists with structure like this
[
[
[vocab[START], vocab["image1"], vocab["caption1"], vocab[END]],
[vocab[START], vocab["image1"], vocab["caption2"], vocab[END]],
...
],
...
]
I don't know the dimensions already therefore I have problem in initializing, keeping an upper limit I could have used the xrange function like this
d=[[[[] for k in xrange(50)] for j in xrange(10)] for i in xrange(8769)]
but I'm working in Python3 and xrange is depreciated. The code goes like this
for i in range (len(t)):
for j in range (len(t[i])):
d[i][j][0]=vocab[START]
for k in range(len(t[i][j])):
if t[i][j][k] not in list(vocab.keys()):
d[i][j][k+1]=vocab[UNK]
else:
d[i][j][k+1]=vocab[t[i][j][k]]
Any help on this is appreciated.

Azure Stream Analitics list in the list

How do I take the data from the file list if json array is in the list? eg.
{"city":{"id":1851632,"name":"Shuzenji",
"coord":{"lon":138.933334,"lat":34.966671},
"country":"JP",
"cod":"200",
"message":0.0045,
"cnt":38,
"list":[{
"dt":1406106000,
"main":{
"temp":298.77,
"temp_min":298.77,
"temp_max":298.774,
"pressure":1005.93,
"sea_level":1018.18,
"grnd_level":1005.93,
"humidity":87
"temp_kf":0.26},
"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
"clouds":{"all":88},
"wind":{"speed":5.71,"deg":229.501},
"sys":{"pod":"d"},
"dt_txt":"2014-07-23 09:00:00"}
]}
How do I get to weather.main?
First your JSON format seem to have errors, let's fix these first:
{"city":{"id":1851632,"name":"Shuzenji"},
"coord":{"lon":138.933334,"lat":34.966671},
"country":"JP",
"cod":"200",
"message":0.0045,
"cnt":38,
"list":[{
"dt":1406106000,
"main":{
"temp":298.77,
"temp_min":298.77,
"temp_max":298.774,
"pressure":1005.93,
"sea_level":1018.18,
"grnd_level":1005.93,
"humidity":87,
"temp_kf":0.26},
"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],
"clouds":{"all":88},
"wind":{"speed":5.71,"deg":229.501},
"sys":{"pod":"d"},
"dt_txt":"2014-07-23 09:00:00"}
]},
Note comma after at the end of humidity line, and closed record in the first line after Shuzenji.
Now, in the JSON example weather.main, is actually list[0].weather[0].main. Here is the way to get precisely this value
out of the JSON structure using Array and Record built-in functions:
SELECT
-- input.list[0].weather[0].main
WeatherMain = GetRecordPropertyValue(GetArrayElement(GetRecordPropertyValue(GetArrayElement(input.list, 0), 'weather'), 0), 'main')
FROM input

Resources