I was trying to extract all the elements of the my data points (x,y) tuples, and put them into list of x values and y list, and transfer them to two columns in excel spreadsheet. It seems writing numbers into file is quite difficult. Can anyone shed a light on this problem?
Current state:
xlist=[list[i][0] for i in range(len(list))]
ylist=[list[i][1] for i in range(len(list))]
fob=open('c:/test/a.txt','w')
fob.write(xlist[i] for i in range(len(xlist))
i want to write down a column of numbers in notepad so that I can highlight and copy into spread sheet directly .
Below are my data.
list = [(0.496, 12.49), (0.531, 12.40), (0.578, 12.18), (0.615,
11.96), (0.657, 11.75), (0.731, 11.28), (0.785, 10.85), (0.812,
10.61), (0.883, 9.92), (0.930, 9.40), (0.979, 8.77), (1.026,
8.10), (1.081, 7.23), (1.134, 6.33), (1.189, 5.39), (1.220,
4.85), (1.273, 3.92), (1.332, 2.91), (1.364, 2.55), (1.418,
2.16), (1.467, 1.65), (1.523, 1.17), (1.569, 0.82), (1.626,
0.47), (1.678, 0.21), (1.723, 0.01), (1.776, 0.19), (1.814,
0.28), (1.869, 0.36), (1.933, 0.36), (1.972, 0.31), (2.021,
0.18), (2.081, 0.13), (2.129, 0.46), (2.169, 0.79), (2.219,
1.24), (2.280, 1.84), (2.306, 2.11), (2.358, 2.67), (2.414,
3.37), (2.471, 4.05), (2.505, 4.51), (2.562, 5.22), (2.613,
5.84), (2.652, 6.31), (2.712, 7.01), (2.758, 7.52), (2.802,
7.99), (2.869, 8.63), (2.930, 9.16), (2.971, 9.57), (3.043,
10.35), (3.078, 10.69), (3.119, 11.00), (3.174, 11.26), (3.217,
11.40), (3.261, 11.53), (3.307, 11.55), (3.371, 11.51), (3.432,
11.40), (3.479, 11.26), (3.507, 11.20), (3.557, 11.00), (3.623,
10.55), (3.663, 10.28), (3.729, 9.79), (3.768, 9.57), (3.825,
9.24), (3.880, 8.85), (3.944, 8.41), (3.969, 8.04), (4.014,
7.55), (4.086, 6.67), (4.105, 6.37), (4.166, 5.50), (4.212,
4.88), (4.266, 4.20), (4.311, 3.69), (4.364, 3.06), (4.401,
2.65), (4.453, 2.09), (4.497, 1.68), (4.556, 1.18), (4.602,
0.85), (4.644, 0.57), (4.695, 0.29), (4.754, 0.04), (4.799,
0.11), (4.847, 0.17), (4.918, 0.11), (4.959, 0.04), (4.992,
0.19), (5.063, 0.64), (5.098, 0.90), (5.157, 1.40), (5.201,
1.79), (5.245, 2.20), (5.291, 2.65), (5.326, 3.00), (5.387,
3.65), (5.420, 4.02), (5.469, 4.62), (5.538, 5.44), (5.579,
5.96), (5.629, 6.57), (5.674, 7.14), (5.724, 7.73), (5.798,
8.60), (5.823, 8.88), (5.888, 9.62), (5.919, 9.94), (5.963,
10.41), (6.009, 10.85), (6.050, 11.22), (6.115, 11.71), (6.153,
11.99), (6.222, 12.39), (6.263, 12.61), (6.302, 12.77), (6.377,
12.99), (6.414, 13.03), (6.454, 13.02), (6.522, 12.89), (6.558,
12.74), (6.626, 12.41), (6.677, 12.05), (6.729, 11.64), (6.791,
11.00), (6.832, 10.58), (6.887, 9.92), (6.949, 9.13), (6.996,
8.48), (7.028, 8.09), (7.094, 7.13), (7.123, 6.70), (7.161,
6.16), (7.213, 5.35), (7.250, 4.81), (7.332, 3.61), (7.382,
2.93), (7.420, 2.45), (7.474, 1.88), (7.514, 1.40), (7.576,
0.71), (7.600, 0.50), (7.662, 0.12), (7.725, 0.16), (7.768,
0.26), (7.810, 0.30), (7.858, 0.26), (7.904, 0.18), (7.980,
0.10), (8.021, 0.29), (8.078, 0.65), (8.133, 1.06), (8.165,
1.33), (8.218, 1.83), (8.267, 2.31), (8.321, 2.87), (8.355,
3.27), (8.413, 3.91), (8.473, 4.61), (8.519, 5.22), (8.553,
5.65), (8.643, 6.74), (8.678, 7.23), (8.734, 7.94), (8.760,
8.27), (8.803, 8.81), (8.851, 9.35), (8.905, 9.94), (8.961,
10.45), (9.009, 10.92), (9.053, 11.34), (9.106, 11.75), (9.166,
12.14), (9.228, 12.48), (9.292, 12.71), (9.340, 12.86), (9.384,
13.01), (9.412, 13.05), (9.452, 13.03), (9.472, 13.00)]
Cheers
Export it into a CSV file. Your use case is very simple and you should be able to do it using standard Python.
with open('output.csv', 'w') as f:
for x, y in l:
f.write("%s, %s\n" % (x, y))
Note: list is a reserved word in python and you should not be using it.
Use openpyxl to write .xslx files from Python:
import openpyxl
my_list = [(0.496, 12.49), (0.531, 12.40), (0.578, 12.18), (0.615,
11.96), (0.657, 11.75), (0.731, 11.28), (0.785, 10.85), (0.812,
10.61), (0.883, 9.92), (0.930, 9.40), (0.979, 8.77), (1.026,
8.10), (1.081, 7.23), (1.134, 6.33), (1.189, 5.39), (1.220,
4.85), (1.273, 3.92), (1.332, 2.91), (1.364, 2.55), (1.418,
2.16), (1.467, 1.65), (1.523, 1.17), (1.569, 0.82), (1.626,
0.47), (1.678, 0.21), (1.723, 0.01), (1.776, 0.19), (1.814,
0.28), (1.869, 0.36), (1.933, 0.36), (1.972, 0.31), (2.021,
0.18), (2.081, 0.13), (2.129, 0.46), (2.169, 0.79), (2.219,
1.24), (2.280, 1.84), (2.306, 2.11), (2.358, 2.67), (2.414,
3.37), (2.471, 4.05), (2.505, 4.51), (2.562, 5.22), (2.613,
5.84), (2.652, 6.31), (2.712, 7.01), (2.758, 7.52), (2.802,
7.99), (2.869, 8.63), (2.930, 9.16), (2.971, 9.57), (3.043,
10.35), (3.078, 10.69), (3.119, 11.00), (3.174, 11.26), (3.217,
11.40), (3.261, 11.53), (3.307, 11.55), (3.371, 11.51), (3.432,
11.40), (3.479, 11.26), (3.507, 11.20), (3.557, 11.00), (3.623,
10.55), (3.663, 10.28), (3.729, 9.79), (3.768, 9.57), (3.825,
9.24), (3.880, 8.85), (3.944, 8.41), (3.969, 8.04), (4.014,
7.55), (4.086, 6.67), (4.105, 6.37), (4.166, 5.50), (4.212,
4.88), (4.266, 4.20), (4.311, 3.69), (4.364, 3.06), (4.401,
2.65), (4.453, 2.09), (4.497, 1.68), (4.556, 1.18), (4.602,
0.85), (4.644, 0.57), (4.695, 0.29), (4.754, 0.04), (4.799,
0.11), (4.847, 0.17), (4.918, 0.11), (4.959, 0.04), (4.992,
0.19), (5.063, 0.64), (5.098, 0.90), (5.157, 1.40), (5.201,
1.79), (5.245, 2.20), (5.291, 2.65), (5.326, 3.00), (5.387,
3.65), (5.420, 4.02), (5.469, 4.62), (5.538, 5.44), (5.579,
5.96), (5.629, 6.57), (5.674, 7.14), (5.724, 7.73), (5.798,
8.60), (5.823, 8.88), (5.888, 9.62), (5.919, 9.94), (5.963,
10.41), (6.009, 10.85), (6.050, 11.22), (6.115, 11.71), (6.153,
11.99), (6.222, 12.39), (6.263, 12.61), (6.302, 12.77), (6.377,
12.99), (6.414, 13.03), (6.454, 13.02), (6.522, 12.89), (6.558,
12.74), (6.626, 12.41), (6.677, 12.05), (6.729, 11.64), (6.791,
11.00), (6.832, 10.58), (6.887, 9.92), (6.949, 9.13), (6.996,
8.48), (7.028, 8.09), (7.094, 7.13), (7.123, 6.70), (7.161,
6.16), (7.213, 5.35), (7.250, 4.81), (7.332, 3.61), (7.382,
2.93), (7.420, 2.45), (7.474, 1.88), (7.514, 1.40), (7.576,
0.71), (7.600, 0.50), (7.662, 0.12), (7.725, 0.16), (7.768,
0.26), (7.810, 0.30), (7.858, 0.26), (7.904, 0.18), (7.980,
0.10), (8.021, 0.29), (8.078, 0.65), (8.133, 1.06), (8.165,
1.33), (8.218, 1.83), (8.267, 2.31), (8.321, 2.87), (8.355,
3.27), (8.413, 3.91), (8.473, 4.61), (8.519, 5.22), (8.553,
5.65), (8.643, 6.74), (8.678, 7.23), (8.734, 7.94), (8.760,
8.27), (8.803, 8.81), (8.851, 9.35), (8.905, 9.94), (8.961,
10.45), (9.009, 10.92), (9.053, 11.34), (9.106, 11.75), (9.166,
12.14), (9.228, 12.48), (9.292, 12.71), (9.340, 12.86), (9.384,
13.01), (9.412, 13.05), (9.452, 13.03), (9.472, 13.00)]
book = openpyxl.Workbook()
sheet = book.active
for i, value in enumerate(my_list):
sheet.cell(row=i+1, column=1).value = value[0]
sheet.cell(row=i+1, column=2).value = value[1]
book.save('test.xlsx')
When you have data like numbers or objects in memory, it's generally not correct to dump that data directly into disk, you'll want to serialize it.
The easiest way to serialize it is with print which automatically calls the "serialization" method __str__. The problem with this serialization method is that's not always easy to deserialize.
When you have a data structure, like the matrix you describe, you'll want a serialization method that will preserve the structure and allow to reconstruct it in memory. In this case you can use CSV (through the csv module), JSON (through the json module) or many others.
Use CSV.
Related
I have an array of tuples that are stored in a csv line by line and I want to convert them back, but each time I convert them back they are still strings and I need them to be ints.
"(1013, 294)","(872, 258)","(744, 190)","(704, 124)","(758, 78)","(853, 121)","(862, 68)","(861, 130)","(861, 166)","(972, 123)","(979, 67)","(956, 145)","(949, 177)","(1088, 136)","(1096, 85)","(1061, 155)","(1050, 188)","(1201, 158)","(1198, 121)","(1152, 168)","(1132, 194)"
"(1037, 305)","(906, 259)","(798, 192)","(756, 126)","(790, 78)","(894, 109)","(882, 29)","(873, -14)","(875, -52)","(1010, 119)","(1046, 72)","(1012, 150)","(990, 192)","(1122, 139)","(1156, 101)","(1101, 174)","(1069, 209)","(1224, 172)","(1248, 140)","(1189, 187)","(1152, 214)"
"(1031, 315)","(891, 269)","(812, 196)","(863, 130)","(968, 101)","(865, 117)","(813, 39)","(791, -10)","(778, -54)","(985, 113)","(989, 17)","(997, -33)","(1004, -70)","(1102, 132)","(1135, 57)","(1093, 105)","(1056, 152)","(1208, 170)","(1219, 124)","(1163, 156)","(1117, 192)"
Desired output should look like this:
handData =[(1013, 294), (872, 258), (744, 190), (704, 124), (758, 78), (853, 121), (862, 68), (861, 130), (861, 166), (972, 123), (979, 67), (956, 145), (949, 177), (1088, 136), (1096, 85), (1061, 155), (1050, 188), (1201, 158), (1198, 121), (1152, 168), (1132, 194)]
Current code:
with open('gesture_data.csv', 'r', newline='') as f:
reader = csv.reader(f)
examples = list(reader)
Gives this:
[['(1013, 294)', '(872, 258)', '(744, 190)', '(704, 124)', '(758, 78)', '(853, 121)', '(862, 68)', '(861, 130)', '(861, 166)', '(972, 123)', '(979, 67)', '(956, 145)', '(949, 177)', '(1088, 136)', '(1096, 85)', '(1061, 155)', '(1050, 188)', '(1201, 158)', '(1198, 121)', '(1152, 168)', '(1132, 194)'], ['(1037, 305)', '(906, 259)', '(798, 192)', '(756, 126)', '(790, 78)', '(894, 109)', '(882, 29)', '(873, -14)', '(875, -52)', '(1010, 119)', '(1046, 72)', '(1012, 150)', '(990, 192)', '(1122, 139)', '(1156, 101)', '(1101, 174)', '(1069, 209)', '(1224, 172)', '(1248, 140)', '(1189, 187)', '(1152, 214)'], ['(1031, 315)', '(891, 269)', '(812, 196)', '(863, 130)', '(968, 101)', '(865, 117)', '(813, 39)', '(791, -10)', '(778, -54)', '(985, 113)', '(989, 17)', '(997, -33)', '(1004, -70)', '(1102, 132)', '(1135, 57)', '(1093, 105)', '(1056, 152)', '(1208, 170)', '(1219, 124)', '(1163, 156)', '(1117, 192)']]
You can use a regex expression to match numbers from couples and map them to integers:
import re
handData = [tuple(map(int, re.findall('\d+', string[1:-1]))) for string in examples[0]]
I have this code block:
def euc_dist(x,y):
return ((x[0] - y[0])**2 +(x[1] - y[1])**2 )**(1/2)
def dist(s1,s2):
distances = [euc_dist(s1[i],s2[i]) for i in range(s1.shape[0])]
return pd.Series(distances)
distances_df = tracking_data.loc[:,tracking_data[['away_player10_point', 'away_player9_point', 'away_player8_point', 'away_player7_point', 'away_player6_point', 'away_player5_point', 'away_player4_point', 'away_player3_point', 'away_player2_point', 'away_player1_point', 'away_player11_point', 'home_player1_point', 'home_player2_point', 'home_player3_point', 'home_player4_point', 'home_player5_point', 'home_player6_point', 'home_player7_point', 'home_player8_point', 'home_player9_point', 'home_player10_point', 'home_player11_point']].apply(tuple, axis = 1)].apply(dist, args = (tracking_data["ball_point"]))
tracking_data["closest"] = distances_df.idxmin(axis = 1).apply(lambda x: str(x)[:-6])
I am getting this error when running:
KeyError: "None of [Index([
((-22.06, -8.32), (-0.12, 21.38), (-1.49, -9.62), (-0.26, -28.52),
(-19.32, 16.22), (-15.11, 0.43), (-7.69, 32.87), (0.45, -0.25),
(-9.88, 7.67), (-47.29, -0.14), (-18.1, -25.42), (0.46, -19.84),
(7.58, 4.82), (15.33, -23.38), (21.08, 6.57), (14.98, 20.7), (8.14,
-4.27), (21.36, -9.06), (46.92, 0.01), (0.29, 9.88), (0.67, 22.24), (-0.06, -9.07)),\n
((-22.06, -8.32), (-0.07, 21.39), (-1.47, -9.64), (-0.23, -28.51),
(-19.31, 16.22), (-15.1, 0.42), (-7.68, 32.88), (0.46, -0.26), (-9.87,
7.7), (-47.3, -0.15), (-18.09, -25.41), (0.43, -19.83), (7.5600000000000005, 4.83), (15.31, -23.38), (21.06, 6.57), (14.97,
20.72), (8.12, -4.28), (21.33, -9.04), (46.91, 0.02), (0.25, 9.85), (0.67, 22.24), (-0.11, -9.05)),\n
((-22.06, -8.33), (-0.03, 21.39), (-1.43, -9.67), (-0.2, -28.5),
(-19.29, 16.24), (-15.09, 0.42), (-7.66, 32.9), (0.47000000000000003,
-0.27), (-9.85, 7.72), (-47.31, -0.16), (-18.08, -25.4), (0.39, -19.83), (7.55, 4.85), (15.28, -23.38), (21.03, 6.57), (14.95, 20.74), (8.09, -4.28), (21.28, -9.02), (46.91, 0.03), (0.2, 9.82), (0.66,
22.24), (-0.16, -9.02)),\n ((-22.06, -8.34), (0.01, 21.39), (-1.3900000000000001, -9.7), (-0.16, -28.5), (-19.28,
16.25), (-15.08, 0.42), (-7.64, 32.92), (0.49, -0.27), (-9.84, 7.75), (-47.32, -0.16), (-18.07, -25.4), (0.3500000000000...
Please reference this notebook to see my table as it is too large to put here. The work pertaining to this question is at the bottom.
https://github.com/piercepatrick/Articles_EDA/blob/main/nashSCProject.ipynb
I have been trying to work out this problem in my previous question: Pandas Dataframe: Find the column with the closest coordinate point to another columns coordinate point
I have a hunch that this issue lies in the source data since I originally loaded it in as JSON?
First, reset the index with
tracking_data = tracking_data.reset_index()
Then change
distances_df = tracking_data.loc[:,tracking_data[['away_player10_point', 'away_player9_point', 'away_player8_point', 'away_player7_point', 'away_player6_point', 'away_player5_point', 'away_player4_point', 'away_player3_point', 'away_player2_point', 'away_player1_point', 'away_player11_point', 'home_player1_point', 'home_player2_point', 'home_player3_point', 'home_player4_point', 'home_player5_point', 'home_player6_point', 'home_player7_point', 'home_player8_point', 'home_player9_point', 'home_player10_point', 'home_player11_point']].apply(tuple, axis = 1)].apply(dist, args = (tracking_data["ball_point"]))
for
distances_df = tracking_data[['away_player10_point', 'away_player9_point', 'away_player8_point', 'away_player7_point', 'away_player6_point', 'away_player5_point', 'away_player4_point', 'away_player3_point', 'away_player2_point', 'away_player1_point', 'away_player11_point', 'home_player1_point', 'home_player2_point', 'home_player3_point', 'home_player4_point', 'home_player5_point', 'home_player6_point', 'home_player7_point', 'home_player8_point', 'home_player9_point', 'home_player10_point', 'home_player11_point']].apply(dist, args = (tracking_data["ball_point"],))
I need to enter data in to csv using headers and put a value if the flag is available in the event else zero it. Required output is:
I am currently getting:
This is my current code, I would like to know how to generate my desired output:
inputs for code is counter1-4 shown below :
OrderedDict([('flags=40', 3971), ('flags=10004', 6244), ('flags=10100', 236), ('flags=90002', 2), ('flags=80', 2009), ('flags=10080', 5421), ('flags=4', 2886), ('flags=100', 227), ('flags=80002', 58), ('flags=10040', 8990), ('flags=0', 5)])
OrderedDict([('flags=40', 16), ('flags=10004', 6244), ('flags=10100', 236), ('flags=90002', 2), ('flags=10080', 5421), ('flags=4', 16), ('flags=80002', 11), ('flags=10040', 8990), ('flags=0', 4), ('Total', 20940)])
OrderedDict([('flags=4', 1332), ('flags=40', 1839), ('flags=80002', 3), ('flags=100', 197), ('flags=80', 935), ('Total', 4306)])
OrderedDict([('Total', 0)])
OrderedDict([('flags=40', 2116), ('flags=80', 1074), ('flags=4', 1538), ('flags=100', 30), ('flags=80002', 44), ('flags=0', 1), ('Total', 4803)])
dat = 1
with open(outputcsv,'wb') as outcsv:
writer = csv.writer(outcsv,delimiter=',')
appname = inputfile[:-3]
writer.writerow(appname.split(','))
for x in threads:
writer.writerows([x.split(',')])
#w.writeheader([x.split(',')])
if dat == 1:
w = csv.DictWriter(outcsv,counter1.keys())
w.writeheader()
w.writerow(counter1)
elif dat == 2:
w = csv.DictWriter(outcsv,counter2.keys())
w.writeheader()
w.writerow(counter2)
elif dat == 3:
w = csv.DictWriter(outcsv,counter3.keys())
w.writeheader()
w.writerow(counter3)
elif dat == 4:
w = csv.DictWriter(outcsv,counter4.keys())
w.writeheader()
w.writerow(counter4)
dat = dat +1
writer.writerows('\n')
code for how threads are being read:
exampleFile = open('top_tasks.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
thread1 = exampleData[11][0]
thread2 = exampleData[12][0]
thread3 = exampleData[13][0]
thread4 = exampleData[14][0]
threads = [thread1,thread2,thread3,thread4]
I think this code meets your requirements:
from collections import OrderedDict
import csv
# build an OrderedDict of all keys
all_keys = OrderedDict()
# first column gets name of data set
all_keys[data_set_name] = data_set_name
# collect all of the known keys, and insert the thread name
for counter, thread in zip(counters, threads):
all_keys.update(counter)
counter[data_set_name] = thread
with open(outputcsv, 'wb') as outcsv:
# using all known keys, create a csv writer
w = csv.DictWriter(outcsv, fieldnames=all_keys.keys())
# output the header and data rows
w.writeheader()
w.writerows(counters)
Data Used:
outputcsv = 'output.csv'
counters = [
OrderedDict(
[('flags=40', 3971), ('flags=10004', 6244), ('flags=10100', 236),
('flags=90002', 2), ('flags=80', 2009), ('flags=10080', 5421),
('flags=4', 2886), ('flags=100', 227), ('flags=80002', 58),
('flags=10040', 8990), ('flags=0', 5)]),
OrderedDict(
[('flags=40', 16), ('flags=10004', 6244), ('flags=10100', 236),
('flags=90002', 2), ('flags=10080', 5421), ('flags=4', 16),
('flags=80002', 11), ('flags=10040', 8990), ('flags=0', 4),
('Total', 20940)]),
OrderedDict([('flags=4', 1332), ('flags=40', 1839), ('flags=80002', 3),
('flags=100', 197), ('flags=80', 935), ('Total', 4306)]),
OrderedDict([('Total', 0)]),
OrderedDict([('flags=40', 2116), ('flags=80', 1074), ('flags=4', 1538),
('flags=100', 30), ('flags=80002', 44), ('flags=0', 1),
('Total', 4803)]),
]
# code assumes thread names are in a list, make some sample names
threads = ['thread%d' % (i+1) for i in range(len(counters))]
# first column header if the name of the data set
data_set_name = 'CandyCrush 1'
I am creating a projectile motion simulator, using the scatter function in matplotlib. I have got a list of coordinates stored in an ordered dictionary but want to be able to add these coordinates one at a time with a given time interval.
PSEUDOCODE/Bad Python:
path = OrderedDict([(0.0707, 0.0702), (0.141, 0.139), (0.212, 0.208), (0.283, 0.275), (0.354, 0.341), (0.424, 0.407), (0.495, 0.471), (0.566, 0.534), (0.636, 0.597), (0.707, 0.658), (0.778, 0.719), (0.849, 0.778), (0.919, 0.836), (0.99, 0.894), (1.06, 0.95), (1.13, 1.01), (1.2, 1.06), (1.27, 1.11), (1.34, 1.17), (1.41, 1.22), (1.48, 1.27), (1.56, 1.32), (1.63, 1.37), (1.7, 1.41), (1.77, 1.46), (1.84, 1.51), (1.91, 1.55), (1.98, 1.6), (2.05, 1.64), (2.12, 1.68), (2.19, 1.72), (2.26, 1.76), (2.33, 1.8), (2.4, 1.84), (2.47, 1.87), (2.55, 1.91), (2.62, 1.95), (2.69, 1.98), (2.76, 2.01), (2.83, 2.04), (2.9, 2.08), (2.97, 2.11), (3.04, 2.13), (3.11, 2.16), (3.18, 2.19), (3.25, 2.22), (3.32, 2.24), (3.39, 2.27), (3.46, 2.29), (3.54, 2.31), (3.61, 2.33), (3.68, 2.35), (3.75, 2.37), (3.82, 2.39), (3.89, 2.41), (3.96, 2.42), (4.03, 2.44), (4.1, 2.45), (4.17, 2.47), (4.24, 2.48), (4.31, 2.49), (4.38, 2.5), (4.45, 2.51), (4.53, 2.52), (4.6, 2.53), (4.67, 2.53), (4.74, 2.54), (4.81, 2.54), (4.88, 2.55), (4.95, 2.55), (5.02, 2.55), (5.09, 2.55), (5.16, 2.55), (5.23, 2.55), (5.3, 2.55), (5.37, 2.54), (5.44, 2.54), (5.52, 2.53), (5.59, 2.53), (5.66, 2.52), (5.73, 2.51), (5.8, 2.5), (5.87, 2.49), (5.94, 2.48), (6.01, 2.47), (6.08, 2.46), (6.15, 2.44), (6.22, 2.43), (6.29, 2.41), (6.36, 2.39), (6.43, 2.38), (6.51, 2.36), (6.58, 2.34), (6.65, 2.32), (6.72, 2.3), (6.79, 2.27), (6.86, 2.25), (6.93, 2.22), (7.0, 2.2), (7.07, 2.17), (7.14, 2.14), (7.21, 2.11), (7.28, 2.08), (7.35, 2.05), (7.42, 2.02), (7.5, 1.99), (7.57, 1.96), (7.64, 1.92), (7.71, 1.89), (7.78, 1.85), (7.85, 1.81), (7.92, 1.77), (7.99, 1.73), (8.06, 1.69), (8.13, 1.65), (8.2, 1.61), (8.27, 1.57), (8.34, 1.52), (8.41, 1.48), (8.49, 1.43), (8.56, 1.38), (8.63, 1.33), (8.7, 1.28), (8.77, 1.23), (8.84, 1.18), (8.91, 1.13), (8.98, 1.08), (9.05, 1.02), (9.12, 0.968), (9.19, 0.911), (9.26, 0.854), (9.33, 0.796), (9.4, 0.737), (9.48, 0.677), (9.55, 0.616), (9.62, 0.554), (9.69, 0.491), (9.76, 0.427), (9.83, 0.361), (9.9, 0.295), (9.97, 0.229), (10.0, 0.161), (10.1, 0.0916), (10.2, 0.0217)])
for each in path:
graph.update(each)
time.sleep(0.1)
Any help would be much appreciated :)
This works for me: (at least for prototyping)
import matplotlib.pyplot as plt
path = [(0.0707, 0.0702), (0.141, 0.139), (0.212, 0.208), (0.283, 0.275), (0.354, 0.341), (0.424, 0.407), (0.495, 0.471), (0.566, 0.534), (0.636, 0.597), (0.707, 0.658), (0.778, 0.719), (0.849, 0.778), (0.919, 0.836), (0.99, 0.894), (1.06, 0.95), (1.13, 1.01), (1.2, 1.06), (1.27, 1.11), (1.34, 1.17), (1.41, 1.22), (1.48, 1.27), (1.56, 1.32), (1.63, 1.37), (1.7, 1.41), (1.77, 1.46), (1.84, 1.51), (1.91, 1.55), (1.98, 1.6), (2.05, 1.64), (2.12, 1.68), (2.19, 1.72), (2.26, 1.76), (2.33, 1.8), (2.4, 1.84), (2.47, 1.87), (2.55, 1.91), (2.62, 1.95), (2.69, 1.98), (2.76, 2.01), (2.83, 2.04), (2.9, 2.08), (2.97, 2.11), (3.04, 2.13), (3.11, 2.16), (3.18, 2.19), (3.25, 2.22), (3.32, 2.24), (3.39, 2.27), (3.46, 2.29), (3.54, 2.31), (3.61, 2.33), (3.68, 2.35), (3.75, 2.37), (3.82, 2.39), (3.89, 2.41), (3.96, 2.42), (4.03, 2.44), (4.1, 2.45), (4.17, 2.47), (4.24, 2.48), (4.31, 2.49), (4.38, 2.5), (4.45, 2.51), (4.53, 2.52), (4.6, 2.53), (4.67, 2.53), (4.74, 2.54), (4.81, 2.54), (4.88, 2.55), (4.95, 2.55), (5.02, 2.55), (5.09, 2.55), (5.16, 2.55), (5.23, 2.55), (5.3, 2.55), (5.37, 2.54), (5.44, 2.54), (5.52, 2.53), (5.59, 2.53), (5.66, 2.52), (5.73, 2.51), (5.8, 2.5), (5.87, 2.49), (5.94, 2.48), (6.01, 2.47), (6.08, 2.46), (6.15, 2.44), (6.22, 2.43), (6.29, 2.41), (6.36, 2.39), (6.43, 2.38), (6.51, 2.36), (6.58, 2.34), (6.65, 2.32), (6.72, 2.3), (6.79, 2.27), (6.86, 2.25), (6.93, 2.22), (7.0, 2.2), (7.07, 2.17), (7.14, 2.14), (7.21, 2.11), (7.28, 2.08), (7.35, 2.05), (7.42, 2.02), (7.5, 1.99), (7.57, 1.96), (7.64, 1.92), (7.71, 1.89), (7.78, 1.85), (7.85, 1.81), (7.92, 1.77), (7.99, 1.73), (8.06, 1.69), (8.13, 1.65), (8.2, 1.61), (8.27, 1.57), (8.34, 1.52), (8.41, 1.48), (8.49, 1.43), (8.56, 1.38), (8.63, 1.33), (8.7, 1.28), (8.77, 1.23), (8.84, 1.18), (8.91, 1.13), (8.98, 1.08), (9.05, 1.02), (9.12, 0.968), (9.19, 0.911), (9.26, 0.854), (9.33, 0.796), (9.4, 0.737), (9.48, 0.677), (9.55, 0.616), (9.62, 0.554), (9.69, 0.491), (9.76, 0.427), (9.83, 0.361), (9.9, 0.295), (9.97, 0.229), (10.0, 0.161), (10.1, 0.0916), (10.2, 0.0217)]
X = []
Y = []
for item in path:
X.append(item[0])
Y.append(item[1])
plt.ion() # turn interactive mode on
animated_plot = plt.plot(X, Y, 'ro')[0]
for i in range(len(X)):
animated_plot.set_xdata(X[0:i])
animated_plot.set_ydata(Y[0:i])
plt.draw()
plt.pause(0.1)
I have a ENTIREMAP which has a mapping of all names and toy prices. What I want to try and do is create a function getResults like below:
#################
def getResults(name, price):
# where name could be 'Laura' and price is 0.02
# then from the ENTIREMAP find the key 'Laura' and if 0.02 is in the range
# of the prices in the map i.e since 0.02 is between (0.0,0.05) then return
# ('PEN', 'BLUE')
prices = [d[name] for d in ENTIRELIST if name in d]
if prices:
print prices[0]
###################
GIRLTOYPRICES = {(0.0,0.05):('PEN', 'BLUE'),
(0.05,0.08):('GLASSES', 'DESIGNER'),
(0.08,0.12):('TOP', 'STRIPY'),
}
BOYTOYPRICES = {(0.0,0.10):('BOOK', 'HARRY POTTER'),
(0.10,0.15):('BLANKET', 'SOFT'),
(0.15,0.40):('GBA', 'GAMES'),
}
GIRLS = ['Laura', 'Samantha']
BOYS = ['Mike','Fred']
GIRLLIST = [{girl: GIRLTOYPRICES} for girl in GIRLS]
BOYLIST = [{boy: BOYTOYPRICES} for boy in BOYS]
ENTIRELIST = GIRLMAP + BOYMAP
print ENTIRELIST
[{'Laura': {(0.0, 0.05): ('PEN', 'BLUE'), (0.08, 0.12): ('TOP', 'STRIPY'), (0.05, 0.08): ('GLASSES', 'DESIGNER')}}, {'Samantha': {(0.0, 0.05): ('PEN', 'BLUE'), (0.08, 0.12): ('TOP', 'STRIPY'), (0.05, 0.08): ('GLASSES', 'DESIGNER')}}, {'Mike': {(0.0, 0.1): ('BOOK', 'HARRY POTTER'), (0.15, 0.4): ('GBA', 'GAMES'), (0.1, 0.15): ('BLANKET', 'SOFT')}}, {'Fred': {(0.0, 0.1): ('BOOK', 'HARRY POTTER'), (0.15, 0.4): ('GBA', 'GAMES'), (0.1, 0.15): ('BLANKET', 'SOFT')}}]
Any help would be appreciated.
Kind of a weird data structure, but:
for person in ENTIRELIST:
person_name, toys = person.items()[0]
if person_name != name: # inverted to reduce nesting
continue
for (price_min, price_max), toy in toys.items():
if price_min <= price < price_max:
return toy
This is simpler (and more effective):
GIRLMAP = {girl: GIRLTOYPRICES for girl in GIRLS}
BOYMAP = {boy: BOYTOYPRICES for boy in BOYS}
ENTIREMAP = dict(GIRLMAP, **BOYMAP)
for (price_min, price_max), toy in ENTIREMAP[name].items():
if price_min <= price < price_max:
return toy