I have two lists containing around 300 decimal numbers. That I need to convert into a dictionary format like below in order to plot a scatter graph using the values within the two lists as X and Y coordinates with Chart.JS.
This is the format that Chart.JS requires the data to be in.
data: [{
x: -10,
y: 0
}, {
x: 0,
y: 10
}, {
x: 10,
y: 5
}]
I've been trying to do this in a number of different ways but each time I get it wrong. Does anyone know a good way to accomplish this?
You can use Array#map
to generate such an array.
The second parameter of the map
function is the index, you can get the y
value by that index.
const features = [1, 2, 3, 4, 5, 6]; const labels = [11, 22, 33, 44, 55, 66]; const coords = features.map((x, i) => ({ x, y: labels[i] })); console.log(coords);
Someone has already written the Javascript solution, I will describe Python way.
a = [1, 2, 3, 4, 5];
b = [9, 8, 7, 6, 5];
data = []
for x, y in zip(a, b):
data.append({'x': x, 'y': y})
or shorter and pythonic style:
data = [{'x': x, 'y': y} for x, y in zip(a, b)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.