Related
I am brand new to Python and I am starting a BS in data analytics in August I am trying to get a head start on learning. Can anyone solve this for me?
from collections import Counter
Counter(one_d)
returns the following
Counter({'Action': 303,
'Adventure': 259,
'Sci-Fi': 120,
'Mystery': 106,
'Horror': 119,
'Thriller': 195,
'Animation': 49,
'Comedy': 279,
'Family': 51,
'Fantasy': 101,
'Drama': 513,
'Music': 16,
'Biography': 81,
'Romance': 141,
'History': 29,
'Crime': 150,
'Western': 7,
'War': 13,
'Musical': 5,
'Sport': 18})
I would like to create a Barplot but am unsure how to do this. Is barplot even the best choice for this data?
The Pandas library is quite useful for data analytics and visualization:
from collections import Counter
import pandas as pd
counts = Counter({'Action': 303, 'Adventure': 259, 'Sci-Fi': 120, 'Mystery': 106, 'Horror': 119, 'Thriller': 195, 'Animation': 49, 'Comedy': 279, 'Family': 51, 'Fantasy': 101, 'Drama': 513, 'Music': 16, 'Biography': 81, 'Romance': 141, 'History': 29, 'Crime': 150, 'Western': 7, 'War': 13, 'Musical': 5, 'Sport': 18})
data = pd.Series(counts)
ax = data.plot.bar()
ax.set(xlabel='Genre', ylabel='Count', title='Good luck on your BS')
Suppose my dataframe has 750 rows in a column and I want to convert that column to an image array of (20,20) numpy array. How to do that?
EDIT1: I want to use the array for ax.contourf (x,y,z) as z. I got x,y by doing x,y=np.meshgrid(df3.x,df.y) now I want to convert another column to an (n,n) array to vary the colors inside the contours using the z parameter.
EDIT2:x,y,z Data to plot contour plot
Given the fact that you have x and y as columns already, I would use pivot_table like this :
df = pd.DataFrame(
columns=["z", "x", "y"],
data=[
[-0.00222, 38, 46.1], [-0.00374, 30.5, 68.4], [0.001043, 36, 86.9], [0.003473, 52.9, 95.3], [0.001382, 50.6, 80.3], [-0.00486, 37, 92.7], [-0.00016, 29.8, 69.5], [0.001368, 34.7, 27.2], [-0.0016, 37.5, 63], [-0.00181, 45.5, 92], [0.003635, 53.6, 82.4], [-0.00363, 46.7, 91.1], [0.002253, 54.1, 97.8], [-0.00089, 43.8, 87], [-0.00115, 41, 62.7], [-0.001, 47.5, 23.2], [0.001332, 61.2, 4.7], [-0.00361, 45.9, 13.9], [0, 47.4, 32], [0, 54.1, 66.6], [0.000891, 53.2, 7.3], [-0.00133, 56.1, 15.8], [-0.00045, 43.6, 19.3], [0.004365, 68.8, 94.2], [-0.00355, 71.1, 76.1], [0, 71.3, 64.8], [-0.00415, 65.7, 59.4], [0.003881, 72.3, 47], [-0.00727, 54.7, 68.4], [0.001848, 63.3, 81.3], [0.002518, 69.7, 93.8], [-0.00252, 65.2, 84], [-0.0025, 55.8, 67.5], [0.004365, 68.2, 94.5], [0.008311, 76.4, 85.4], [0.000208, 35.4, 70.5], [-0.00021, 45, 92.7], [-0.00189, 32, 75.5], [0.000219, 32.3, 38.3], [0.001522, 33.4, 61.7], [0, 37.6, 37.1], [0.0016, 47.6, 32.5], [0.000595, 59.1, 7.1], [0, 63.1, 5], [0.00212, 55.3, 26.5], [0.002495, 64.7, 27.4], [0.00197, 79.4, 24.2], [0.006561, 75.2, 38.9], [0.011262, 82.3, 53.3], [0.019798, 88.7, 25.6], [-0.0198, 78.9, 45.2], [0.000602, 22.6, 74.7], [-0.00082, 29, 93.5], [0.002707, 43.8, 95], [0.000497, 27.7, 45.4], [-0.00131, 19.3, 28.7], [0.000152, 19.2, 38.3], [-0.00015, 23.5, 64.5], [0.001311, 31.7, 41.9], [0.004164, 54.8, 28.4], [-0.00185, 57.8, 32], [0.004771, 71.1, 69.3], [0.010728, 87.7, 92.3], [-0.0063, 81.6, 71.6], [0.079499, 84.6, 52.1], [-0.07294, 78.2, 52.1], [-0.00656, 80.6, 64.8], [0, 70.7, 67.2], [0.00541, 76.1, 75.5], [0.003548, 81.6, 93.2], [-0.00355, 74.9, 84.8], [-0.00016, 68.6, 60], [0.000164, 70, 17.2], [0.005518, 78.8, 16.3], [-0.00466, 64, 27.7], [0.000619, 66.5, 46], [-0.00552, 71.6, 77.9], [-0.00027, 72, 50.7], [0.002583, 46, 87.2], [0.007159, 69.6, 75.6], [0.005518, 77.1, 84.9], [-0.00729, 79.4, 92.4], [0.00197, 75.2, 75], [-0.00011, 74.7, 19.3], [0.011137, 90.9, 22.6], [0.006561, 77.8, 41.2], [-0.00186, 14.3, 38.6], [0.000497, 28, 61.2], [0.001303, 38.9, 55.7], [0.002049, 52.4, 41.8], [-0.00205, 41.9, 51.3], [-0.00055, 46.7, 70.2], [0.004365, 71.8, 94.5], [-0.00477, 65.8, 67.8], [0.00541, 75.5, 84.2], [-0.00541, 74.5, 72.2], [0.00541, 77.8, 85.7], [-0.00568, 72, 54.5], [0.001221, 46.9, 3.5], [0.003635, 58.2, 14.9], [0.000446, 45.2, 34], [0.004771, 68.4, 37.4], [0, 71.3, 55.1], [0, 67.6, 61.8], [0.022626, 94.2, 84.6], [-0.0026, 45.8, 69.8], [0.002565, 55.7, 74.9], [0.007159, 68, 76.1], [-0.00027, 71.1, 38.3], [0.019798, 91.3, 70.7], [0, 92.6, 92.4], [-0.01073, 74.6, 74.6], [0.005318, 87.2, 92.6], [-0.0063, 79.8, 70.5], [0.017925, 98.1, 78], [0.000668, 34.4, 91.2], [0, 36.2, 92.9], [0.000885, 38.2, 75.9], [-0.00094, 21.7, 47], [0.000939, 30.2, 31.3], [-0.00088, 37.9, 6.2], [-0.00016, 27.4, 36.7], [-0.00048, 39.8, 87.2], [0.005303, 63.2, 95.9], [0.001901, 61.8, 70.5], [0.000652, 22, 89.1], [0.003699, 33.6, 80.3], [0.005125, 60.3, 58.1], [0, 63.1, 38.5], [0, 65, 60.2], [0, 60.9, 39.1], [0.004152, 67.7, 27.1], [0.003441, 77, 9.7], [-0.00344, 74.6, 28], [0, 70.9, 49.7], [0.012118, 90.9, 23.8], [0.000366, 25.2, 93.3], [-0.00212, 58, 94.4], [-0.00225, 42.4, 93.5], [0.003635, 52, 85.7], [0.001332, 58.7, 94.5], [-0.00133, 53.9, 84.6], [-0.00388, 58.9, 45.1], [0.006956, 67, 48.2], [-0.007, 51.9, 67.8], [-0.00416, 39.1, 29.8], [-0.00253, 19.8, 44], [-0.00044, 45, 30.4], [-0.001, 45.9, 77.4], [0.002565, 53.1, 66.8], [-0.00416, 41.5, 35.2], [-0.00055, 46.9, 66], [0.000595, 64.1, 96], [0.001901, 61.2, 68.3], [-0.00252, 62.9, 9.4], [0, 73.4, 7.1], [0.025256, 94.3, 13.3], [-0.01084, 72.3, 16.4], [0.010991, 75.2, 56.3], [0.010999, 89.2, 87.5], [0.000164, 72.5, 80.4], [0.000107, 72, 69.6], [-0.00027, 73, 38.6], [0.002273, 56.7, 83.3], [-0.00035, 23.4, 77.6], [0.000353, 29.8, 87.5], [0.002707, 44, 95], [-0.00034, 34.3, 84.5], [-0.00122, 39.4, 59.7], [0.001219, 47.9, 77.1], [0.001001, 42.4, 58.1], [-0.00055, 43.9, 31.4], [0.000555, 45.1, 55.7], [0.003857, 58.6, 75.6], [0.002518, 67.5, 96.5], [-0.00059, 55.8, 73.7], [-0.00201, 49.5, 56.3], [-0.00078, 54, 17], [0.004152, 72, 66.5], [0.003441, 78.9, 91.4], [-0.00355, 70.4, 79.8], [-0.00027, 69.2, 61.7], [0.000271, 68.3, 36.2], [0.003441, 78.8, 11.2], [0, 82.8, 15.4], [-0.00098, 75.9, 33.5], [0, 75.2, 67.5], [0.012118, 90.5, 87], [0, 42.1, 10.4], [0.001808, 48.4, 31.4], [0.007266, 69.4, 26.6], [0.003548, 78.6, 6.2], [0.000989, 80.3, 28.1], [-0.00443, 74.8, 28.9], [0.001417, 10.9, 38.6], [0, 9.2, 46.3], [0.000497, 32.9, 49.4], [-0.0005, 8.6, 48.1], [0.000277, 28.1, 32.2], [-0.00232, 12.5, 23.3], [0.000991, 30.2, 3.5], [0.000818, 30.7, 18.7], [-0.00021, 43.8, 92.9], [0.000479, 39.6, 54.9], [0.00222, 48.8, 46], [-3.93E-05, 53.7, 29.9], [-0.00166, 35.1, 50.7], [-0.00116, 17.8, 45.1], [0.002461, 33.6, 44], [-0.00014, 46.2, 4], [0.001382, 54.5, 19.6], [3.93E-05, 54.5, 50.9], [0.001251, 73.6, 92.4], [0.000989, 75.2, 68.1], [-0.01036, 75.6, 94.4], [-0.00252, 66.5, 80.3], [0.005419, 69.7, 66.3], [0, 74.4, 74.4], [0, 70.9, 66.3], [0, 72.5, 72.5], [-0.00027, 67.6, 55.2], [0.016548, 90, 87.2], [0.067381, 86.9, 60.2], [-0.00656, 79.2, 71.6], [-0.0047, 73.7, 52.1], [0.005681, 78.1, 19.1], [0.006561, 78.2, 46.3], [-0.00656, 77.2, 69.3], [-0.0047, 72, 58.7], [-0.0045, 66, 67.4], [-0.00246, 53, 51.3], [0.007227, 66.9, 66.2], [0.003441, 79.8, 90.5], [-0.00016, 72, 54.5], [0.019798, 86.4, 29.5], [0.009515, 97.7, 25.6], [0.016818, 89.1, 76.8], [-0.01212, 80.7, 68.6], [0.013629, 85.6, 96.9], [-0.00729, 81, 96], [-0.00355, 71, 82.1], [-0.00011, 70.4, 24.5], [0.005518, 76.7, 14.9], [-0.00027, 71, 45.2], [0.005681, 79, 85.4], [-0.00568, 72.5, 53.6], [0, 72.5, 41.9], [0, 29.7, 66], [0, 31.7, 72.6], [0, 29.7, 64.2], [0.00222, 44.1, 44.9], [-0.0027, 37.8, 17.8], [0.000336, 44.3, 5], [-0.00034, 37.6, 16.4], [0.000336, 44.4, 12.1], [-0.00024, 29.9, 88.2], [0.000589, 23.2, 70.2], [0.001522, 33.9, 42.8], [-0.0006, 5.7, 45.7], [0.000602, 23, 37.1], [-0.00024, 25.9, 4.3], [-0.0042, 40.7, 68], [0.001154, 43.4, 87], [0.000891, 55.2, 93.5], [-0.00225, 42, 88.7], [-0.00138, 31.2, 74.7], [-0.00094, 16.8, 49.4], [0.002526, 41.4, 72.9], [0.000891, 55.9, 94.5], [-0.00133, 53, 75.3], [0.010668, 77.5, 95.7], [0.000989, 76.1, 68.1], [0.019798, 87.9, 30.8], [0.003771, 72.5, 96.5], [0.002253, 54.5, 93.2], [-0.00225, 43.3, 92.7], [-0.00271, 27.4, 91.7], [0.001051, 14.2, 54], [0.003464, 43.5, 67.1], [0.000555, 47.1, 57.5], [0.00201, 56.5, 72.5], [0.00187, 73.4, 95.6], [0.008311, 75.8, 77.9], [-0.00541, 67.2, 69.9], [-0.00027, 73.9, 54.6], [-0.01126, 71.8, 57], [0.004701, 81.4, 34.4], [0.000232, 9.2, 40.6], [0.000277, 26.3, 26.8], [-0.00075, 4.1, 51.5], [-0.00062, 8.5, 83.6], [0.002706, 46.6, 93.9], [0.003881, 68.7, 58.8], [0, 74.6, 47.5], [0, 70.7, 47.5], [-0.00149, 30.1, 90.6], [0.002097, 36.6, 65.4], [6.53E-05, 34.2, 30.3], [0.002044, 54.9, 4.1], [0, 54, 10.7], [0.004771, 68.9, 73.8], [0.00541, 82, 82.7], [-0.00098, 75.8, 64.2], [0, 79.5, 74.9], [-0.00552, 74.9, 75.9], [0.016944, 92.2, 85.5], [-0.00634, 70, 92.4], [0, 73.7, 96.2], [-0.00377, 61.8, 96.2], [0.00212, 53.7, 69], [0.002495, 59.7, 66.9], [0, 74, 83.4], [0.005518, 77.8, 86.3], [0.003548, 80.9, 93.6], [0, 79.1, 51.9], [-0.01126, 73.6, 55.5], [-0.00733, 75, 93.3], [-0.00344, 74.4, 71.3], [-0.00477, 66.3, 71], [0.004664, 68.1, 75.9], [0.000107, 69.9, 65.6], [-0.00027, 74.3, 55.7], [0, 66.7, 93], [0.002901, 68.1, 72.3], [0.00443, 75.2, 74.9], [0, 72.8, 92.3], [0.006342, 77.6, 95.6], [-0.00634, 70, 88.1], [-0.00016, 68.6, 57.8], [-0.00062, 66.4, 35.5], [0.000271, 73, 68.7], [-0.00388, 65.7, 53.7], [0.007592, 81.5, 11.3], [-0.01655, 74.3, 29.3], [-0.00388, 61.3, 53.9], [0.003881, 69, 49.9], [0.000271, 74.3, 64.5], [0.012118, 88.9, 86.1], [0.002565, 50.1, 66], [0.004326, 71.7, 7.1], [-0.0047, 71.3, 41.5], [0.004701, 79.3, 64.1], [-0.00843, 92.7, 76.2], [0.001253, 59.4, 80.3], [0, 56, 33.7], [0.000595, 65.4, 5.3], [0.001253, 61.5, 14.2], [0.001267, 65.2, 37.9], [0.003548, 77.4, 93.2], [-0.00355, 72.7, 85.4], [-0.00016, 70.7, 54.2], [0.026359, 91.4, 72.9], [-0.00729, 76.8, 6.1], [-0.00022, 29.6, 70.2], [0.001522, 36.5, 61.8], [0.011496, 67, 37.4], [-0.00477, 64.4, 26.8], [-0.00027, 74.5, 39.8], [0.019798, 90.3, 68.6], [0.000367, 14.6, 69], [0.001327, 28.8, 63.3], [0.001043, 37, 78.9], [0.000336, 42.2, 95.3], [-0.00189, 27.2, 85.1], [0.00222, 46.6, 56.6], [0.001328, 26.1, 93.8], [0.002707, 42.3, 92.3], [0.00011, 44.9, 38.5], [0.0045, 67.6, 38.8], [0, 62, 45.7], [0.007592, 75.3, 91.2], [0, 18.8, 55.4], [-0.00039, 13.4, 68.1], [-0.00014, 46.5, 89.7], [0, 42.4, 89.1], [0.002253, 53.8, 95], [0.005318, 85.7, 89.4], [-0.01212, 75.1, 66.3], [0.005681, 80.7, 14.6], [-0.00098, 76.1, 31.3], [-0.00027, 67.3, 56.9], [0.003712, 82.5, 87.8], [-0.01212, 80.2, 70.4], [-0.00541, 70.1, 74.4], [0.00541, 79.9, 85.8], [0, 75.2, 75.9], [-0.00098, 79.7, 72.9], [-0.00443, 72.1, 72.6], [0.00443, 76.6, 74.7], [-0.00443, 70.6, 73.5], [0.00443, 78.7, 65], [-0.0047, 72.7, 54.5], [0.003712, 82.8, 11.5], [-0.01655, 74.7, 29.8], [-0.00027, 73.3, 41.9], [0, 74.2, 57.6], [0.016548, 84.6, 86.1], [-0.01665, 74.3, 75], [0.004537, 80.3, 71.4], [-0.00116, 27.8, 47.3], [0, 39.1, 93.5], [0.003473, 53.5, 96], [-0.00122, 40.4, 91.1], [-0.00122, 34.4, 51], [0.002109, 52.5, 5.3], [0.00212, 52.7, 25.7], [0, 55.6, 36.7], [0.006996, 67.1, 43.9], [-0.00388, 62.7, 45.4], [0.001251, 74.1, 90.6], [0, 73.9, 63.3], [0, 70.7, 67.2], [-0.00027, 68.1, 44.3], [0, 65, 60.9], [0.004045, 73.5, 83.6], [0.00443, 81.6, 68.9], [1.33E-05, 22.2, 78], [0.010728, 85.8, 87.8], [-0.01665, 72.3, 78.6], [-0.00011, 72.6, 13.7], [0.00201, 51.7, 65.3], [-0.00256, 45.9, 68.8], [0.001848, 59.7, 78.6], [0.005419, 68.6, 73.2], [-0.00656, 78, 36.4], [0.000981, 80.4, 87.2], [-0.00541, 74.7, 71.3], [-0.00308, 87.4, 92.6], [-0.00532, 76.6, 85.8], [0.005318, 86.8, 93.2], [-0.01084, 72.8, 76.5], [0.004701, 81.8, 29.6], [0.006561, 81.9, 47.8], [-0.00089, 46.9, 79.8], [-0.00122, 39.3, 45.2], [0.00222, 46.3, 59.6], [0.001272, 55.6, 87.3], [0.002715, 64.9, 96.2], [-0.00125, 64, 91.4], [0.001253, 65.4, 78.9], [-0.00125, 63, 87.9], [-0.00059, 54.7, 73.4], [-0.00074, 54, 16.6], [0.001332, 60.6, 6.5], [0.000619, 64.9, 59.6], [0.001251, 72.3, 89.1], [0.011137, 85.4, 84.6], [0.001332, 66.6, 93.6], [-0.00252, 64.2, 86.3], [0.005312, 74.1, 77.6], [-0.00531, 66, 77.1], [0.002518, 70.1, 93.3], [-0.00252, 64.5, 83.6], [-0.00185, 54.7, 65.5], [-3.93E-05, 51.5, 35.2], [0.000595, 60.9, 4.7], [-0.00059, 58.3, 30.5], [-0.00415, 63, 52], [0.004664, 67.1, 77.4], [0.005518, 83, 87.3], [-0.0198, 79.4, 60.3], [0.202542, 97, 44.5], [0.000367, 10.1, 36.7], [-0.00037, 7.9, 49.3], [0, 29.8, 48.1], [0.001043, 37.5, 80], [0, 34.4, 54.2], [-0.00246, 24.3, 58.2], [0.002461, 35.9, 58.1], [-0.00048, 33.5, 86], [-0.00088, 41.6, 95.3], [0, 36, 89.6], [0.00305, 55.7, 56.9], [-0.0027, 37.1, 13.7], [0.003473, 54.3, 4.1], [0.000738, 56, 37.3], [3.93E-05, 53.8, 59.7], [0.00187, 74.6, 90.2], [0, 75.9, 66.3], [0.012118, 87.2, 83.7], [-0.01212, 75.1, 71.9], [0.012118, 87.2, 19.6], [-0.00541, 75, 29.9], [0.000164, 70.3, 75.8], [0.005518, 82.1, 86], [-0.00541, 74.2, 69.9], [-0.00027, 74.4, 60.8], [0.0842, 88.8, 62.3], [0.002518, 70.1, 91.4], [0.013107, 91.5, 85.1], [-0.00729, 81.8, 95.7], [-0.00355, 74.7, 80.6], [0.000107, 74.9, 66], [0, 72.1, 72.2], [0.000164, 69.7, 20.8], [0.000107, 71.4, 27.2], [-0.00477, 64.5, 32.5], [-0.00477, 65.4, 70.2], [0.004664, 70.2, 86], [0.003548, 80.7, 92.9], [-0.00355, 73.9, 78.5], [0.026359, 89.4, 68.7], [0, 92.4, 53.6], [0.061574, 84.9, 39.8], [0.011696, 76.6, 25.9], [-0.00027, 74.3, 57.9], [0.003631, 53.2, 12.2], [0, 59.8, 53.7], [0.004152, 70.6, 71.3], [0, 69.8, 64.7], [0.011262, 76.7, 53.4], [-0.00388, 66, 46.4], [0.020699, 86.3, 81], [0.011137, 90, 81], [-0.01114, 79.2, 75.2], [-0.00308, 84.5, 95.6], [-0.01084, 71.5, 86.4], [0.010836, 86.6, 94.4], [-0.01363, 73.5, 91.7], [0.002794, 69.6, 77.7], [0.005518, 76.5, 76.2], [0.004204, 54.4, 57], [0.003075, 60.3, 50.1], [-0.00427, 40.3, 54.9], [0.001219, 44.7, 84.3], [0.000891, 58.2, 94.5], [0, 56, 89.6], [0, 51.2, 88.7], [0.000738, 52.8, 65.4], [0, 57.8, 69.5], [0.002495, 61.5, 63.9], [0, 65, 69.3], [-0.00377, 65.2, 89.7], [-0.00185, 54.3, 64.1], [0, 51.6, 65.1], [0.001272, 50.8, 21.8], [0, 55, 33.5], [-0.00575, 33, 30.8], [-0.0006, 7.5, 51], [-0.00191, 7.1, 20.6], [0.000531, 2.9, 43.7], [0.001656, 45, 89.1], [-0.00125, 66.3, 89.3], [0, 65.4, 67.2], [0.016818, 85, 13.1], [-0.02636, 78.4, 36.8], [0.005681, 78.8, 78.2], [-0.00098, 79.8, 71.1], [0.006561, 75.7, 43], [0, 68.7, 46.4], [0.005681, 78.3, 19.7], [-0.01665, 71.7, 24.8], [0.003441, 80.7, 11.8], [-0.00355, 68.2, 20.2], [-0.00415, 65, 60.6], [0, 72.7, 57.2], [-0.00011, 67.9, 79.1], [0.000107, 71.6, 74.9], [-0.00011, 67.2, 78.6], [-0.0042, 37.3, 27.7], [-0.00054, 37.8, 14.3], [0.000545, 37.4, 36.1], [-0.00152, 32, 65.9], [0.000219, 29.8, 45.1], [-0.00022, 31.7, 63.6], [0.001303, 39.8, 40.7], [0.000823, 41, 20], [-0.00067, 29.7, 23.5], [0.003187, 44.3, 69.5], [0.00506, 64.5, 68.7], [0.002518, 74.1, 99.3], [0.00755, 80.7, 59.7], [-0.03587, 81, 62.6], [0.009374, 93.8, 5.3], [0.010363, 95.3, 6.5], [-0.01793, 82.2, 28.1], [0.000388, 20.7, 41.9], [-0.0019, 23.5, 24.7], [0.000814, 12.2, 51.8], [0.000277, 31.1, 66.2], [0.002144, 48.1, 72.3], [0.010836, 84.4, 94.1], [0.0045, 69.2, 56], [0.001303, 38.1, 45.4], [-0.0013, 25.8, 49.4], [-0.00062, 66.3, 25.6], [0, 57.4, 90.8], [0.003115, 61.3, 43.3], [-0.00066, 17.1, 43.1], [0.000662, 10.3, 56.7], [0.000497, 27.6, 59.3], [0.003187, 44.1, 72.2], [-0.00303, 39.6, 92.1], [-0.00149, 32.8, 90.9], [0.003473, 55, 96.2], [-0.00347, 39.5, 89.3], [0.000479, 33.9, 41], [0.002565, 52.4, 33.4], [0.001848, 62.5, 13.3], [0.000648, 62.2, 31], [0.004152, 67.1, 66.2], [0.00541, 81.6, 84.2], [0.0265, 95.9, 64.8], [0.001552, 37.7, 78.8], [0, 39, 87.2], [0.00853, 70.5, 96.2], [-0.00062, 9.6, 14.5], [0.001809, 26.4, 18.8], [0.000668, 40.1, 6.8], [3.93E-05, 55.3, 39.2], [0.004152, 68, 68.6], [0.00541, 77.5, 84.2], [0.000107, 72, 65.7], [0.00622, 61.8, 14.8], [-0.00065, 58.5, 13.9], [0.002518, 68.2, 7.9], [-0.00011, 72.2, 24.5], [0.016655, 85.7, 15.5], [-0.02636, 75.3, 29.6], [-0.00027, 72.4, 49.6], [0.000366, 29.9, 88.7], [0.009959, 72.8, 87.8], [-0.01212, 76.3, 67.8], [0.00443, 82.2, 27.4], [-3.93E-05, 57, 64.2], [-0.00201, 45.6, 46.3], [0.005125, 59.7, 47.6], [-0.00062, 63.7, 63.5], [0.004664, 73.9, 76.4], [-0.00098, 75.6, 27.7], [0.026359, 85, 32.2], [0.000948, 23.1, 90.8], [0.002915, 36, 31.7], [0.002044, 57, 10.4], [0.003473, 57.4, 96.5], [-0.00027, 67.8, 55.2], [0.005681, 75.2, 17], [-0.00088, 39.3, 9.5], [0.006188, 60.3, 4.7], [0.002901, 67.2, 26], [0.030789, 86.1, 74.3], [-0.0198, 76.8, 57.8], [-0.00252, 65.7, 91.5], [-0.00133, 58.3, 86.1], [0.000738, 55.8, 67.2], [0.002495, 58.9, 70.4], [0.003881, 71.7, 45.4], [-0.00263, 68.7, 97.5], [-1.33E-05, 6.4, 54.2], [0.001656, 46.7, 93.5], [-0.00377, 62.9, 8.2], [0.001267, 60, 61.5], [-0.00062, 65.4, 67.4], [0.001251, 71.4, 8.5], [0.000755, 21.1, 58.5], [-0.00075, 6, 52.4], [0.008524, 60.2, 66.2], [0, 66.4, 70.4], [0.004771, 67.7, 74.3], [0, 72.2, 62.9], [0, 72.2, 68.4], [0.040304, 98, 73.8],
]
)
df2 = df.pivot_table("z", index="y", columns="x", aggfunc="sum").fillna(0)
array = df2.sort_index(ascending=False).values
The beauty of pivot_table is that this will sort your x/y values.
Be carefull though, your y value would be ascending (top to bottom), which is the opposite of what you would want in a picture. You have to re-sort the index in reverse mode (hence the sort_index command).
There is also an assumption here : that your x's and y's values are linear OR that you are not trying to plot anything linear. In fact, you data sample are not (for instance, your first discrete y values are 3.5, 4, 4.1) ; you simply cannot use that kind of coordinates for anything in a grid format.
Given the fact that your datas' coordinates are all float with one decimal (at most), you could complete your datas this way :
all_xs = all_ys = range(0, 1001) # get all discrete xs and ys values (on an integer basis)
missing_xs = list(set(all_xs) - set(df['x']*10)) #find all missing xs
missing_ys = list(set(all_ys) - set(df['y']*10)) #find all missing ys
dummy_df = pd.DataFrame(0, index=missing_ys, columns=missing_xs) #construct a dummy dataframe with those xs and ys (filled with zeros)
df2.index *= 10 #convert your indexes and columns of the previous df2 to integers
df2.columns *= 10
df3 = df2.append(dummy_df).fillna(0) #append the dataframes, which will create the missing columns and index and fill all empty values with zeros
df3.sort_index(inplace=True, ascending=False) #resort the index (ie ys values)
df3.sort_index(axis=1, inplace=True, ascending=True) #resort the columns (ie xs values)
print(df3.shape) #make sure the shape is a square array
array = df3.values #extract the numpy array
you can convert the column data into a numpy array and reshape it to your required dimensions:
In [57]: df = pd.DataFrame({"A": [1, 2,3,4,5,6], "B": [3.0, 4.5,9,8,36,6]})
In [58]: df
Out[58]:
A B
0 1 3.0
1 2 4.5
2 3 9.0
3 4 8.0
4 5 36.0
5 6 6.0
In [59]: res= np.array(df['B'].values.tolist())
In [60]: res
Out[60]: array([ 3. , 4.5, 9. , 8. , 36. , 6. ])
In [61]: res.reshape(3,2)
Out[61]:
array([[ 3. , 4.5],
[ 9. , 8. ],
[36. , 6. ]])
I have got some code here to solve a challenge problem(If you need to see it just ask):
from collections import deque
def breadthfirstsearch(graph, start, goal):
if start == goal:
return [start]
visited = {start}
queue = deque([(start, [])])
while queue:
current, path = queue.popleft()
visited.add(current)
for neighbor in graph[current]:
if neighbor == goal:
return path + [current, neighbor]
if neighbor in visited:
continue
queue.append((neighbor, path + [current]))
visited.add(neighbor)
return None
'''
ONLY FOR CHALLENGE PROBLEM IRRELEVANT NOW
x = 0 #int(input())
final =[]
for i in range(x):
a, b, c, d = list(map(int, input().split()))
lne = []
for j in range(b):
t = list(map(int, input().split()))
lne.append(t)
r = [c, d, lne]
final.append(r)
'''
roadCounts = 0
final = [[100, 1000, [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16], [16, 17], [17, 18], [18, 19], [19, 20], [20, 21], [21, 22], [22, 23], [23, 24], [24, 25], [25, 26], [26, 27], [27, 28], [28, 29], [29, 30], [30, 31], [31, 32], [32, 33], [33, 34], [34, 35], [35, 36], [36, 37], [37, 38], [38, 39], [39, 40], [40, 41], [41, 42], [42, 43], [43, 44], [44, 45], [45, 46], [46, 47], [47, 48], [48, 49], [49, 50], [50, 51], [51, 52], [52, 53], [53, 54], [54, 55], [55, 56], [56, 57], [57, 58], [58, 59], [59, 60], [60, 61], [61, 62], [62, 63], [63, 64], [64, 65], [65, 66], [66, 67], [67, 68], [68, 69], [69, 70], [70, 71], [71, 72], [72, 73], [73, 74], [74, 75], [75, 76], [76, 77], [77, 78], [78, 79], [79, 80], [80, 81], [81, 82], [82, 83], [83, 84], [84, 85], [85, 86], [86, 87], [87, 88], [88, 89], [89, 90], [90, 91], [91, 92], [92, 93], [93, 94], [94, 95], [95, 96], [96, 97], [97, 98], [98, 99], [99, 100], [100, 101], [101, 102], [102, 103], [103, 104], [104, 105], [105, 106], [106, 107], [107, 108], [108, 109], [109, 110], [110, 111], [111, 112], [112, 113], [113, 114], [114, 115], [115, 116], [116, 117], [117, 118], [118, 119], [119, 120], [120, 121], [121, 122], [122, 123], [123, 124], [124, 125], [125, 126], [126, 127], [127, 128], [128, 129], [129, 130], [130, 131], [131, 132], [132, 133], [133, 134], [134, 135], [135, 136], [136, 137], [137, 138], [138, 139], [139, 140], [140, 141], [141, 142], [142, 143], [143, 144], [144, 145], [145, 146], [146, 147], [147, 148], [148, 149], [149, 150], [150, 151], [151, 152], [152, 153], [153, 154], [154, 155], [155, 156], [156, 157], [157, 158], [158, 159], [159, 160], [160, 161], [161, 162], [162, 163], [163, 164], [164, 165], [165, 166], [166, 167], [167, 168], [168, 169], [169, 170], [170, 171], [171, 172], [172, 173], [173, 174], [174, 175], [175, 176], [176, 177], [177, 178], [178, 179], [179, 180], [180, 181], [181, 182], [182, 183], [183, 184], [184, 185], [185, 186], [186, 187], [187, 188], [188, 189], [189, 190], [190, 191], [191, 192], [192, 193], [193, 194], [194, 195], [195, 196], [196, 197], [197, 198], [198, 199], [199, 200], [200, 201], [201, 202], [202, 203], [203, 204], [204, 205], [205, 206], [206, 207], [207, 208], [208, 209], [209, 210], [210, 211], [211, 212], [212, 213], [213, 214], [214, 215], [215, 216], [216, 217], [217, 218], [218, 219], [219, 220], [220, 221], [221, 222], [222, 223], [223, 224], [224, 225], [225, 226], [226, 227], [227, 228], [228, 229], [229, 230], [230, 231], [231, 232], [232, 233], [233, 234], [234, 235], [235, 236], [236, 237], [237, 238], [238, 239], [239, 240], [240, 241], [241, 242], [242, 243], [243, 244], [244, 245], [245, 246], [246, 247], [247, 248], [248, 249], [249, 250], [250, 251], [251, 252], [252, 253], [253, 254], [254, 255], [255, 256], [256, 257], [257, 258], [258, 259], [259, 260], [260, 261], [261, 262], [262, 263], [263, 264], [264, 265], [265, 266], [266, 267], [267, 268], [268, 269], [269, 270], [270, 271], [271, 272], [272, 273], [273, 274], [274, 275], [275, 276], [276, 277], [277, 278], [278, 279], [279, 280], [280, 281], [281, 282], [282, 283], [283, 284], [284, 285], [285, 286], [286, 287], [287, 288], [288, 289], [289, 290], [290, 291], [291, 292], [292, 293], [293, 294], [294, 295], [295, 296], [296, 297], [297, 298], [298, 299], [299, 300], [300, 301], [301, 302], [302, 303], [303, 304], [304, 305], [305, 306], [306, 307], [307, 308], [308, 309], [309, 310], [310, 311], [311, 312], [312, 313], [313, 314], [314, 315], [315, 316], [316, 317], [317, 318], [318, 319], [319, 320], [320, 321], [321, 322], [322, 323], [323, 324], [324, 325], [325, 326], [326, 327], [327, 328], [328, 329], [329, 330], [330, 331], [331, 332], [332, 333], [333, 334], [334, 335], [335, 336], [336, 337], [337, 338], [338, 339], [339, 340], [340, 341], [341, 342], [342, 343], [343, 344], [344, 345], [345, 346], [346, 347], [347, 348], [348, 349], [349, 350], [350, 351], [351, 352], [352, 353], [353, 354], [354, 355], [355, 356], [356, 357], [357, 358], [358, 359], [359, 360], [360, 361], [361, 362], [362, 363], [363, 364], [364, 365], [365, 366], [366, 367], [367, 368], [368, 369], [369, 370], [370, 371], [371, 372], [372, 373], [373, 374], [374, 375], [375, 376], [376, 377], [377, 378], [378, 379], [379, 380], [380, 381], [381, 382], [382, 383], [383, 384], [384, 385], [385, 386], [386, 387], [387, 388], [388, 389], [389, 390], [390, 391], [391, 392], [392, 393], [393, 394], [394, 395], [395, 396], [396, 397], [397, 398], [398, 399], [399, 400], [400, 401], [401, 402], [402, 403], [403, 404], [404, 405], [405, 406], [406, 407], [407, 408], [408, 409], [409, 410], [410, 411], [411, 412], [412, 413], [413, 414], [414, 415], [415, 416], [416, 417], [417, 418], [418, 419], [419, 420], [420, 421], [421, 422], [422, 423], [423, 424], [424, 425], [425, 426], [426, 427], [427, 428], [428, 429], [429, 430], [430, 431], [431, 432], [432, 433], [433, 434], [434, 435], [435, 436], [436, 437], [437, 438], [438, 439], [439, 440], [440, 441], [441, 442], [442, 443], [443, 444], [444, 445], [445, 446], [446, 447], [447, 448], [448, 449], [449, 450], [450, 451], [451, 452], [452, 453], [453, 454], [454, 455], [455, 456], [456, 457], [457, 458], [458, 459], [459, 460], [460, 461], [461, 462], [462, 463], [463, 464], [464, 465], [465, 466], [466, 467], [467, 468], [468, 469], [469, 470], [470, 471], [471, 472], [472, 473], [473, 474], [474, 475], [475, 476], [476, 477], [477, 478], [478, 479], [479, 480], [480, 481], [481, 482], [482, 483], [483, 484], [484, 485], [485, 486], [486, 487], [487, 488], [488, 489], [489, 490], [490, 491], [491, 492], [492, 493], [493, 494], [494, 495], [495, 496], [496, 497], [497, 498], [498, 499], [499, 500], [500, 501], [501, 502], [502, 503], [503, 504], [504, 505], [505, 506], [506, 507], [507, 508], [508, 509], [509, 510], [510, 511], [511, 512], [512, 513], [513, 514], [514, 515], [515, 516], [516, 517], [517, 518], [518, 519], [519, 520], [520, 521], [521, 522], [522, 523], [523, 524], [524, 525], [525, 526], [526, 527], [527, 528], [528, 529], [529, 530], [530, 531], [531, 532], [532, 533], [533, 534], [534, 535], [535, 536], [536, 537], [537, 538], [538, 539], [539, 540], [540, 541], [541, 542], [542, 543], [543, 544], [544, 545], [545, 546], [546, 547], [547, 548], [548, 549], [549, 550], [550, 551], [551, 552], [552, 553], [553, 554], [554, 555], [555, 556], [556, 557], [557, 558], [558, 559], [559, 560], [560, 561], [561, 562], [562, 563], [563, 564], [564, 565], [565, 566], [566, 567], [567, 568], [568, 569], [569, 570], [570, 571], [571, 572], [572, 573], [573, 574], [574, 575], [575, 576], [576, 577], [577, 578], [578, 579], [579, 580], [580, 581], [581, 582], [582, 583], [583, 584], [584, 585], [585, 586], [586, 587], [587, 588], [588, 589], [589, 590], [590, 591], [591, 592], [592, 593], [593, 594], [594, 595], [595, 596], [596, 597], [597, 598], [598, 599], [599, 600], [600, 601], [601, 602], [602, 603], [603, 604], [604, 605], [605, 606], [606, 607], [607, 608], [608, 609], [609, 610], [610, 611], [611, 612], [612, 613], [613, 614], [614, 615], [615, 616], [616, 617], [617, 618], [618, 619], [619, 620], [620, 621], [621, 622], [622, 623], [623, 624], [624, 625], [625, 626], [626, 627], [627, 628], [628, 629], [629, 630], [630, 631], [631, 632], [632, 633], [633, 634], [634, 635], [635, 636], [636, 637], [637, 638], [638, 639], [639, 640], [640, 641], [641, 642], [642, 643], [643, 644], [644, 645], [645, 646], [646, 647], [647, 648], [648, 649], [649, 650], [650, 651], [651, 652], [652, 653], [653, 654], [654, 655], [655, 656], [656, 657], [657, 658], [658, 659], [659, 660], [660, 661], [661, 662], [662, 663], [663, 664], [664, 665], [665, 666], [666, 667], [667, 668], [668, 669], [669, 670], [670, 671], [671, 672], [672, 673], [673, 674], [674, 675], [675, 676], [676, 677], [677, 678], [678, 679], [679, 680], [680, 681], [681, 682], [682, 683], [683, 684], [684, 685], [685, 686], [686, 687], [687, 688], [688, 689], [689, 690], [690, 691], [691, 692], [692, 693], [693, 694], [694, 695], [695, 696], [696, 697], [697, 698], [698, 699], [699, 700], [700, 701], [701, 702], [702, 703], [703, 704], [704, 705], [705, 706], [706, 707], [707, 708], [708, 709], [709, 710], [710, 711], [711, 712], [712, 713], [713, 714], [714, 715], [715, 716], [716, 717], [717, 718], [718, 719], [719, 720], [720, 721], [721, 722], [722, 723], [723, 724], [724, 725], [725, 726], [726, 727], [727, 728], [728, 729], [729, 730], [730, 731], [731, 732], [732, 733], [733, 734], [734, 735], [735, 736], [736, 737], [737, 738], [738, 739], [739, 740], [740, 741], [741, 742], [742, 743], [743, 744], [744, 745], [745, 746], [746, 747], [747, 748], [748, 749], [749, 750], [750, 751], [751, 752], [752, 753], [753, 754], [754, 755], [755, 756], [756, 757], [757, 758], [758, 759], [759, 760], [760, 761], [761, 762], [762, 763], [763, 764], [764, 765], [765, 766], [766, 767], [767, 768], [768, 769], [769, 770], [770, 771], [771, 772], [772, 773], [773, 774], [774, 775], [775, 776], [776, 777], [777, 778], [778, 779], [779, 780], [780, 781], [781, 782], [782, 783], [783, 784], [784, 785], [785, 786], [786, 787], [787, 788], [788, 789], [789, 790], [790, 791], [791, 792], [792, 793], [793, 794], [794, 795], [795, 796], [796, 797], [797, 798], [798, 799], [799, 800], [800, 801], [801, 802], [802, 803], [803, 804], [804, 805], [805, 806], [806, 807], [807, 808], [808, 809], [809, 810], [810, 811], [811, 812], [812, 813], [813, 814], [814, 815], [815, 816], [816, 817], [817, 818], [818, 819], [819, 820], [820, 821], [821, 822], [822, 823], [823, 824], [824, 825], [825, 826], [826, 827], [827, 828], [828, 829], [829, 830], [830, 831], [831, 832], [832, 833], [833, 834], [834, 835], [835, 836], [836, 837], [837, 838], [838, 839], [839, 840], [840, 841], [841, 842], [842, 843], [843, 844], [844, 845], [845, 846], [846, 847], [847, 848], [848, 849], [849, 850], [850, 851], [851, 852], [852, 853], [853, 854], [854, 855], [855, 856], [856, 857], [857, 858], [858, 859], [859, 860], [860, 861], [861, 862], [862, 863], [863, 864], [864, 865], [865, 866], [866, 867], [867, 868], [868, 869], [869, 870], [870, 871], [871, 872], [872, 873], [873, 874], [874, 875], [875, 876], [876, 877], [877, 878], [878, 879], [879, 880], [880, 881], [881, 882], [882, 883], [883, 884], [884, 885], [885, 886], [886, 887], [887, 888], [888, 889], [889, 890], [890, 891], [891, 892], [892, 893], [893, 894], [894, 895], [895, 896], [896, 897], [897, 898], [898, 899], [899, 900], [900, 901], [901, 902], [902, 903], [903, 904], [904, 905], [905, 906], [906, 907], [907, 908], [908, 909], [909, 910], [910, 911], [911, 912], [912, 913], [913, 914], [914, 915], [915, 916], [916, 917], [917, 918], [918, 919], [919, 920], [920, 921], [921, 922], [922, 923], [923, 924], [924, 925], [925, 926], [926, 927], [927, 928], [928, 929], [929, 930], [930, 931], [931, 932], [932, 933], [933, 934], [934, 935], [935, 936], [936, 937], [937, 938], [938, 939], [939, 940], [940, 941], [941, 942], [942, 943], [943, 944], [944, 945], [945, 946], [946, 947], [947, 948], [948, 949], [949, 950], [950, 951], [951, 952], [952, 953], [953, 954], [954, 955], [955, 956], [956, 957], [957, 958], [958, 959], [959, 960], [960, 961], [961, 962], [962, 963], [963, 964], [964, 965], [965, 966], [966, 967], [967, 968], [968, 969], [969, 970], [970, 971], [971, 972], [972, 973], [973, 974], [974, 975], [975, 976], [976, 977], [977, 978], [978, 979], [979, 980], [980, 981], [981, 982], [982, 983], [983, 984], [984, 985], [985, 986], [986, 987], [987, 988], [988, 989], [989, 990], [990, 991], [991, 992], [992, 993], [993, 994], [994, 995], [995, 996], [996, 997], [997, 998], [998, 999], [999, 1000]]]]
for roadPaths in final:
home = roadPaths[0]
school = roadPaths[1]
cool = roadPaths[2]
alrd = []
graph = {}
for val in cool:
n1 = val[0]
n2 = val[1]
if n1 not in alrd:
alrd.append(n1)
graph[n1] = [n2]
else:
graph[n1].append(n2)
if n2 not in alrd:
alrd.append(n2)
graph[n2] = [n1]
else:
graph[n2].append(n2)
notAllowed = []
lis = list(graph.keys())
path = breadthfirstsearch(graph, home, school)
for i in lis:
notAllowed.append([i, i])
for key, val in graph.items():
for v in val:
n = sorted([key, v])
if n not in notAllowed:
notAllowed.append(n)
for i in range(len(path)-2):
for j in range(i+2, len(path)):
n = sorted([path[i], path[j]])
notAllowed.append(n)
roads = 0
for i in lis:
for j in lis:
if sorted([i, j]) not in notAllowed:
notAllowed.append(sorted([i, j]))
roads += 1
print(roads)
And everything runs fine until it gets to the last section:
roads = 0
for i in lis:
for j in lis:
if sorted([i, j]) not in notAllowed:
notAllowed.append(sorted([i, j]))
roads += 1
Because there are 1000 elements in lis this loop basically executes 1,000,000 times which makes just this nested for loop insanely slow over half an hour to execute. Does anyone have any idea how I can speed this part up as when I submit the problem into the website I get a runtime error and I suspect that this part of the code is the problem.
The problem lies in the line if sorted([i, j]) not in notAllowed: which is very slow.
Indeed, notAllowed is a list that is growing very quickly and finding an element in a list is done in a linear time (the size of the list).
As a result, your loop as roughly a complexity of O(N^4) (with N=len(lis)).
It is possible to make this loop O(N^2) times faster using a set because finding an element in a set can be done in a constant time (in average).
Here is the result:
notAllowedSet = {tuple(e) for e in notAllowed}
roads = 0
for i in lis:
for j in lis:
elem = (min(i, j), max(i, j))
if elem not in notAllowedSet:
notAllowed.append(list(elem))
notAllowedSet.add(elem)
roads += 1
Please note that since sets cannot hash list-based pairs, I used tuple-based pairs that can be successfully hashed (using a tuple-typed pairs in notAllowed should also be faster than list-based pairs).
Additionally, sets may not preserve the ordering (regarding your version of python) and I do not know if the ordering of notAllowed is important. If the ordering of notAllowed is not important, you can remove notAllowed and only work on notAllowedSet. In such a case, it is probably possible to improve a lot the algorithm (by sorting lis and avoiding a relatively costly set check).
This question already has answers here:
Get country name from Country code in python?
(3 answers)
Closed 4 years ago.
I'm interested in getting a mapping of country codes to international phone number prefixes, like so:
{'US': '+1', 'GB': '+44', 'DE': '+49', ...}
One library that probably contains this information is python-phonenumbers. However, after a quick perusal of the source code I wasn't able to find where this information is stored. For example, the shortdata/region_DE.py module looks like this:
"""Auto-generated file, do not edit by hand. DE metadata"""
from ..phonemetadata import NumberFormat, PhoneNumberDesc, PhoneMetadata
PHONE_METADATA_DE = PhoneMetadata(id='DE', country_code=None, international_prefix=None,
general_desc=PhoneNumberDesc(national_number_pattern='1\\d{2,5}', possible_length=(3, 6)),
toll_free=PhoneNumberDesc(national_number_pattern='116\\d{3}', example_number='116000', possible_length=(6,)),
emergency=PhoneNumberDesc(national_number_pattern='11[02]', example_number='112', possible_length=(3,)),
short_code=PhoneNumberDesc(national_number_pattern='11(?:[025]|6(?:00[06]|1(?:1[17]|23)))', example_number='115', possible_length=(3, 6)),
short_data=True)
It seems like the country_code and international_prefix fields are None. How can I get such a mapping (possibly with a different library)?
You can get the mapping you want using pycountry and phonenumbers, along with a simple dictionary comprehension:
import phonenumbers as pn
import pycountry
dct = {c.alpha_2: pn.country_code_for_region(c.alpha_2) for c in pycountry.countries}
print(dct)
Output:
{'SK': 421, 'KI': 686, 'LV': 371, 'GH': 233, 'JP': 81, 'SA': 966, 'TD': 235, 'SX': 1, 'CY': 357, 'CH': 41, 'EG': 20, 'PA': 507, 'KP': 850, 'CO': 57, 'GW': 245, 'KG': 996, 'AW': 297, 'FM': 691, 'SB': 677, 'HR': 385, 'PY': 595, 'BG': 359, 'IQ': 964, 'ID': 62, 'GQ': 240, 'CA': 1, 'CG': 242, 'MO': 853, 'SL': 232, 'LA': 856, 'OM': 968, 'MP': 1, 'DK': 45, 'FI': 358, 'DO': 1, 'BM': 1, 'GN': 224, 'NE': 227, 'ER': 291, 'DE': 49, 'UM': 0, 'CM': 237, 'PR': 1, 'RO': 40, 'AZ': 994, 'DZ': 213, 'BW': 267, 'MK': 389, 'HN': 504, 'IS': 354, 'SJ': 47, 'ME': 382, 'NR': 674, 'AD': 376, 'BY': 375, 'RE': 262, 'PG': 675, 'SO': 252, 'NO': 47, 'CC': 61, 'EE': 372, 'BN': 673, 'AU': 61, 'HM': 0, 'ML': 223, 'BD': 880, 'GE': 995, 'US': 1, 'UY': 598, 'SM': 378, 'NG': 234, 'BE': 32, 'KY': 1, 'AR': 54, 'CR': 506, 'VA': 39, 'YE': 967, 'TR': 90, 'CV': 238, 'DM': 1, 'ZM': 260, 'BR': 55, 'MG': 261, 'BL': 590, 'FJ': 679, 'SH': 290, 'KN': 1, 'ZA': 27, 'CF': 236, 'ZW': 263, 'PL': 48, 'SV': 503, 'QA': 974, 'MN': 976, 'SE': 46, 'JE': 44, 'PS': 970, 'MZ': 258, 'TK': 690, 'PM': 508, 'CW': 599, 'HK': 852, 'LB': 961, 'SY': 963, 'LC': 1, 'IE': 353, 'RW': 250, 'NL': 31, 'MA': 212, 'GM': 220, 'IR': 98, 'AT': 43, 'SZ': 268, 'GT': 502, 'MT': 356, 'BQ': 599, 'MX': 52, 'NC': 687, 'CK': 682, 'SI': 386, 'VE': 58, 'IM': 44, 'AM': 374, 'SD': 249, 'LY': 218, 'LI': 423, 'TN': 216, 'UG': 256, 'RU': 7, 'DJ': 253, 'IL': 972, 'TM': 993, 'BF': 226, 'GF': 594, 'TO': 676, 'GI': 350, 'MH': 692, 'UZ': 998, 'PF': 689, 'KZ': 7, 'GA': 241, 'PE': 51, 'TV': 688, 'BT': 975, 'MQ': 596, 'MF': 590, 'AF': 93, 'IN': 91, 'AX': 358, 'BH': 973, 'JM': 1, 'MY': 60, 'BO': 591, 'AI': 1, 'SR': 597, 'ET': 251, 'ES': 34, 'TF': 0, 'GU': 1, 'BJ': 229, 'SS': 211, 'KE': 254, 'BZ': 501, 'IO': 246, 'MU': 230, 'CL': 56, 'MD': 373, 'LU': 352, 'TJ': 992, 'EC': 593, 'VG': 1, 'NZ': 64, 'VU': 678, 'FO': 298, 'LR': 231, 'AL': 355, 'GB': 44, 'AS': 1, 'IT': 39, 'TC': 1, 'TW': 886, 'BI': 257, 'HU': 36, 'TL': 670, 'GG': 44, 'PN': 0, 'SG': 65, 'LS': 266, 'KH': 855, 'FR': 33, 'BV': 0, 'CX': 61, 'AE': 971, 'LT': 370, 'PT': 351, 'KR': 82, 'BB': 1, 'TG': 228, 'AQ': 0, 'EH': 212, 'AG': 1, 'VN': 84, 'CI': 225, 'BS': 1, 'GL': 299, 'MW': 265, 'NU': 683, 'NF': 672, 'LK': 94, 'MS': 1, 'GP': 590, 'NP': 977, 'PW': 680, 'PK': 92, 'WF': 681, 'BA': 387, 'KM': 269, 'JO': 962, 'CU': 53, 'GR': 30, 'YT': 262, 'RS': 381, 'NA': 264, 'ST': 239, 'SC': 248, 'CN': 86, 'CD': 243, 'GS': 0, 'KW': 965, 'MM': 95, 'AO': 244, 'MV': 960, 'UA': 380, 'TT': 1, 'FK': 500, 'WS': 685, 'CZ': 420, 'PH': 63, 'VI': 1, 'TZ': 255, 'MR': 222, 'MC': 377, 'SN': 221, 'HT': 509, 'VC': 1, 'NI': 505, 'GD': 1, 'GY': 592, 'TH': 66}
I have just found a python library that must be perfect for your problem.
It's called PhoneISO3166.
This is the github link: GitHub phoneiso3166
I got a really big data like below, the structure may not be fixed, but I want to find each key name is 'used', multi the value of the 'used' by 10. How can I do it? The difficult part is the list structure for some time for some reason will be changed, and the 'used': inf should be kept unchanged.
[{'block_data':
[{'item_title': u'\u4e3b\u673a',
'item_num': 5,
'item_class': 'hosts',
'item_url': '/admin/hypervisors/',
'usage': [{'used': 5, 'title': u'\u6d3b\u52a8'},
{'used': 5, 'title': u'\u542f\u7528'}]},
{'item_title': u'\u8d44\u6e90',
'usage': [{'used': 1319,
'limit': 1536,
'title': u'\u53ef\u5206\u914d\u5185\u6838'},
{'used': 5828230,
'limit': 6024840,
'unit': 'MB',
'title': u'\u53ef\u5206\u914d\u5185\u5b58'}],
'item_class': 'resources'},
{'item_title': u'\u9879\u76ee',
'item_num': 24,
'item_class': 'tenants',
'item_url': '/identity/',
'usage': [{'used': 23,
'limit': 24,
'title': u'\u6d3b\u52a8'}]},
{'item_title': u'\u7528\u6237',
'item_num': 125,
'item_class': 'users',
'item_url': '/identity/users/',
'usage': [{'used': 125,
'limit': 125,
'title': u'\u6d3b\u52a8'}]}
],
'block_title': u'\u8d44\u6e90'},
{'block_data':
[{'item_title': u'\u4e91\u4e3b\u673a',
'item_num': 124,
'item_class': 'instances',
'item_url': '/admin/instances/',
'usage': [{'used': 124,
'title': u'\u4e91\u4e3b\u673a'},
{'used': 245,
'title': u'\u865a\u62df\u5185\u6838'},
{'used': 222722,
'unit': 'MB',
'title': u'\u5185\u5b58'},
{'used': 15,
'title': u'\u5feb\u7167'}]},
{'item_title': u'\u4e91\u786c\u76d8',
'item_num': 69,
'item_class': 'volumes',
'item_url': '/admin/volumes/',
'usage': [{'used': 69,
'title': u'\u4e91\u786c\u76d8'},
{'used': 'inf',
'title': u'\u4e91\u786c\u76d8\u5feb\u7167'},
{'used': 'inf',
'unit': 'MB',
'title': u'\u786c\u76d8\u53ca\u5feb\u7167\u5bb9\u91cf'}]},
{'item_title': u'\u955c\u50cf',
'item_num': 27,
'item_class': 'images',
'item_url': '/admin/images/',
'usage': [{'used': 27,
'title': u'\u955c\u50cf'},
{'used': 11, 'title': u'Linux'},
{'used': 3, 'title': u'Windows'},
{'used': 13, 'title': u'ISO'}]},
{'item_title': u'\u5b89\u5168\u7ec4',
'item_num': 39,
'item_class': 'access_and_security',
'usage': [{'used': 39,
'title': u'\u5b89\u5168\u7ec4'}]
}],
'block_title': u'\u8ba1\u7b97'},
{'block_data':
[{'item_title': u'\u7f51\u7edc',
'item_num': 39,
'item_class': 'networks',
'item_url': '/admin/networks/',
'usage': [{'used': 39,
'title': u'\u7f51\u7edc'}]
},
{'item_title': u'\u5916\u7f51IP',
'item_num': 40,
'item_class': 'floating_ips',
'usage': [{'used': 40,
'title': u'\u5916\u7f51IP'}]
},
{'item_title': u'\u8def\u7531\u5668',
'item_num': 9,
'item_class': 'routers',
'item_url': '/admin/routers/',
'usage': [{'used': 9,
'title': u'\u8def\u7531\u5668'}]}],
'block_title': u'\u7f51\u7edc'}]
As I see it, the answer is:
for item in list:
for usage in item['block_data']:
for used in usage['usage']:
new_used = used['used'] * 10
At least, that code worked for me.
Using list comprehension:
[k['used']*10 if isinstance(k['used'], (int, long)) else k['used'] for i in a for j in i['block_data'] for k in j['usage']]
Out[29]:
[500,
500,
131900,
582823000,
2300,
12500,
12400,
24500,
22272200,
1500,
6900,
'inf',
'inf',
2700,
1100,
300,
1300,
3900,
3900,
4000,
900]