{"id":9440,"date":"2025-04-20T04:56:26","date_gmt":"2025-04-20T11:56:26","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=9440"},"modified":"2025-04-20T05:31:09","modified_gmt":"2025-04-20T12:31:09","slug":"459-python-script-to-group-consecutive-numbers-into-ranges-like-in-msc-patran","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2025\/04\/459-python-script-to-group-consecutive-numbers-into-ranges-like-in-msc-patran\/","title":{"rendered":"#459 Python Script to Group Consecutive Numbers into Ranges Like in MSC Patran"},"content":{"rendered":"<p>This Python script reads a text file containing a list of numbers (such as node or element IDs), sorts them, removes duplicates, and groups consecutive numbers into compact ranges &#8211; just like MSC Patran&#8217;s range format. It&#8217;s a handy tool for preprocessing or organizing large ID lists efficiently.<\/p>\n<pre class=\"lang:python decode:true\">def read_numbers(filename):\n    with open(filename, 'r') as file:\n        numbers = [int(line.strip()) for line in file if line.strip().isdigit()]\n    return sorted(set(numbers))  # Sort and remove duplicates\n\ndef find_ranges(numbers):\n    if not numbers:\n        return []\n\n    ranges = []\n    start = prev = numbers[0]\n\n    for num in numbers[1:]:\n        if num == prev + 1:\n            prev = num\n        else:\n            if start == prev:\n                ranges.append(str(start))\n            else:\n                ranges.append(f\"{start}:{prev}\")\n            start = prev = num\n\n    # Add the final range\n    if start == prev:\n        ranges.append(str(start))\n    else:\n        ranges.append(f\"{start}:{prev}\")\n\n    return ranges\n\n# Example usage\ninput_filename = \"elems.txt\"\nnumbers = read_numbers(input_filename)\nranges = find_ranges(numbers)\nprint(\" \".join(ranges))\n\noutput_filename = \"ranges.txt\"\nfout = open(output_filename, 'w')\nprint(\" \".join(ranges), file=fout)\nfout.close()<\/pre>\n<p>Input file: elems.txt<\/p>\n<pre class=\"lang:python decode:true \">1\n2\n3\n4\n5\n6\n10\n11\n23\n14\n15\n83\n16\n12\n25\n24<\/pre>\n<p>Output file: ranges.txt<\/p>\n<pre class=\"lang:python decode:true \">1:6 10:12 14:16 23:25 83<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This Python script reads a text file containing a list of numbers (such as node or element IDs), sorts them, removes duplicates, and groups consecutive numbers into compact ranges &#8211; just like MSC Patran&#8217;s range format. It&#8217;s a handy tool for preprocessing or organizing large ID lists efficiently. def read_numbers(filename): with open(filename, &#8216;r&#8217;) as file: [&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":[39,2],"tags":[324,3],"class_list":["post-9440","post","type-post","status-publish","format-standard","hentry","category-patran","category-python","tag-patran","tag-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-2sg","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":700,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/125-xdb-vs-op2-in-nastran\/","url_meta":{"origin":9440,"position":0},"title":"#125: xdb vs op2 in Nastran","author":"gantovnik","date":"2020-09-28","format":false,"excerpt":"#125: xdb vs op2 in Nastran xdb: Entry: PARAM,POST,0 Results are accessed through Direct Results Access (DRA) working directly with MSC Nastran results database 1) xdb files are compatible primarily with Patran and other MSC Software. 2) Time to read the xdb results into the Patran database is eliminated (accessed\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":987,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/175-turtle-graphics-in-python\/","url_meta":{"origin":9440,"position":1},"title":"#175 Turtle graphics in python","author":"gantovnik","date":"2021-10-05","format":false,"excerpt":"#175 Turtle graphics in python","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":[]},{"id":1695,"url":"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-6\/","url_meta":{"origin":9440,"position":2},"title":"#321 unittest &#8211; unit testing framework in python","author":"gantovnik","date":"2022-11-30","format":false,"excerpt":"calculator.py test_calculator.py Output:","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":993,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/177-random-snowflakes-using-python-turtle\/","url_meta":{"origin":9440,"position":3},"title":"#177 Random snowflakes using python turtle","author":"gantovnik","date":"2021-10-06","format":false,"excerpt":"#177 Random snowflakes using python turtle","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\/ex177.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex177.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1458,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/05\/210-parametric-curve-in-3d-2-2-2-2-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2-2-2-2-2-3-2-2-2-2-2\/","url_meta":{"origin":9440,"position":4},"title":"#281 How to convert numbers from Nastran notation to float","author":"gantovnik","date":"2022-05-29","format":false,"excerpt":"Often, it is necessary to convert numbers from Nastran notation to float. For example, convert -5.75-3 into -5.75e-3. We can write a simple python function for such conversion.","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":686,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/09\/120-sol200-extract-the-updated-element-thickness-during-topology-optimization\/","url_meta":{"origin":9440,"position":5},"title":"#120: SOL200 &#8211; Extract the updated element thickness during topology optimization.","author":"gantovnik","date":"2020-09-28","format":false,"excerpt":"#119: SOL200 - Extract the updated element thickness during topology optimization. 1) To extract the updated (modified) element thicknesses, use ECHO=PUNCH (NEWBULK) in the case control section. 2) Read this punch file, which contains updated bulk data into Patran. In the new database, go to File-->Import-->MSC Nastran input-->In the file\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":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/9440","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=9440"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/9440\/revisions"}],"predecessor-version":[{"id":9451,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/9440\/revisions\/9451"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=9440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=9440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=9440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}