{"id":1853,"date":"2023-05-21T03:10:43","date_gmt":"2023-05-21T10:10:43","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=1853"},"modified":"2023-05-21T03:10:43","modified_gmt":"2023-05-21T10:10:43","slug":"347-operations-with-sets-in-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2023\/05\/347-operations-with-sets-in-python\/","title":{"rendered":"#347 Operations with sets in python"},"content":{"rendered":"<p>ex347.py<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n# Operations with sets in python\r\nset1 = {-1,0,4,5,6}\r\nset2 = {-5,0,4,7,8}\r\n# Finding the differences between two sets\r\nprint(set1.difference(set2))\r\nprint(set2.difference(set1))\r\n# Finding the duplicate items in two sets\r\nprint(set1.intersection(set2))\r\n# Combining sets\r\nprint(set1.union(set2))\r\nprint(set2.union(set1))\r\n# Determining whether one set is a superset of another\r\nset1 = {1,2,3,4,5}\r\nset2 = {1,2,3}\r\nprint(set1.issuperset(set2))\r\nprint(set2.issuperset(set1))\r\n# Output:\r\n#{5, 6, -1}\r\n#{8, -5, 7}\r\n#{0, 4}\r\n#{0, 4, 5, 6, 7, 8, -5, -1}\r\n#{0, 4, 5, 6, 7, 8, -5, -1}\r\n#True\r\n#False\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ex347.py # Operations with sets in python set1 = {-1,0,4,5,6} set2 = {-5,0,4,7,8} # Finding the differences between two sets print(set1.difference(set2)) print(set2.difference(set1)) # Finding the duplicate items in two sets print(set1.intersection(set2)) # Combining sets print(set1.union(set2)) print(set2.union(set1)) # Determining whether one set is a superset of another set1 = {1,2,3,4,5} set2 = {1,2,3} print(set1.issuperset(set2)) print(set2.issuperset(set1)) # [&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-1853","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-tT","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":996,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/178-fractal-tree-using-python-turtle\/","url_meta":{"origin":1853,"position":0},"title":"#178 Fractal tree using python turtle","author":"gantovnik","date":"2021-10-06","format":false,"excerpt":"#178 Fractal tree using python turtle [code language=\"python\"] import turtle # Set up 'constants' # image size IMAGE_SIZE_X = 500 IMAGE_SIZE_Y = 500 # Determines depth of tree - try 2 and 1.25 as alternatives FACTOR = 1.45 def draw_tree(length, width=9): color = 'brown' if length < 1: return elif\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\/2021\/10\/ex178.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":671,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/114-python-script-for-remove-offsets-from-cbar-elements\/","url_meta":{"origin":1853,"position":1},"title":"#114: Python script for remove offsets from CBAR elements","author":"gantovnik","date":"2020-09-28","format":false,"excerpt":"#114: Python script for remove offsets from CBAR elements SOL 106 does not allow offsets in CBAR elements. [code language=\"python\"] print \"-------------------------------------\" print \"remove_cbush_offsets_v1.0\" print \"Vladimir Gantovnik\" print \"March 15 2016\" print \"-------------------------------------\" #input file inName='test.bdf' inFile=open(inName) #output file outName='stripped.bdf' outFile=open(outName,'w') import string while 1 : line=inFile.readline() if not line\u2026","rel":"","context":"In &quot;nastran&quot;","block_context":{"text":"nastran","link":"https:\/\/gantovnik.com\/bio-tips\/category\/nastran\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10318,"url":"https:\/\/gantovnik.com\/bio-tips\/2025\/08\/461-how-to-merge-two-dictionaries-in-python\/","url_meta":{"origin":1853,"position":2},"title":"#461 How to Merge Two Dictionaries in Python","author":"gantovnik","date":"2025-08-23","format":false,"excerpt":"Merging dictionaries is a common task in Python programming. Suppose you have two separate dictionaries and want to combine them into one. In Python 3.9 and later, this is very easy thanks to the union operator (|). # Two sample dictionaries dic_1 = {'x1': 1, 'x2': 2} dic_2 = {'y1':\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":2634,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/436-non-convex-univariate-function-optimization-using-brents-method-in-python\/","url_meta":{"origin":1853,"position":3},"title":"#436 Non-convex univariate function optimization using Brent&#8217;s method in python","author":"gantovnik","date":"2024-07-18","format":false,"excerpt":"Brent\u2019s method is an optimization algorithm that combines a bisecting algorithm (Dekker\u2019s method) and inverse quadratic interpolation. It can be used for constrained and unconstrained univariate function optimization. The Brent-Dekker method is an extension of the bisection method. It is a root-finding algorithm that combines elements of the secant method\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex436.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1758,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/335-solution-of-nonlinear-equation-using-brent-method-in-python\/","url_meta":{"origin":1853,"position":4},"title":"#335 Solution of nonlinear equation by Brent&#8217;s method in python","author":"gantovnik","date":"2023-01-06","format":false,"excerpt":"Brent's method is a hybrid root-finding algorithm combining the bisection method, the secant method, and inverse quadratic interpolation. ex335.py [code language=\"python\"] import numpy as np import matplotlib.pyplot as plt import scipy.optimize as optimize def f(x): return 2*x - 1 + 2*np.cos(np.pi*x) x = np.linspace(0.0,2.0,201) y=f(x) plt.plot(x,y,label='$2x - 1 + 2\\\\cos(\\\\pi\u2026","rel":"","context":"In &quot;optimization&quot;","block_context":{"text":"optimization","link":"https:\/\/gantovnik.com\/bio-tips\/category\/optimization\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex335.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":987,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/175-turtle-graphics-in-python\/","url_meta":{"origin":1853,"position":5},"title":"#175 Turtle graphics in python","author":"gantovnik","date":"2021-10-05","format":false,"excerpt":"#175 Turtle graphics in python [code language=\"python\"] import turtle from random import randint def get_input_angle(): angle = 199 return angle def generate_random_colour(): # Generates an R,G,B values randomly in range 0 to 255 r = randint(0, 255) g = randint(0, 255) b = randint(0, 255) return r, g, b print('Set\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\/2021\/10\/ex175.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex175.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1853","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=1853"}],"version-history":[{"count":0,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/1853\/revisions"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=1853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=1853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=1853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}