{"id":7844,"date":"2022-09-10T15:09:34","date_gmt":"2022-09-10T09:39:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/"},"modified":"2022-09-10T15:09:34","modified_gmt":"2022-09-10T09:39:34","slug":"solved-count-occurences-of-all-items-of-a-list-in-a-tuple","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/","title":{"rendered":"[Solved] Count occurences of all items of a list in a tuple"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-47518323\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"47518323\" data-parentid=\"47518293\" data-score=\"5\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Being NumPy tagged, here&#8217;s a NumPy solution &#8211;<\/p>\n<pre><code>In [846]: import numpy as np\n\nIn [847]: t = (1,5,2,3,4,5,6,7,3,2,2,4,3)\n\nIn [848]: a = [1,2,3]\n\nIn [849]: np.in1d(t,a).sum()\nOut[849]: 7\n\n# Alternatively with np.count_nonzero for summing booleans\nIn [850]: np.count_nonzero(np.in1d(t,a))\nOut[850]: 7\n<\/code><\/pre>\n<p>Another NumPy one with <code>np.bincount<\/code> for the specific case of positive numbered elements in the inputs, basically using the numbers as bins, then doing bin based summing, indexing into those with the list elements to get the counts and a final summation for the final output &#8211;<\/p>\n<pre><code>In [856]: np.bincount(t)[a].sum()\nOut[856]: 7\n<\/code><\/pre>\n<p>Other approaches &#8211;<\/p>\n<pre><code>from collections import Counter\n# @Brad Solomon's soln\ndef collections_counter(tgt, tup):\n    counts = Counter(tup)\n    return sum(counts[t] for t in tgt)\n\n# @timgeb's soln\ndef set_sum(l, t):\n    l = set(l)\n    return sum(1 for x in t if x in l)\n\n# @Amit Tripathi's soln\ndef dict_sum(l, t):\n    dct = {}\n    for i in t:\n        if not dct.get(i):\n            dct[i] = 0\n        dct[i] += 1\n    return sum(dct.get(i, 0) for i in l)\n<\/code><\/pre>\n<p><strong>Runtime tests<\/strong><\/p>\n<p>Case #1 : Timings on a tuple with <code>10,000<\/code> elements and with a list of <code>100<\/code> random elements off it &#8211;<\/p>\n<pre><code>In [905]: a = np.random.choice(1000, 100, replace=False).tolist()\n\nIn [906]: t = tuple(np.random.randint(1,1000,(10000)))\n\nIn [907]: %timeit dict_sum(a, t)\n     ...: %timeit set_sum(a, t)\n     ...: %timeit collections_counter(a, t)\n     ...: %timeit np.in1d(t,a).sum()\n     ...: %timeit np.bincount(t)[a].sum()\n100 loops, best of 3: 2 ms per loop\n1000 loops, best of 3: 437 \u00b5s per loop\n100 loops, best of 3: 2.44 ms per loop\n1000 loops, best of 3: 1.18 ms per loop\n1000 loops, best of 3: 503 \u00b5s per loop\n<\/code><\/pre>\n<p><code>set_sum<\/code> from @timgeb&#8217;s soln looks quite efficient for such inputs.<\/p>\n<p>Case #2 : Timings on a tuple with <code>100,000<\/code> elements that has <code>10,000<\/code> unique elements and with a list of <code>1000<\/code> unique random elements off it &#8211;<\/p>\n<pre><code>In [916]: t = tuple(np.random.randint(0,10000,(100000)))\n\nIn [917]: a = np.random.choice(10000, 1000, replace=False).tolist()\n\nIn [918]: %timeit dict_sum(a, t)\n     ...: %timeit set_sum(a, t)\n     ...: %timeit collections_counter(a, t)\n     ...: %timeit np.in1d(t,a).sum()\n     ...: %timeit np.bincount(t)[a].sum()\n10 loops, best of 3: 21.1 ms per loop\n100 loops, best of 3: 5.33 ms per loop\n10 loops, best of 3: 24.2 ms per loop\n100 loops, best of 3: 13.4 ms per loop\n100 loops, best of 3: 5.05 ms per loop\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Count occurences of all items of a list in a tuple <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Being NumPy tagged, here&#8217;s a NumPy solution &#8211; In [846]: import numpy as np In [847]: t = (1,5,2,3,4,5,6,7,3,2,2,4,3) In [848]: a = [1,2,3] In [849]: np.in1d(t,a).sum() Out[849]: 7 # Alternatively with np.count_nonzero for summing booleans In [850]: np.count_nonzero(np.in1d(t,a)) Out[850]: 7 Another NumPy one with np.bincount for the specific case of positive numbered elements &#8230; <a title=\"[Solved] Count occurences of all items of a list in a tuple\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\" aria-label=\"More on [Solved] Count occurences of all items of a list in a tuple\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[540,581,349,1064],"class_list":["post-7844","post","type-post","status-publish","format-standard","hentry","category-solved","tag-list","tag-numpy","tag-python","tag-tuples"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Count occurences of all items of a list in a tuple - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Count occurences of all items of a list in a tuple - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Being NumPy tagged, here&#8217;s a NumPy solution &#8211; In [846]: import numpy as np In [847]: t = (1,5,2,3,4,5,6,7,3,2,2,4,3) In [848]: a = [1,2,3] In [849]: np.in1d(t,a).sum() Out[849]: 7 # Alternatively with np.count_nonzero for summing booleans In [850]: np.count_nonzero(np.in1d(t,a)) Out[850]: 7 Another NumPy one with np.bincount for the specific case of positive numbered elements ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-10T09:39:34+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Count occurences of all items of a list in a tuple\",\"datePublished\":\"2022-09-10T09:39:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\"},\"wordCount\":135,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"list\",\"numpy\",\"python\",\"tuples\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\",\"name\":\"[Solved] Count occurences of all items of a list in a tuple - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-10T09:39:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Count occurences of all items of a list in a tuple\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Count occurences of all items of a list in a tuple - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Count occurences of all items of a list in a tuple - JassWeb","og_description":"[ad_1] Being NumPy tagged, here&#8217;s a NumPy solution &#8211; In [846]: import numpy as np In [847]: t = (1,5,2,3,4,5,6,7,3,2,2,4,3) In [848]: a = [1,2,3] In [849]: np.in1d(t,a).sum() Out[849]: 7 # Alternatively with np.count_nonzero for summing booleans In [850]: np.count_nonzero(np.in1d(t,a)) Out[850]: 7 Another NumPy one with np.bincount for the specific case of positive numbered elements ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/","og_site_name":"JassWeb","article_published_time":"2022-09-10T09:39:34+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Count occurences of all items of a list in a tuple","datePublished":"2022-09-10T09:39:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/"},"wordCount":135,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["list","numpy","python","tuples"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/","url":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/","name":"[Solved] Count occurences of all items of a list in a tuple - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-10T09:39:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-count-occurences-of-all-items-of-a-list-in-a-tuple\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Count occurences of all items of a list in a tuple"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=7844"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7844\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}