Matplotlib Auto Annotate max doesn't annotate and pushes chart off page - python

Hello I'm trying to auto annotate matplotlib chart.
I've manage to create it in a way that that doesn't give me any errors when I run it.
However, it doesn't plot the annotation and as I'm plotting in jupyter notebooks it pushes the plot right off the page.
The result I'm looking for is an automatically assigning annotation pointing to the max number in the series ppc_rolling_7d on the chart.
I'm kinda out of ideas as to what has happened here.
example data:
ppc_data = pd.DataFrame({
'Day':['2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03',
'2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07',
'2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11',
'2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15',
'2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19',
'2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23',
'2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27',
'2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01',
'2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05',
'2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09',
'2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13',
'2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17',
'2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21',
'2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25',
'2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29',
'2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02',
'2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06',
'2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10',
'2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14',
'2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18',
'2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22',
'2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26',
'2018-11-27', '2018-11-28', '2018-11-29', '2018-11-30',
'2018-12-01', '2018-12-02', '2018-12-03', '2018-12-04',
'2018-12-05', '2018-12-06', '2018-12-07', '2018-12-08'],
'Cost' : [1105.8097834013993, 1035.8355715930172, 2335.4700418958632,
655.0721024605979, 1154.3067936459986, 2275.8927050269917,
174.47816810392712,1606.0865381579742,973.1285739075876,
677.3734705782231,2381.149891233519, 1137.840620239881,
673.0575320194132, 1969.3783478235364, 1667.3405411738886,
1365.707089062391, 1686.492803446683, 1613.2530220414621,
2275.475164597224, 1593.9382082221036, 1278.8267306408893,
1342.2964464944962, 863.9840442789089, 289.34425736432837,
15.219941807702485, 1595.2327617943374, 1592.8333476628231,
961.5931139385652, 703.2690737772505, 312.9730830647801,
2105.920303495205, 707.710807657391, 873.7377744639931,
152.51387772605813, 1292.4027169055073, 1142.7323830723421,
2400.462099397225, 2027.5730000421765, 2380.127923249452,
370.97680360266463, 978.7472607817784, 144.50724935561453,
1257.3962926696906, 339.44922335906256, 989.3364341529344,
1274.7020560588671, 1697.9640365081489, 81.00819304765376,
528.9126509191693, 893.839100786781, 1778.7263797734338,
1388.1976452584615, 533.7823940180391, 1390.507110740847,
1582.8069647428326, 2058.124928605663, 1456.0037174730746,
315.93672830017414,488.9620970966599, 2020.6125475658266,
1358.8988386729175,1967.1442608919235,436.40540549351783,
2090.41730824453,2114.3435803364277,2235.719648814769,
1773.3190866160382,2372.165649889117, 1186.850504563462,
864.4092140750176, 772.6148714908818,1749.9856862684244,
802.1475898419487, 1013.3410373277948, 1604.4137362997474,
1880.084707526689, 1823.9691856540412,550.6041906641643,
75.26104973616485, 819.9409527114842, 2272.8529542934198,
1836.7071931445969,1491.3728333359875, 1807.2130424285615,
2378.1185581431337,1434.1809462567153,296.49945129452675,
2025.2054514729998,2346.234514785023, 2438.058561262957,
277.36529451533386, 1212.541281523483,2005.258496330315,
2053.7325650486177,2076.001012737591, 2245.606468047353,
2493.336539619115,1116.075112703116,319.54750552662733,
648.633853658328]}
).set_index('Day')
ppc_data.index = pd.to_datetime(ppc_data.index)
ppc_weekly = ppc_data['Cost'].resample('W').mean()
ppc_rolling_7d = ppc_data['Cost'].rolling(window=7, center=True).mean()
ax = fig.add_subplot(111)
figsize = (15,8)
ppc_data['Cost'].plot(figsize=figsize,
alpha=.5,
marker='.',
linestyle='-',
linewidth=0.5,
label='Daily'
)
ppc_weekly.plot(figsize=figsize,
marker='x',
markersize=8,
linestyle='-',
label='Weekly Mean Resample'
)
ppc_rolling_7d.plot(figsize=figsize,
marker='o',
linestyle='-',
label='7-d Rolling Mean'
)
max_value = ppc_rolling_7d.max()
max_value_index = [i for i, j in enumerate(ppc_rolling_7) if j == max_value]
#Create ax customatisations
ax.annotate('Lots of Pageviews but few clicks',
xy=(max_value_index[0],max_value),
xytext=(max_value_index[0],max_value),
arrowprops=dict(facecolor='cyan', #colour
shrink=0.05, #length of arrow
lw=1, #line width
ec='magenta', #boarder colour
zorder=1)) #layering order of annotation
#Global Plot settings
plt.title('COMPARE: Daily, Weekly Mean, 7-d Rolling Mean ') # set chart name
fig.legend() # set the legend
#display the charts
plt.show()
Any suggestions to what could be the problem are welcome.

Thanks to ImportanceOfBeingErnest who commented with the answer.
Using idxmax() will quickly find the index of the max value.
x = ppc_rolling_7d.idxmax(); y = ppc_rolling_7d.max()

Related

GeoJson object (layer) not being added to Folium map

