{"id":11634,"date":"2022-09-28T03:04:37","date_gmt":"2022-09-27T21:34:37","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/"},"modified":"2022-09-28T03:04:37","modified_gmt":"2022-09-27T21:34:37","slug":"solved-inverted-index-in-python-not-returning-desired-results","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/","title":{"rendered":"[Solved] Inverted Index in Python not returning desired results"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-17555468\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"17555468\" data-parentid=\"17554977\" data-score=\"2\" 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>Based on what you&#8217;re saying, I think you&#8217;re trying to get some data like this:<\/p>\n<pre><code>input = [\"hello world\", \"foo bar\", \"red cat\"]\ndata_wanted = {\n    \"foo\" : 1,\n    \"hello\" : 0,\n    \"cat\" : 2,\n    \"world\" : 0,\n    \"red\" : 2\n    \"bar\" : 1\n}\n<\/code><\/pre>\n<p>So what you should be doing is adding the <em>words<\/em> as keys to a dictionary, and have their values be the index of the substring in <code>strlist<\/code> in which they are located.<\/p>\n<pre><code>def locateWords(strlist):\nd = {}\nfor i, substr in enumerate(strlist):   # gives you the index and the item itself\n    for word in substr.split()\n        d[word] = i\nreturn d\n<\/code><\/pre>\n<p>If the word occurs in more than one string in <code>strlist<\/code>, you should change the code to the following:<\/p>\n<pre><code>def locateWords(strlist):\nd = {}\nfor i, substr in enumerate(strlist):\n    for word in substr.split()\n        if word not in d:\n            d[word] = [i]\n        else:\n            d[word].append(i)\nreturn d\n<\/code><\/pre>\n<p>This changes the values to lists, which contain the indices of the substrings in <code>strlist<\/code> which contain that word.<\/p>\n<h3>Some of your code&#8217;s problems explained<\/h3>\n<ol>\n<li><code>{}<\/code> is not a set, it&#8217;s a dictionary.<\/li>\n<li><code>break<\/code> forces a loop to terminate immediately &#8211; you didn&#8217;t want to end the loop early because you still had data to process.<\/li>\n<li><code>d.update(index)<\/code> will give you a <code>TypeError: 'int' object is not iterable<\/code>. This method actually takes an iterable object and updates the dictionary with it. Normally you would use a list of tuples for this: <code>[(\"foo\",1), (\"hello\",0)]<\/code>. It just adds the data to the dictionary.<\/li>\n<li>You don&#8217;t normally want to use <code>d.__setitem__<\/code> (which you typed wrong anyway). You&#8217;d just use <code>d[key] = value<\/code>.<\/li>\n<li>You can iterate using a &#8220;for each&#8221; style loop instead, like my code above shows. Looping over the range means you are looping over the indices. (Not exactly a problem, but it could lead to extra bugs if you&#8217;re not careful to use the indices properly).<\/li>\n<\/ol>\n<p>It looks like you are coming from another programming language in which braces indicate sets and there is a keyword which ends control blocks (like <code>if, fi<\/code>). It&#8217;s easy to confuse syntax when you&#8217;re first starting &#8211; but if you run into trouble running the code, look at the exceptions you get and search them on the web!<\/p>\n<p><em>P.S.<\/em> I&#8217;m not sure why you wanted a set &#8211; if there are duplicates, you probably want to know all of their locations, not just the first or the last one or anything in between. Just my $0.02.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Inverted Index in Python not returning desired results <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Based on what you&#8217;re saying, I think you&#8217;re trying to get some data like this: input = [&#8220;hello world&#8221;, &#8220;foo bar&#8221;, &#8220;red cat&#8221;] data_wanted = { &#8220;foo&#8221; : 1, &#8220;hello&#8221; : 0, &#8220;cat&#8221; : 2, &#8220;world&#8221; : 0, &#8220;red&#8221; : 2 &#8220;bar&#8221; : 1 } So what you should be doing is adding the &#8230; <a title=\"[Solved] Inverted Index in Python not returning desired results\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\" aria-label=\"More on [Solved] Inverted Index in Python not returning desired results\">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":[821,3180,349],"class_list":["post-11634","post","type-post","status-publish","format-standard","hentry","category-solved","tag-indexing","tag-inverse","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Inverted Index in Python not returning desired results - 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-inverted-index-in-python-not-returning-desired-results\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Inverted Index in Python not returning desired results - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Based on what you&#8217;re saying, I think you&#8217;re trying to get some data like this: input = [&quot;hello world&quot;, &quot;foo bar&quot;, &quot;red cat&quot;] data_wanted = { &quot;foo&quot; : 1, &quot;hello&quot; : 0, &quot;cat&quot; : 2, &quot;world&quot; : 0, &quot;red&quot; : 2 &quot;bar&quot; : 1 } So what you should be doing is adding the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-27T21:34:37+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-inverted-index-in-python-not-returning-desired-results\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Inverted Index in Python not returning desired results\",\"datePublished\":\"2022-09-27T21:34:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\"},\"wordCount\":337,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"indexing\",\"inverse\",\"python\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\",\"name\":\"[Solved] Inverted Index in Python not returning desired results - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-27T21:34:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Inverted Index in Python not returning desired results\"}]},{\"@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] Inverted Index in Python not returning desired results - 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-inverted-index-in-python-not-returning-desired-results\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Inverted Index in Python not returning desired results - JassWeb","og_description":"[ad_1] Based on what you&#8217;re saying, I think you&#8217;re trying to get some data like this: input = [\"hello world\", \"foo bar\", \"red cat\"] data_wanted = { \"foo\" : 1, \"hello\" : 0, \"cat\" : 2, \"world\" : 0, \"red\" : 2 \"bar\" : 1 } So what you should be doing is adding the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/","og_site_name":"JassWeb","article_published_time":"2022-09-27T21:34:37+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-inverted-index-in-python-not-returning-desired-results\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Inverted Index in Python not returning desired results","datePublished":"2022-09-27T21:34:37+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/"},"wordCount":337,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["indexing","inverse","python"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/","url":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/","name":"[Solved] Inverted Index in Python not returning desired results - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-27T21:34:37+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-inverted-index-in-python-not-returning-desired-results\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Inverted Index in Python not returning desired results"}]},{"@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\/11634","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=11634"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11634\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}