<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Simpler way to get combinations and group them based on key values? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253369#M66731</link>
    <description>&lt;P&gt;After taking a longer look at your data and code, it seems you are trying to reinvent the &lt;A href="https://docs.python.org/3/library/itertools.html#itertools.product" target="_self"&gt;product - &lt;/A&gt;&lt;SPAN&gt;&lt;A href="https://docs.python.org/3/library/itertools.html#itertools.product" target="_self"&gt;itertools — Functions creating iterators for efficient looping&lt;/A&gt; wheel.&amp;nbsp; I can confidently state that any native Python code someone writes will not outperform the compiled functions in the itertools module.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from itertools import product
Fields, Grps = ['FieldA','FieldB','FieldC'], ['GroupA','GroupB','GroupC']
Main = [['DOG','CAT','HORSE'], ['HOUSE','BARN'], ['FIELDS', 'YARD'],['MOUSE','SNAKE','LIZARD','OTHER'],['AQUARIUM','GLASS TANK','SMALL CAGE'],
        ['HOUSE','APARTMENT','PET STORE'],['COW','PIG', 'SHEEP'], ['FIELDS','PASTURES'], ['BARN','FARM HOUSE','OTHER']]
Main = {g:list(product(*Main[i*3:i*3+3])) for i,g in enumerate(Grps)}
print(Main)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 31 Jan 2023 15:47:50 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2023-01-31T15:47:50Z</dc:date>
    <item>
      <title>Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253097#M66717</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I wanted to see if anyone could help with figuring out if there is a simpler way to code what I have below. What I have below works fine but I wanted to see if there was a simpler way to write the code.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;Fields, Grps = ['FieldA','FieldB','FieldC'], ['GroupA','GroupB','GroupC']
Main = [['DOG','CAT','HORSE'], ['HOUSE','BARN'], ['FIELDS', 'YARD'],['MOUSE','SNAKE','LIZARD','OTHER'],['AQUARIUM','GLASS TANK','SMALL CAGE'],
        ['HOUSE','APARTMENT','PET STORE'],['COW','PIG', 'SHEEP'], ['FIELDS','PASTURES'], ['BARN','FARM HOUSE','OTHER']]
MainDict = dict(zip([chr(i) for i in range(ord('A'),ord('A')+len(Main))], Main))
EstVOrder = [[i for i in [chr(i) for i in range(ord('A')+n,ord('A')+n+len(Fields))]] for n in range(0,len(Main),len(Main)//len(Fields))]
Main = dict(zip(Grps,[[[x,y,z] for x in MainDict[a] for y in MainDict[b] for z in MainDict[c]] for a,b,c in EstVOrder]))
print (Main)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would also like to know if the code above is properly written.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 21:53:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253097#M66717</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2023-01-30T21:53:07Z</dc:date>
    </item>
    <item>
      <title>Re: Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253129#M66718</link>
      <description>&lt;P&gt;We code for people, so keep it zen:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import this&lt;/LI-CODE&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;Beautiful is better than ugly.&lt;/P&gt;&lt;P&gt;Simple is better than complex.&lt;BR /&gt;Complex is better than complicated.&lt;BR /&gt;Flat is better than nested.&lt;BR /&gt;Sparse is better than dense.&lt;BR /&gt;Readability counts.&lt;BR /&gt;Special cases aren't special enough to break the rules.&lt;BR /&gt;Although practicality beats purity.&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;If the implementation is hard to explain, it's a bad idea.&lt;BR /&gt;If the implementation is easy to explain, it may be a good idea.&lt;/P&gt;&lt;P&gt;Think I'd add some comments of the what the list comprehensions are creating to add clarity.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# MainDict = {'A': ['DOG', 'CAT', 'HORSE'], 'B': ['HOUSE', 'BARN'], ...}

# Main = {'GroupA': [['DOG', 'HOUSE', 'FIELDS'], ..., ['HORSE', 'BARN', 'YARD']], ...}&lt;/LI-CODE&gt;&lt;P&gt;I suspect Pandas would make short work of creating your final dictionary in a much more elegant way.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jan 2023 22:56:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253129#M66718</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-01-30T22:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253269#M66726</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;@Anonymous User,&lt;/P&gt;&lt;P&gt;The code itself was to see if there was a way to create a list of lists with combinations of values that were the same length and order as the fields. I was also trying to do it with the fewest lines of code possible to see if it could&amp;nbsp;be done. I know the code itself is not that pretty, beautiful, or easy to read/understand, but my goal was to see if I could achieve a complex task with the fewest lines&amp;nbsp;of code.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2023 13:45:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253269#M66726</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2023-01-31T13:45:35Z</dc:date>
    </item>
    <item>
      <title>Re: Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253339#M66728</link>
      <description>&lt;P&gt;Code golf might be a fun exercise, but it usually leads to challenging code to read and even poorer performing code at times.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2023 15:18:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253339#M66728</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2023-01-31T15:18:28Z</dc:date>
    </item>
    <item>
      <title>Re: Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253358#M66730</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1371"&gt;@JoshuaBixby&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I thought of it as a fun exercise to see if I could utilize it for smaller scripts since it gave me the opportunity to try and test what I can do. I was not aware of the performance issues regarding how the code is written, so I will have a to run a few test&amp;nbsp;scripts to see how significant the difference is.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2023 15:35:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253358#M66730</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2023-01-31T15:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253369#M66731</link>
      <description>&lt;P&gt;After taking a longer look at your data and code, it seems you are trying to reinvent the &lt;A href="https://docs.python.org/3/library/itertools.html#itertools.product" target="_self"&gt;product - &lt;/A&gt;&lt;SPAN&gt;&lt;A href="https://docs.python.org/3/library/itertools.html#itertools.product" target="_self"&gt;itertools — Functions creating iterators for efficient looping&lt;/A&gt; wheel.&amp;nbsp; I can confidently state that any native Python code someone writes will not outperform the compiled functions in the itertools module.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from itertools import product
Fields, Grps = ['FieldA','FieldB','FieldC'], ['GroupA','GroupB','GroupC']
Main = [['DOG','CAT','HORSE'], ['HOUSE','BARN'], ['FIELDS', 'YARD'],['MOUSE','SNAKE','LIZARD','OTHER'],['AQUARIUM','GLASS TANK','SMALL CAGE'],
        ['HOUSE','APARTMENT','PET STORE'],['COW','PIG', 'SHEEP'], ['FIELDS','PASTURES'], ['BARN','FARM HOUSE','OTHER']]
Main = {g:list(product(*Main[i*3:i*3+3])) for i,g in enumerate(Grps)}
print(Main)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2023 15:47:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253369#M66731</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2023-01-31T15:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Simpler way to get combinations and group them based on key values?</title>
      <link>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253416#M66735</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/1371"&gt;@JoshuaBixby&lt;/a&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I didn't even realize that I had replicated an existing function. I knew of the itertools, but from examples I have seen online, I thought it might be limited, or there were other ways to achieve the same thing without importing another module. I will research more into these and other python modules to see if some of the code that I have written can be simplified using an existing module.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2023 17:01:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/simpler-way-to-get-combinations-and-group-them/m-p/1253416#M66735</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2023-01-31T17:01:19Z</dc:date>
    </item>
  </channel>
</rss>