Have looked at previous similar questions and implemented suggestions but I am still having trouble getting a layer to appear on a Folium map. Basically I have used Here's isoline API to create an array of GeoJson data objects (these are bascially areas reacjable within certain drivetimes). I can render these drivetimes on a Here map but would like to use Folium as I think it's easier to set layer colors etc.
So the GeoJson objects (drivetimes) are stored in an array called "values".
My code is as follows:
# Create the map
m = folium.Map(location=[latitude, longitude], zoom_start=13)
# Add a marker to the map
folium.Marker(
[latitude, longitude]
).add_to(m)
# Add the layer
folium.GeoJson(data=value[0], name="geojson").add_to(m)
folium.LayerControl().add_to(m)
m
An example of one of the drivetimes is as follows, any help would be appreciated:
{"data": {"features": [{"geometry": {"coordinates": [[[-6.643639, 53.382568], [-6.635742, 53.382568], [-6.633682, 53.381882], [-6.632309, 53.380508], [-6.630249, 53.379822], [-6.622009, 53.379822], [-6.619949, 53.379135], [-6.618576, 53.377762], [-6.616516, 53.377075], [-6.611023, 53.377075], [-6.608963, 53.376389], [-6.60759, 53.375015], [-6.60553, 53.374329], [-6.600037, 53.374329], [-6.597977, 53.375015], [-6.596603, 53.376389], [-6.594543, 53.377075], [-6.586304, 53.377075], [-6.584244, 53.376389], [-6.58287, 53.372269], [-6.580811, 53.371582], [-6.564331, 53.371582], [-6.562271, 53.372269], [-6.560898, 53.373642], [-6.558838, 53.374329], [-6.556091, 53.374329], [-6.554031, 53.373642], [-6.552658, 53.372269], [-6.550598, 53.371582], [-6.548538, 53.372269], [-6.547852, 53.374329], [-6.547852, 53.377075], [-6.547165, 53.379135], [-6.544418, 53.381882], [-6.542358, 53.382568], [-6.540298, 53.381882], [-6.538925, 53.380508], [-6.537552, 53.380508], [-6.536179, 53.381882], [-6.534119, 53.382568], [-6.525879, 53.382568], [-6.523819, 53.381882], [-6.522446, 53.377762], [-6.520386, 53.377075], [-6.514893, 53.377075], [-6.512833, 53.376389], [-6.511459, 53.375015], [-6.509399, 53.374329], [-6.50116, 53.374329], [-6.4991, 53.373642], [-6.497726, 53.372269], [-6.495667, 53.371582], [-6.493607, 53.372269], [-6.493607, 53.373642], [-6.497726, 53.377762], [-6.498413, 53.379822], [-6.497726, 53.381882], [-6.49498, 53.384628], [-6.49292, 53.385315], [-6.490173, 53.385315], [-6.488113, 53.384628], [-6.48674, 53.383255], [-6.48468, 53.382568], [-6.479187, 53.382568], [-6.477127, 53.381882], [-6.475754, 53.380508], [-6.471634, 53.379135], [-6.470261, 53.377762], [-6.468201, 53.377075], [-6.465454, 53.377075], [-6.463394, 53.377762], [-6.463394, 53.379135], [-6.466141, 53.381882], [-6.470261, 53.383255], [-6.473007, 53.386002], [-6.473694, 53.388062], [-6.473694, 53.396301], [-6.47438, 53.398361], [-6.4785, 53.399734], [-6.479187, 53.401794], [-6.479187, 53.404541], [-6.479874, 53.406601], [-6.481934, 53.407288], [-6.483994, 53.406601], [-6.485367, 53.402481], [-6.487427, 53.401794], [-6.495667, 53.401794], [-6.497726, 53.402481], [-6.4991, 53.406601], [-6.500473, 53.406601], [-6.501846, 53.405228], [-6.503906, 53.404541], [-6.514893, 53.404541], [-6.516953, 53.405228], [-6.517639, 53.407288], [-6.517639, 53.410034], [-6.516953, 53.412094], [-6.514893, 53.412781], [-6.512146, 53.412781], [-6.510086, 53.413467], [-6.508713, 53.414841], [-6.504593, 53.416214], [-6.50322, 53.417587], [-6.4991, 53.418961], [-6.498413, 53.421021], [-6.4991, 53.42308], [-6.50116, 53.423767], [-6.517639, 53.423767], [-6.519699, 53.424454], [-6.521072, 53.428574], [-6.525192, 53.429947], [-6.526566, 53.43132], [-6.530685, 53.432693], [-6.533432, 53.43544], [-6.534119, 53.4375], [-6.533432, 53.43956], [-6.530685, 53.442307], [-6.528625, 53.442993], [-6.525879, 53.442993], [-6.523819, 53.442307], [-6.522446, 53.440933], [-6.518326, 53.43956], [-6.516953, 53.438187], [-6.514893, 53.4375], [-6.512146, 53.4375], [-6.510086, 53.436813], [-6.508713, 53.43544], [-6.506653, 53.434753], [-6.50116, 53.434753], [-6.4991, 53.434067], [-6.497726, 53.432693], [-6.493607, 53.43132], [-6.492233, 53.429947], [-6.49086, 53.429947], [-6.489487, 53.43132], [-6.485367, 53.432693], [-6.48468, 53.434753], [-6.485367, 53.436813], [-6.489487, 53.438187], [-6.490173, 53.440247], [-6.489487, 53.442307], [-6.488113, 53.44368], [-6.488113, 53.445053], [-6.49086, 53.4478], [-6.49292, 53.448486], [-6.498413, 53.448486], [-6.500473, 53.4478], [-6.501846, 53.446426], [-6.503906, 53.44574], [-6.509399, 53.44574], [-6.511459, 53.446426], [-6.512146, 53.448486], [-6.512146, 53.451233], [-6.512833, 53.453293], [-6.514893, 53.453979], [-6.517639, 53.453979], [-6.519699, 53.454666], [-6.521072, 53.456039], [-6.525192, 53.457413], [-6.527939, 53.460159], [-6.528625, 53.462219], [-6.527939, 53.464279], [-6.525192, 53.467026], [-6.523132, 53.467712], [-6.521072, 53.467026], [-6.519699, 53.465652], [-6.517639, 53.464966], [-6.515579, 53.465652], [-6.515579, 53.467026], [-6.536179, 53.487625], [-6.536865, 53.489685], [-6.536865, 53.492432], [-6.536179, 53.494492], [-6.534119, 53.495178], [-6.528625, 53.495178], [-6.526566, 53.494492], [-6.511459, 53.479385], [-6.509399, 53.478699], [-6.506653, 53.478699], [-6.504593, 53.479385], [-6.50322, 53.480759], [-6.50116, 53.481445], [-6.4991, 53.480759], [-6.497726, 53.479385], [-6.493607, 53.478012], [-6.481247, 53.465652], [-6.479874, 53.465652], [-6.4785, 53.469772], [-6.477127, 53.471146], [-6.477127, 53.472519], [-6.4785, 53.473892], [-6.479187, 53.475952], [-6.4785, 53.478012], [-6.475754, 53.480759], [-6.473694, 53.481445], [-6.470947, 53.481445], [-6.468887, 53.480759], [-6.466141, 53.478012], [-6.465454, 53.475952], [-6.465454, 53.470459], [-6.464767, 53.468399], [-6.462021, 53.465652], [-6.457901, 53.464279], [-6.457214, 53.462219], [-6.457214, 53.459473], [-6.457901, 53.457413], [-6.459961, 53.456726], [-6.465454, 53.456726], [-6.467514, 53.456039], [-6.470261, 53.453293], [-6.470261, 53.45192], [-6.467514, 53.449173], [-6.463394, 53.4478], [-6.462021, 53.446426], [-6.459961, 53.44574], [-6.448975, 53.44574], [-6.446915, 53.446426], [-6.445541, 53.450546], [-6.443481, 53.451233], [-6.437988, 53.451233], [-6.435928, 53.450546], [-6.434555, 53.449173], [-6.430435, 53.4478], [-6.429749, 53.44574], [-6.430435, 53.44368], [-6.431808, 53.442307], [-6.431808, 53.440933], [-6.430435, 53.43956], [-6.429749, 53.4375], [-6.430435, 53.43544], [-6.434555, 53.434067], [-6.435928, 53.432693], [-6.437988, 53.432007], [-6.440735, 53.432007], [-6.442795, 53.432693], [-6.444168, 53.434067], [-6.445541, 53.434067], [-6.446915, 53.429947], [-6.451035, 53.428574], [-6.451721, 53.426514], [-6.451035, 53.424454], [-6.446915, 53.42308], [-6.445541, 53.421707], [-6.444168, 53.421707], [-6.437302, 53.428574], [-6.433182, 53.429947], [-6.431808, 53.43132], [-6.429749, 53.432007], [-6.424255, 53.432007], [-6.422195, 53.43132], [-6.418076, 53.4272], [-6.416016, 53.426514], [-6.405029, 53.426514], [-6.402969, 53.425827], [-6.401596, 53.424454], [-6.397476, 53.42308], [-6.396103, 53.421707], [-6.394043, 53.421021], [-6.391983, 53.421707], [-6.387863, 53.425827], [-6.383743, 53.4272], [-6.383057, 53.42926], [-6.383743, 53.43132], [-6.385803, 53.432007], [-6.391296, 53.432007], [-6.393356, 53.432693], [-6.39473, 53.434067], [-6.39679, 53.434753], [-6.399536, 53.434753], [-6.401596, 53.43544], [-6.402283, 53.4375], [-6.402283, 53.440247], [-6.401596, 53.442307], [-6.399536, 53.442993], [-6.383057, 53.442993], [-6.380997, 53.44368], [-6.376877, 53.4478], [-6.374817, 53.448486], [-6.369324, 53.448486], [-6.367264, 53.4478], [-6.365891, 53.446426], [-6.363831, 53.44574], [-6.361771, 53.446426], [-6.361084, 53.448486], [-6.361084, 53.451233], [-6.361771, 53.453293], [-6.363831, 53.453979], [-6.369324, 53.453979], [-6.371384, 53.454666], [-6.37207, 53.456726], [-6.37207, 53.459473], [-6.371384, 53.461533], [-6.37001, 53.462906], [-6.37001, 53.464279], [-6.371384, 53.465652], [-6.372757, 53.469772], [-6.383743, 53.480759], [-6.385803, 53.481445], [-6.38855, 53.481445], [-6.39061, 53.482132], [-6.391983, 53.483505], [-6.396103, 53.484879], [-6.397476, 53.486252], [-6.399536, 53.486938], [-6.410522, 53.486938], [-6.412582, 53.487625], [-6.413956, 53.488998], [-6.418076, 53.490372], [-6.423569, 53.495865], [-6.424255, 53.497925], [-6.424255, 53.500671], [-6.424942, 53.502731], [-6.426315, 53.504105], [-6.427002, 53.506165], [-6.427002, 53.514404], [-6.426315, 53.516464], [-6.424255, 53.517151], [-6.418762, 53.517151], [-6.416702, 53.516464], [-6.413956, 53.513718], [-6.413269, 53.511658], [-6.413269, 53.506165], [-6.412582, 53.504105], [-6.411209, 53.502731], [-6.409836, 53.498611], [-6.408463, 53.498611], [-6.407089, 53.499985], [-6.405029, 53.500671], [-6.399536, 53.500671], [-6.397476, 53.501358], [-6.39679, 53.503418], [-6.39679, 53.506165], [-6.396103, 53.508224], [-6.394043, 53.508911], [-6.38855, 53.508911], [-6.38649, 53.508224], [-6.379623, 53.501358], [-6.377563, 53.500671], [-6.37207, 53.500671], [-6.37001, 53.499985], [-6.368637, 53.498611], [-6.364517, 53.497238], [-6.363831, 53.495178], [-6.363831, 53.492432], [-6.364517, 53.490372], [-6.368637, 53.488998], [-6.368637, 53.487625], [-6.367264, 53.486252], [-6.365891, 53.482132], [-6.363144, 53.479385], [-6.361084, 53.478699], [-6.352844, 53.478699], [-6.350784, 53.478012], [-6.348038, 53.475266], [-6.348038, 53.473892], [-6.349411, 53.472519], [-6.350098, 53.470459], [-6.349411, 53.468399], [-6.348038, 53.468399], [-6.346664, 53.469772], [-6.344604, 53.470459], [-6.339111, 53.470459], [-6.337051, 53.469772], [-6.331558, 53.464279], [-6.330872, 53.462219], [-6.331558, 53.460159], [-6.335678, 53.458786], [-6.335678, 53.457413], [-6.332932, 53.454666], [-6.330872, 53.453979], [-6.319885, 53.453979], [-6.317825, 53.454666], [-6.317139, 53.456726], [-6.317139, 53.459473], [-6.316452, 53.461533], [-6.314392, 53.462219], [-6.303406, 53.462219], [-6.301346, 53.462906], [-6.299973, 53.464279], [-6.297913, 53.464966], [-6.295166, 53.464966], [-6.293106, 53.464279], [-6.290359, 53.461533], [-6.288986, 53.457413], [-6.286926, 53.456726], [-6.27594, 53.456726], [-6.27388, 53.456039], [-6.271133, 53.453293], [-6.26976, 53.449173], [-6.268387, 53.449173], [-6.267014, 53.450546], [-6.264954, 53.451233], [-6.256714, 53.451233], [-6.254654, 53.450546], [-6.253281, 53.448486], [-6.251907, 53.447113], [-6.250534, 53.4478], [-6.249161, 53.449173], [-6.247787, 53.453293], [-6.245728, 53.453979], [-6.234741, 53.453979], [-6.232681, 53.454666], [-6.228561, 53.458786], [-6.226501, 53.459473], [-6.221008, 53.459473], [-6.218948, 53.460159], [-6.218948, 53.461533], [-6.220322, 53.462906], [-6.221008, 53.464966], [-6.221008, 53.475952], [-6.220322, 53.478012], [-6.218262, 53.478699], [-6.215515, 53.478699], [-6.213455, 53.479385], [-6.212769, 53.481445], [-6.212769, 53.489685], [-6.212082, 53.491745], [-6.207962, 53.495865], [-6.207275, 53.497925], [-6.207275, 53.508911], [-6.206589, 53.510971], [-6.204529, 53.511658], [-6.199036, 53.511658], [-6.196976, 53.510971], [-6.194229, 53.508224], [-6.193542, 53.506165], [-6.193542, 53.495178], [-6.194229, 53.493118], [-6.195602, 53.491745], [-6.195602, 53.490372], [-6.194229, 53.488998], [-6.193542, 53.486938], [-6.193542, 53.484192], [-6.192856, 53.482132], [-6.191483, 53.480759], [-6.190796, 53.478699], [-6.191483, 53.476639], [-6.195602, 53.472519], [-6.196289, 53.470459], [-6.196289, 53.451233], [-6.195602, 53.449173], [-6.191483, 53.4478], [-6.188736, 53.445053], [-6.188049, 53.442993], [-6.188736, 53.440933], [-6.191483, 53.438187], [-6.193542, 53.4375], [-6.199036, 53.4375], [-6.201096, 53.436813], [-6.206589, 53.43132], [-6.206589, 53.429947], [-6.204529, 53.42926], [-6.193542, 53.42926], [-6.191483, 53.428574], [-6.190109, 53.4272], [-6.188049, 53.426514], [-6.185303, 53.426514], [-6.183243, 53.425827], [-6.182556, 53.423767], [-6.183243, 53.421707], [-6.183929, 53.420334], [-6.182556, 53.418961], [-6.17981, 53.418274], [-6.174316, 53.418274], [-6.172256, 53.417587], [-6.170883, 53.413467], [-6.166763, 53.412094], [-6.164017, 53.409348], [-6.162643, 53.405228], [-6.158524, 53.403854], [-6.155777, 53.401108], [-6.15509, 53.399048], [-6.155777, 53.396988], [-6.158524, 53.394241], [-6.162643, 53.392868], [-6.16333, 53.390808], [-6.16333, 53.388062], [-6.162643, 53.386002], [-6.160583, 53.385315], [-6.157837, 53.385315], [-6.155777, 53.386002], [-6.154404, 53.387375], [-6.152344, 53.388062], [-6.144104, 53.388062], [-6.142044, 53.387375], [-6.141357, 53.385315], [-6.141357, 53.382568], [-6.142044, 53.380508], [-6.144791, 53.377762], [-6.146851, 53.377075], [-6.149597, 53.377075], [-6.151657, 53.376389], [-6.152344, 53.374329], [-6.152344, 53.371582], [-6.15303, 53.369522], [-6.15509, 53.368835], [-6.157837, 53.368835], [-6.159897, 53.368149], [-6.16127, 53.366776], [-6.16539, 53.365402], [-6.168137, 53.362656], [-6.168137, 53.361282], [-6.166763, 53.359909], [-6.166077, 53.357849], [-6.166077, 53.355103], [-6.166763, 53.353043], [-6.168823, 53.352356], [-6.177063, 53.352356], [-6.179123, 53.353043], [-6.180496, 53.354416], [-6.182556, 53.355103], [-6.185303, 53.355103], [-6.187363, 53.354416], [-6.188049, 53.352356], [-6.188049, 53.346863], [-6.188736, 53.344803], [-6.190109, 53.34343], [-6.190796, 53.34137], [-6.190796, 53.338623], [-6.191483, 53.336563], [-6.193542, 53.335876], [-6.201782, 53.335876], [-6.203842, 53.33519], [-6.203842, 53.333817], [-6.202469, 53.332443], [-6.201096, 53.328323], [-6.199722, 53.32695], [-6.199036, 53.32489], [-6.199036, 53.319397], [-6.198349, 53.317337], [-6.195602, 53.31459], [-6.191483, 53.313217], [-6.190109, 53.311844], [-6.185989, 53.310471], [-6.185303, 53.308411], [-6.185303, 53.305664], [-6.185989, 53.303604], [-6.188049, 53.302917], [-6.190796, 53.302917], [-6.192856, 53.302231], [-6.194229, 53.298111], [-6.195602, 53.296738], [-6.196976, 53.292618], [-6.199036, 53.291931], [-6.207275, 53.291931], [-6.209335, 53.292618], [-6.210709, 53.293991], [-6.212769, 53.294678], [-6.221008, 53.294678], [-6.223068, 53.293991], [-6.224442, 53.289871], [-6.232681, 53.281631], [-6.236801, 53.280258], [-6.237488, 53.278198], [-6.236801, 53.276138], [-6.234741, 53.275452], [-6.210022, 53.275452], [-6.207962, 53.274765], [-6.206589, 53.273392], [-6.202469, 53.272018], [-6.201782, 53.269958], [-6.201782, 53.267212], [-6.202469, 53.265152], [-6.204529, 53.264465], [-6.210022, 53.264465], [-6.212082, 53.265152], [-6.213455, 53.266525], [-6.215515, 53.267212], [-6.237488, 53.267212], [-6.239548, 53.266525], [-6.240921, 53.265152], [-6.242981, 53.264465], [-6.245728, 53.264465], [-6.247787, 53.263779], [-6.249161, 53.262405], [-6.251221, 53.261719], [-6.25946, 53.261719], [-6.26152, 53.262405], [-6.262894, 53.263779], [-6.264954, 53.264465], [-6.270447, 53.264465], [-6.272507, 53.263779], [-6.27388, 53.262405], [-6.27594, 53.261719], [-6.289673, 53.261719], [-6.291733, 53.262405], [-6.293106, 53.263779], [-6.295166, 53.264465], [-6.300659, 53.264465], [-6.302719, 53.265152], [-6.304092, 53.266525], [-6.306152, 53.267212], [-6.314392, 53.267212], [-6.316452, 53.266525], [-6.316452, 53.265152], [-6.312332, 53.263779], [-6.311646, 53.261719], [-6.312332, 53.259659], [-6.315079, 53.256912], [-6.317139, 53.256226], [-6.322632, 53.256226], [-6.324692, 53.256912], [-6.328812, 53.261032], [-6.332932, 53.262405], [-6.334305, 53.263779], [-6.336365, 53.264465], [-6.347351, 53.264465], [-6.349411, 53.265152], [-6.350784, 53.266525], [-6.352158, 53.266525], [-6.353531, 53.265152], [-6.355591, 53.264465], [-6.363831, 53.264465], [-6.365891, 53.265152], [-6.366577, 53.267212], [-6.366577, 53.269958], [-6.367264, 53.272018], [-6.369324, 53.272705], [-6.377563, 53.272705], [-6.379623, 53.273392], [-6.380997, 53.277512], [-6.383057, 53.278198], [-6.385117, 53.277512], [-6.38649, 53.276138], [-6.38855, 53.275452], [-6.405029, 53.275452], [-6.407089, 53.276138], [-6.408463, 53.276825], [-6.409836, 53.275452], [-6.411209, 53.273392], [-6.413269, 53.272705], [-6.421509, 53.272705], [-6.423569, 53.273392], [-6.424942, 53.274765], [-6.426315, 53.274765], [-6.427689, 53.270645], [-6.429749, 53.269958], [-6.440735, 53.269958], [-6.442795, 53.269272], [-6.444168, 53.267899], [-6.446228, 53.267212], [-6.451721, 53.267212], [-6.453781, 53.267899], [-6.457901, 53.272018], [-6.459961, 53.273392], [-6.462708, 53.274765], [-6.465454, 53.275452], [-6.468201, 53.275452], [-6.470261, 53.274765], [-6.471634, 53.273392], [-6.475754, 53.272018], [-6.477127, 53.270645], [-6.4785, 53.270645], [-6.479874, 53.272018], [-6.481934, 53.272705], [-6.498413, 53.272705], [-6.500473, 53.272018], [-6.501846, 53.270645], [-6.503906, 53.269958], [-6.506653, 53.269958], [-6.508713, 53.269272], [-6.510086, 53.267899], [-6.514206, 53.266525], [-6.515579, 53.265152], [-6.517639, 53.264465], [-6.523132, 53.264465], [-6.525192, 53.265152], [-6.525879, 53.267212], [-6.525879, 53.269958], [-6.525192, 53.272018], [-6.521072, 53.273392], [-6.519699, 53.274765], [-6.515579, 53.276138], [-6.514206, 53.277512], [-6.510086, 53.278885], [-6.508713, 53.280258], [-6.506653, 53.280945], [-6.498413, 53.280945], [-6.496353, 53.281631], [-6.49498, 53.283005], [-6.49292, 53.283691], [-6.487427, 53.283691], [-6.485367, 53.284378], [-6.483994, 53.285751], [-6.481934, 53.286438], [-6.47644, 53.286438], [-6.47438, 53.287125], [-6.473694, 53.289185], [-6.473694, 53.291931], [-6.473007, 53.293991], [-6.470947, 53.294678], [-6.465454, 53.294678], [-6.463394, 53.293991], [-6.462021, 53.292618], [-6.459961, 53.291931], [-6.454468, 53.291931], [-6.452408, 53.292618], [-6.451035, 53.293991], [-6.446915, 53.295364], [-6.445541, 53.299484], [-6.444168, 53.300858], [-6.444168, 53.302231], [-6.446228, 53.302917], [-6.448975, 53.302917], [-6.451035, 53.303604], [-6.452408, 53.307724], [-6.454468, 53.308411], [-6.459961, 53.308411], [-6.462021, 53.307724], [-6.463394, 53.306351], [-6.465454, 53.305664], [-6.473694, 53.305664], [-6.475754, 53.306351], [-6.477127, 53.310471], [-6.479187, 53.311157], [-6.481247, 53.310471], [-6.48262, 53.309097], [-6.48468, 53.308411], [-6.490173, 53.308411], [-6.492233, 53.309097], [-6.49498, 53.311844], [-6.495667, 53.313904], [-6.49498, 53.315964], [-6.49086, 53.317337], [-6.48674, 53.321457], [-6.48468, 53.322144], [-6.473694, 53.322144], [-6.471634, 53.32283], [-6.467514, 53.32695], [-6.463394, 53.328323], [-6.463394, 53.329697], [-6.467514, 53.33107], [-6.468887, 53.332443], [-6.470947, 53.33313], [-6.47644, 53.33313], [-6.4785, 53.333817], [-6.479874, 53.33519], [-6.483994, 53.336563], [-6.485367, 53.337936], [-6.48674, 53.337936], [-6.49086, 53.333817], [-6.49498, 53.332443], [-6.495667, 53.330383], [-6.495667, 53.327637], [-6.496353, 53.325577], [-6.4991, 53.32283], [-6.50116, 53.322144], [-6.514893, 53.322144], [-6.516953, 53.321457], [-6.518326, 53.320084], [-6.520386, 53.319397], [-6.523132, 53.319397], [-6.525192, 53.320084], [-6.527939, 53.32283], [-6.528625, 53.32489], [-6.527939, 53.32695], [-6.523819, 53.33107], [-6.523132, 53.33313], [-6.523819, 53.33519], [-6.525879, 53.335876], [-6.527939, 53.33519], [-6.529312, 53.333817], [-6.531372, 53.33313], [-6.542358, 53.33313], [-6.544418, 53.333817], [-6.545105, 53.335876], [-6.545105, 53.338623], [-6.545792, 53.340683], [-6.548538, 53.34343], [-6.550598, 53.344116], [-6.553345, 53.344116], [-6.555405, 53.344803], [-6.556778, 53.348923], [-6.560898, 53.353043], [-6.562271, 53.357162], [-6.564331, 53.357849], [-6.569824, 53.357849], [-6.571884, 53.358536], [-6.573257, 53.359909], [-6.575317, 53.360596], [-6.583557, 53.360596], [-6.585617, 53.359909], [-6.58699, 53.355789], [-6.59111, 53.351669], [-6.592484, 53.347549], [-6.59523, 53.344803], [-6.59729, 53.344116], [-6.600037, 53.344116], [-6.602097, 53.344803], [-6.604843, 53.347549], [-6.60553, 53.349609], [-6.60553, 53.352356], [-6.604843, 53.354416], [-6.600723, 53.358536], [-6.600037, 53.360596], [-6.600723, 53.362656], [-6.60347, 53.365402], [-6.60553, 53.366089], [-6.61377, 53.366089], [-6.615829, 53.366776], [-6.617203, 53.368149], [-6.619263, 53.368835], [-6.624756, 53.368835], [-6.626816, 53.369522], [-6.628189, 53.370895], [-6.630249, 53.371582], [-6.635742, 53.371582], [-6.637802, 53.372269], [-6.639175, 53.373642], [-6.641235, 53.374329], [-6.643982, 53.374329], [-6.646042, 53.375015], [-6.646729, 53.377075], [-6.646729, 53.379822], [-6.646385, 53.381882], [-6.645355, 53.382568], [-6.643639, 53.382568]]], "type": "Polygon"}, "properties": {"range": {"type": "time", "value": 1500}}, "type": "Feature"}], "type": "FeatureCollection"}, "type": "GeoJSON"}
To specify polygons without changing the data in your question, you need to specify geometry values in dictionary form.
import folium
latitude = 53.372
longitude = -6.49
# Create the map
m = folium.Map(location=[latitude, longitude], zoom_start=10)
# Add a marker to the map
folium.Marker(
[latitude, longitude]
).add_to(m)
# Add the layer
folium.GeoJson(data=value['data']['features'][0]['geometry'], name="geojson").add_to(m)
folium.LayerControl().add_to(m)
m

