{"id":1682,"date":"2022-11-22T04:42:23","date_gmt":"2022-11-22T12:42:23","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1682"},"modified":"2022-11-24T08:46:29","modified_gmt":"2022-11-24T16:46:29","slug":"210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-5","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2022\/11\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-3-2-2-3-2-2-5\/","title":{"rendered":"#318 Use lambda as sorting key in python"},"content":{"rendered":"<p>One of the most common situations where a lambda comes in handy is when specifying a key function, which is a callable that returns the part of a collection or object that should be used for sorting.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nforces = &#x5B;\r\n(385364, 188.2),\r\n(385365, 306.2),\r\n(385366, 402.4),\r\n(385367, 128.6),\r\n(385368, 174.5)\r\n]\r\nby_force_value = sorted(forces, key=lambda x: x&#x5B;1], reverse=True)\r\nprint(by_force_value)\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&#x5B;(385366, 402.4), (385365, 306.2), (385364, 188.2), (385368, 174.5), (385367, 128.6)]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One of the most common situations where a lambda comes in handy is when specifying a key function, which is a callable that returns the part of a collection or object that should be used for sorting. forces = &#x5B; (385364, 188.2), (385365, 306.2), (385366, 402.4), (385367, 128.6), (385368, 174.5) ] by_force_value = sorted(forces, key=lambda [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1682","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-r8","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":10320,"url":"https:\/\/gantovnik.com\/bio-tips\/2025\/08\/462-sorting-a-dictionary-by-key-or-value-in-python\/","url_meta":{"origin":1682,"position":0},"title":"#462 Sorting a Dictionary by Key or Value in Python","author":"gantovnik","date":"2025-08-24","format":false,"excerpt":"In Python, dictionaries are unordered collections of key-value pairs. Sometimes, we need to sort a dictionary either by its keys or by its values. This can be done easily using the built-in sorted() function. # Example dictionary d = {'a': 3, 'c': 1, 'b': 2} # Sort dictionary items by\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1718,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2\/","url_meta":{"origin":1682,"position":1},"title":"#325 Finding the intersection points between two functions using python","author":"gantovnik","date":"2022-12-13","format":false,"excerpt":"[code language=\"python\"] from scipy.optimize import fsolve import numpy as np import matplotlib.pyplot as plt # Defining function to simplify intersection solution def findIntersection(func1, func2, x0): return fsolve(lambda x : func1(x) - func2(x), x0) # Defining functions that will intersect funky = lambda x : np.cos(x \/ 5) * np.sin(x \/\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex325.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2889,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/440-cycloid-on-interactive-figure-with-widgets-in-python\/","url_meta":{"origin":1682,"position":2},"title":"#440 Cycloid on interactive figure with widgets in python","author":"gantovnik","date":"2024-07-21","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider theta = np.linspace(0,2*np.pi) circ_x = lambda t: t + np.cos(theta) circ_y = 1 + np.sin(theta) cycl_x = lambda t: t - np.sin(t) cycl_y = lambda t: 1 - np.cos(t) t = 0 fig,ax = plt.subplots() plt.subplots_adjust(bottom=0.2) plt.ylim(0, 3)\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex440.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex440.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex440.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":370,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/roots-of-equation\/","url_meta":{"origin":1682,"position":3},"title":"#52 Roots of equation using python","author":"gantovnik","date":"2019-01-29","format":false,"excerpt":"[code language=\"python\"] import os import numpy as np import matplotlib.pyplot as plt from scipy.optimize import brentq os.chdir(r'D:\\projects\\wordpress\\ex52') os.getcwd() f=lambda x: 0.2+x * np.cos(3\/x) x=np.linspace(-1,1,1000) plt.plot(x,f(x)) plt.axhline(0,color='k') xstar=brentq(f,-0.7,-0.5) print(xstar,f(xstar)) plt.plot(xstar,f(xstar), 'ro') plt.text(-1.0, -0.5, 'x=%6.4f and f(x)=%6.4f' % (xstar, f(xstar))) plt.tight_layout() plt.savefig(\"example52.png\", dpi=300) plt.show() plt.close() [\/code]","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example62.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1786,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/","url_meta":{"origin":1682,"position":4},"title":"#340 Lorenz attractors using python","author":"gantovnik","date":"2023-01-17","format":false,"excerpt":"[code language=\"python\"] import os import matplotlib.pyplot as plt from numpy import linspace from scipy.integrate import odeint os.chdir(r'D:\\projects\\wordpress\\ex340') os.getcwd() sigma=10.0 b=8\/3.0 r=28.0 f = lambda x,t: [sigma*(x[1]-x[0]), r*x[0]-x[1]-x[0]*x[2], x[0]*x[1]-b*x[2]] t=linspace(0,20,2000) y0=[5.0,5.0,5.0] solution=odeint(f,y0,t) X=solution[:,0]; Y=solution[:,1]; Z=solution[:,2] plt.subplot(projection='3d') plt.plot(X,Y,Z) plt.xlabel('x') plt.ylabel('y') plt.savefig(\"ex340_a.png\", dpi=100) plt.show() plt.rcParams['figure.figsize'] = (10.0, 5.0) plt.subplot(121) plt.plot(t,Z) plt.xlabel('t') plt.ylabel('Z') plt.subplot(122)\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":10657,"url":"https:\/\/gantovnik.com\/bio-tips\/2025\/11\/itertools-dropwhile-python-example\/","url_meta":{"origin":1682,"position":5},"title":"#465 Using itertools.dropwhile in Python: Skip Values Until a Condition Fails","author":"gantovnik","date":"2025-11-04","format":false,"excerpt":"Python\u2019s itertools module provides a robust set of tools for working with iterators. One of the lesser-known but convenient functions is itertools.dropwhile(). It allows you to skip elements in an iterable as long as a specific condition remains true \u2014 and once the condition fails, it yields the rest of\u2026","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1682","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=1682"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1682\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}