How do I remove duplicate numbers from my hand of cards?

So i have my hand of cards that i randomly dealt from a deck but i need to remove the duplicate numbers in my hand. I have no idea where to start.
this is an example of what my hand would look like before removing the pairs:
l=['6♠', '6♣', '7♢', '10♣', '8♣', '2♣', '9♠', '8♢', '7♠', 'K♢', '9♡', 'Q♡', '10♢', '2♠', 'K♡', '2♢', '3♢', 'Q♢', '6♡', '4♣', 'A♡', '4♡', 'A♠', 'A♣', 'J♣', 'Q♠']
and this is what i've tried:
no_pairs=[]
l.sort()
for i in range(len(l)):
for j in (i+1, len(l)):
if i[-2] not in j:
no_pairs.append(i)
when i do this, it keeps saying
TypeError: 'int' object is not subscriptable
Strip the final character when iterating through the list of strings.
If you're just looking for the number, ignoring the suit, then you just need to strip the suit from the number before comparing it to an array of the numbers you've already sorted, and then adding both the stripped value to the array of numbers, and the original value to the final output. This should work:
l.sort()
sorted_num = []
final = []
for i in l:
if i[:-1] not in sorted_num:
sorted_num.append(i[:-1])
final.append(i)
When run, this gives the value of final as:
['10♢', '2♠', '3♢', '4♡', '6♠', '7♠', '8♢', '9♠', 'A♠', 'J♣', 'K♡', 'Q♠']
Check for the occurrence of each element in the list, and append only if occurs once.
l=['6♠', '6♣', '7♢', '10♣', '8♣', '2♣', '9♠', '8♢', '7♠', 'K♢', '9♡', 'Q♡', '10♢', '2♠', 'K♡', '2♢', '3♢', 'Q♢', '6♡', '4♣', 'A♡', '4♡', 'A♠', 'A♣', 'J♣', 'Q♠']
no_pairs=[]
for i in set(l):
if l.count(i)==1:
no_pairs.append(i)
Get first character of items with item[0], then check it is a number with num.isdigit() method and add it to no_pairs list.
l=['6♠', '6♣', '7♢', '10♣', '8♣', '2♣', '9♠', '8♢', '7♠', 'K♢', '9♡', 'Q♡', '10♢', '2♠', 'K♡', '2♢', '3♢', 'Q♢', '6♡', '4♣', 'A♡', '4♡', 'A♠', 'A♣', 'J♣', 'Q♠']
no_pairs = []
for item in l:
num = item[0]
if num.isdigit():
num = int(num)
no_pairs.append(int(num))
print(no_pairs)
#output :
[6, 6, 7, 1, 8, 2, 9, 8, 7, 9, 1, 2, 2, 3, 6, 4, 4]
Your i is an int, you can achieve i[2] (for the error). Then I really don't know where your code was going, so I didn't try to fix I wrote a new one.
I'd suggest you keep track of the numbers you already added, so that you know if you add the card or not
l = ['6♠', '6♣', '7♢', '10♣', '8♣', '2♣', '9♠', '8♢', '7♠', 'K♢', '9♡', 'Q♡', '10♢',
'2♠', 'K♡', '2♢', '3♢', 'Q♢', '6♡', '4♣', 'A♡', '4♡', 'A♠', 'A♣', 'J♣', 'Q♠']
no_pairs = []
nb_cards = set()
for card in l:
nb_card = card[:-1]
if nb_card not in nb_cards:
nb_cards.add(nb_card)
no_pairs.append(card)
print(no_pairs) # ['6♠', '7♢', '10♣', '8♣', '2♣', '9♠', 'K♢', 'Q♡', '3♢', '4♣', 'A♡', 'J♣']
You can replace the set by a list, but that woul dbe just less performant

Select specific dates from a data frame in python using MODIS data in NETCDF4

I am not well-versed in python, and I'm sure there is a simple solution to this (although, I have looked). I got this code from an lpdaac tutorial.
My input is a NETCDF4 file downloaded from MODIS satellite. Printing the metadata of the file returns the variables
file_in = Dataset(file_list[0], 'r', format = 'NETCDF4')
#print metadata
list(file_in.variables)
Out[19]: ['crs', 'time', 'lat', 'lon', '_1_km_16_days_EVI', '_1_km_16_days_VI_Quality']
I want to convert the time variable to date format, and then only select 1 date from each year. Here is the code to convert to date format:
from netCDF4 import num2date
times = file_in.variables["time"] #import time variables
dates = num2date(times[:], times.units) #get the time info
dates = [date.strftime("%Y-%m-%d") for date in dates] #get the list of datetime
print(dates)
The dates are as follows:
['2000-06-25', '2000-07-11', '2000-07-27', '2000-08-12', '2000-08-28', '2000-09-13', '2000-09-29', '2000-10-15', '2000-10-31', '2000-11-16', '2000-12-02', '2000-12-18', '2001-01-01', '2001-01-17', '2001-02-02', '2001-02-18', '2001-03-06', '2001-03-22', '2001-04-07', '2001-04-23', '2001-05-09', '2001-05-25', '2001-06-10', '2001-06-26', '2001-07-12', '2001-07-28', '2001-08-13', '2001-08-29', '2001-09-14', '2001-09-30', '2001-10-16', '2001-11-01', '2001-11-17', '2001-12-03', '2001-12-19', '2002-01-01', '2002-01-17', '2002-02-02', '2002-02-18', '2002-03-06', '2002-03-22', '2002-04-07', '2002-04-23', '2002-05-09', '2002-05-25', '2002-06-10', '2002-06-26', '2002-07-12', '2002-07-28', '2002-08-13', '2002-08-29', '2002-09-14', '2002-09-30', '2002-10-16', '2002-11-01', '2002-11-17', '2002-12-03', '2002-12-19', '2003-01-01', '2003-01-17', '2003-02-02', '2003-02-18', '2003-03-06', '2003-03-22', '2003-04-07', '2003-04-23', '2003-05-09', '2003-05-25', '2003-06-10', '2003-06-26', '2003-07-12', '2003-07-28', '2003-08-13', '2003-08-29', '2003-09-14', '2003-09-30', '2003-10-16', '2003-11-01', '2003-11-17', '2003-12-03', '2003-12-19', '2004-01-01', '2004-01-17', '2004-02-02', '2004-02-18', '2004-03-05', '2004-03-21', '2004-04-06', '2004-04-22', '2004-05-08', '2004-05-24', '2004-06-09', '2004-06-25', '2004-07-11', '2004-07-27', '2004-08-12', '2004-08-28', '2004-09-13', '2004-09-29', '2004-10-15', '2004-10-31', '2004-11-16', '2004-12-02', '2004-12-18', '2005-01-01', '2005-01-17', '2005-02-02', '2005-02-18', '2005-03-06', '2005-03-22', '2005-04-07', '2005-04-23', '2005-05-09', '2005-05-25', '2005-06-10', '2005-06-26', '2005-07-12', '2005-07-28', '2005-08-13', '2005-08-29', '2005-09-14', '2005-09-30', '2005-10-16', '2005-11-01', '2005-11-17', '2005-12-03', '2005-12-19', '2006-01-01', '2006-01-17', '2006-02-02', '2006-02-18', '2006-03-06', '2006-03-22', '2006-04-07', '2006-04-23', '2006-05-09', '2006-05-25', '2006-06-10', '2006-06-26', '2006-07-12', '2006-07-28', '2006-08-13', '2006-08-29', '2006-09-14', '2006-09-30', '2006-10-16', '2006-11-01', '2006-11-17', '2006-12-03', '2006-12-19', '2007-01-01', '2007-01-17', '2007-02-02', '2007-02-18', '2007-03-06', '2007-03-22', '2007-04-07', '2007-04-23', '2007-05-09', '2007-05-25', '2007-06-10', '2007-06-26', '2007-07-12', '2007-07-28', '2007-08-13', '2007-08-29', '2007-09-14', '2007-09-30', '2007-10-16', '2007-11-01', '2007-11-17', '2007-12-03', '2007-12-19', '2008-01-01', '2008-01-17', '2008-02-02', '2008-02-18', '2008-03-05', '2008-03-21', '2008-04-06', '2008-04-22', '2008-05-08', '2008-05-24', '2008-06-09', '2008-06-25', '2008-07-11', '2008-07-27', '2008-08-12', '2008-08-28', '2008-09-13', '2008-09-29', '2008-10-15', '2008-10-31', '2008-11-16', '2008-12-02', '2008-12-18', '2009-01-01', '2009-01-17', '2009-02-02', '2009-02-18', '2009-03-06', '2009-03-22', '2009-04-07', '2009-04-23', '2009-05-09', '2009-05-25', '2009-06-10', '2009-06-26', '2009-07-12', '2009-07-28', '2009-08-13', '2009-08-29', '2009-09-14', '2009-09-30', '2009-10-16', '2009-11-01', '2009-11-17', '2009-12-03', '2009-12-19', '2010-01-01', '2010-01-17', '2010-02-02', '2010-02-18', '2010-03-06', '2010-03-22', '2010-04-07', '2010-04-23', '2010-05-09', '2010-05-25', '2010-06-10', '2010-06-26', '2010-07-12', '2010-07-28', '2010-08-13', '2010-08-29', '2010-09-14', '2010-09-30', '2010-10-16', '2010-11-01', '2010-11-17', '2010-12-03', '2010-12-19', '2011-01-01', '2011-01-17', '2011-02-02', '2011-02-18', '2011-03-06', '2011-03-22', '2011-04-07', '2011-04-23', '2011-05-09', '2011-05-25', '2011-06-10', '2011-06-26', '2011-07-12', '2011-07-28', '2011-08-13', '2011-08-29', '2011-09-14', '2011-09-30', '2011-10-16', '2011-11-01', '2011-11-17', '2011-12-03', '2011-12-19', '2012-01-01', '2012-01-17', '2012-02-02', '2012-02-18', '2012-03-05', '2012-03-21', '2012-04-06', '2012-04-22', '2012-05-08', '2012-05-24', '2012-06-09', '2012-06-25', '2012-07-11', '2012-07-27', '2012-08-12', '2012-08-28', '2012-09-13', '2012-09-29', '2012-10-15', '2012-10-31', '2012-11-16', '2012-12-02', '2012-12-18', '2013-01-01', '2013-01-17', '2013-02-02', '2013-02-18', '2013-03-06', '2013-03-22', '2013-04-07', '2013-04-23', '2013-05-09', '2013-05-25', '2013-06-10', '2013-06-26', '2013-07-12', '2013-07-28', '2013-08-13', '2013-08-29', '2013-09-14', '2013-09-30', '2013-10-16', '2013-11-01', '2013-11-17', '2013-12-03', '2013-12-19', '2014-01-01', '2014-01-17', '2014-02-02', '2014-02-18', '2014-03-06', '2014-03-22', '2014-04-07', '2014-04-23', '2014-05-09', '2014-05-25', '2014-06-10', '2014-06-26', '2014-07-12', '2014-07-28', '2014-08-13', '2014-08-29', '2014-09-14', '2014-09-30', '2014-10-16', '2014-11-01', '2014-11-17', '2014-12-03', '2014-12-19', '2015-01-01', '2015-01-17', '2015-02-02', '2015-02-18', '2015-03-06', '2015-03-22', '2015-04-07', '2015-04-23', '2015-05-09', '2015-05-25', '2015-06-10', '2015-06-26', '2015-07-12', '2015-07-28', '2015-08-13', '2015-08-29', '2015-09-14', '2015-09-30', '2015-10-16', '2015-11-01', '2015-11-17', '2015-12-03', '2015-12-19', '2016-01-01', '2016-01-17', '2016-02-02', '2016-02-18', '2016-03-05', '2016-03-21', '2016-04-06', '2016-04-22', '2016-05-08', '2016-05-24', '2016-06-09', '2016-06-25', '2016-07-11', '2016-07-27', '2016-08-12', '2016-08-28', '2016-09-13', '2016-09-29', '2016-10-15', '2016-10-31', '2016-11-16', '2016-12-02', '2016-12-18', '2017-01-01', '2017-01-17', '2017-02-02', '2017-02-18', '2017-03-06', '2017-03-22', '2017-04-07', '2017-04-23', '2017-05-09', '2017-05-25', '2017-06-10', '2017-06-26', '2017-07-12', '2017-07-28', '2017-08-13', '2017-08-29', '2017-09-14', '2017-09-30', '2017-10-16', '2017-11-01', '2017-11-17', '2017-12-03', '2017-12-19', '2018-01-01', '2018-01-17', '2018-02-02', '2018-02-18', '2018-03-06', '2018-03-22', '2018-04-07', '2018-04-23', '2018-05-09', '2018-05-25', '2018-06-10', '2018-06-26', '2018-07-12', '2018-07-28', '2018-08-13', '2018-08-29', '2018-09-14', '2018-09-30', '2018-10-16', '2018-11-01', '2018-11-17', '2018-12-03', '2018-12-19', '2019-01-01', '2019-01-17', '2019-02-02', '2019-02-18', '2019-03-06', '2019-03-22', '2019-04-07', '2019-04-23', '2019-05-09', '2019-05-25', '2019-06-10', '2019-06-26', '2019-07-12', '2019-07-28', '2019-08-13', '2019-08-29', '2019-09-14', '2019-09-30', '2019-10-16', '2019-11-01', '2019-11-17', '2019-12-03', '2019-12-19', '2020-01-01', '2020-01-17', '2020-02-02', '2020-02-18', '2020-03-05', '2020-03-21', '2020-04-06', '2020-04-22', '2020-05-08', '2020-05-24', '2020-06-09', '2020-06-25', '2020-07-11', '2020-07-27']
And these are the dates I want in the data frame:
dates = ['2000-07-11', '2001-07-12', '2002-07-12', '2003-07-12', '2004-07-11',
'2005-07-12', '2006-07-12', '2007-07-12', '2008-07-11', '2009-07-12',
'2010-07-12', '2011-07-12', '2012-07-11', '2013-07-12', '2014-07-12',
'2015-07-12', '2016-07-11', '2017-07-12', '2018-07-12', '2019-07-12',
'2020-07-11']
I tried just defining a new dates data frame, but I think that caused problems for me later in the code, so I would like to just subset the first dates data frame if there is an easy way to do it.
Thank you for your help

Pandas Dataframe slice fillna value not being assigned (not working)

I tried this
values = {'BsmtQual':'None','BsmtCond':'None', 'BsmtExposure':'None', 'BsmtFinType1':'None', 'BsmtFinType2':'None'}
df_test.loc[:, ('BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2')].fillna(value=values, inplace=True)
and this
values = {'BsmtQual':'None','BsmtCond':'None', 'BsmtExposure':'None', 'BsmtFinType1':'None', 'BsmtFinType2':'None'}
df_test.loc[:, ['BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']].fillna(value=values, inplace=True)
and this
values = {'BsmtQual':'None','BsmtCond':'None', 'BsmtExposure':'None', 'BsmtFinType1':'None', 'BsmtFinType2':'None'}
df_test[['BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']].fillna(value=values, inplace=True)
just this
df_test[['BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']].fillna('None', inplace=True)
one line with .loc
df_test.loc[:, ['BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']].fillna('None', inplace=True)
and nothing worked! Please help me out
You need to assign it. For example:
df_test.loc[:, ('BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2')] = df_test.loc[:, ('BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2')].fillna(value=values)

How to find pairs of a card pack list using python

I have the following list in python.
list = ['10♠', '10♣', '2♡', '4♠', '4♣', '5♡', '5♣', '6♡', '6♣', '7♠', '7♡', '7♢', '7♣', '8♡', '8♢', '8♣', '9♡', '9♢', '9♣', 'A♠', 'A♢', 'A♣', 'J♢', 'K♠', 'K♢', 'Q♡']
how can I remove pairs from this? As an example, if a number appears an odd number of times, the last appearance of it should be kept. All others should be removed. Including all the ones that appear an even number of times.
ex: From '9♡', '9♢', '9♣', only the '9♣' should be kept.
Can someone help me with this?
I tried to use the below code to identify indices first. But still no luck.
i = 0
while i < len(deck):
count = 0
k = len(deck[i])
pivot = i
j = i
while j < len(deck):
if deck[i][:k-1] == deck[j][:k-1]:
print(deck[i]+','+deck[j])
count+= 1
pivot = j
j+=1
if (count %2 != 0):
print('pivot:'+str(pivot))
i = pivot +1
i +=1
No need to consider symbols. Just want to remove pairs from the list.
Please provide your suggestions.
Is this what you are looking for?
from collections import defaultdict
deck = ['10♠', '10♣', '2♡', '4♠', '4♣', '5♡', '5♣', '6♡', '6♣', '7♠', '7♡', '7♢', '7♣', '8♡', '8♢', '8♣', '9♡', '9♢', '9♣', 'A♠', 'A♢', 'A♣', 'J♢', 'K♠', 'K♢', 'Q♡']
# Create a dictionary and group all the cards with same number
groups = defaultdict(list)
for card in deck:
key = card[:-1]
groups[key].append(card)
new_deck = []
for subgroup in groups.values():
# iterate through the dictionary
# If you have odd number of cards in a subgroup
# consider the last card in that subgroup
if len(subgroup)%2 == 1:
new_deck.append(subgroup[-1])
for card in new_deck:
print(card)
Output
2♡ 8♣ 9♣ A♣ J♢ Q♡
Edit: A minor simplification to the second iteration with groups.values, thanks to RoadRunner.
Group the card pairs into a collections.defaultdict, then only return the last card from uneven pairs in a new list using a list comprehension:
from collections import defaultdict
lst = ['10♠', '10♣', '2♡', '4♠', '4♣', '5♡', '5♣', '6♡', '6♣', '7♠', '7♡', '7♢', '7♣', '8♡', '8♢', '8♣', '9♡', '9♢', '9♣', 'A♠', 'A♢', 'A♣', 'J♢', 'K♠', 'K♢', 'Q♡']
cards = defaultdict(list)
for card in lst:
cards[card[:-1]].append(card)
result = [pairs[-1] for pairs in cards.values() if len(pairs) % 2]
print(result)
Output:
['2♡', '8♣', '9♣', 'A♣', 'J♢', 'Q♡']
Keeping the same order, you can use:
import re
l = ['10♠', '10♣', '2♡', '4♠', '4♣', '5♡', '5♣', '6♡', '6♣', '7♠', '7♡', '7♢', '7♣', '8♡', '8♢', '8♣', '9♡', '9♢', '9♣', 'A♠', 'A♢', 'A♣', 'J♢', 'K♠', 'K♢', 'Q♡']
nc, nl = [], [0]
for x in l:
clean = re.sub(r"[^A-Z\d]", "", x)
if clean != nl[-1]:
nl.append(clean)
nc.append(x)
else:
del nl[-1]
del nc[-1]
print(nc)
# ['2♡', '8♣', '9♣', 'A♣', 'J♢', 'Q♡']
Demo
First of all, list is a reserved keyword, you should never name your variables after reserved keywords, use lst instead of list
Now, Here is the minimal solution:
lst = ['10♠', '10♣', '2♡', '4♠', '4♣', '5♡', '5♣', '6♡', '6♣', '7♠', '7♡', '7♢', '7♣', '8♡', '8♢', '8♣', '9♡', '9♢', '9♣', 'A♠', 'A♢', 'A♣', 'J♢', 'K♠', 'K♢', 'Q♡']
dictionary = dict.fromkeys(list('A23456789JQK')+['10'])
for item in lst:
dictionary[item[:-1]] = item if dictionary[item[:-1]] is None else None
print(list(filter(None.__ne__, dictionary.values())))
output:
['A♣', '2♡', '8♣', '9♣', 'J♢', 'Q♡']

Categories