<?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: Python to compare values for ranking in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178622#M26353</link>
    <description>&lt;P&gt;You are asking about the situation when 2 of the types have the same value, but it is hard for people to provide any code samples when they don't know what is supposed to be returned in general.&amp;nbsp; Are you trying to return the type with the highest count from the data?&amp;nbsp; And when the highest count is shared between types, you want only the most important type?&lt;/P&gt;</description>
    <pubDate>Tue, 31 May 2022 15:46:39 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2022-05-31T15:46:39Z</dc:date>
    <item>
      <title>Python to compare values for ranking</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178229#M26349</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have a dataset with four 'types' and their 'counts' for example:&lt;/P&gt;&lt;P&gt;A = 4, B = 2, C = 0, D = 0&lt;/P&gt;&lt;P&gt;In order of ranking, D (is the most important) &amp;gt; C &amp;gt; B &amp;gt; A (lowest ranking).&lt;/P&gt;&lt;P&gt;If I have an output with:&lt;/P&gt;&lt;P&gt;A = 0, B = 1, C = 0 , D = 1&lt;/P&gt;&lt;P&gt;How do I script in Python to compare the string so that when B = D that the variable I capture will be D based on ranking?&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Craig&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2022 03:05:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178229#M26349</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-05-30T03:05:41Z</dc:date>
    </item>
    <item>
      <title>Re: Python to compare values for ranking</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178263#M26350</link>
      <description>&lt;UL&gt;&lt;LI&gt;what is the type of your output? string, list, dict, something else?&lt;/LI&gt;&lt;LI&gt;are the types actually just letters or are you simplifying things?&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Assuming that you return a string and that the types are just letters (getting more important in ascending alphabetical order):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;output = "A = 0, B = 1, C = 0 , D = 1"

# change to [ [type, count] ]
converted_output = output.replace(" ", "").split(",")
converted_output = [tc.split("=") for tc in converted_output]
print(f"converted output: {converted_output}")

# sort by count and type (both descending), return first element
sorted_output = sorted(converted_output, key=lambda r: (r[1], r[0]), reverse=True)
print(f"sorted output: {sorted_output}")
most_significant_output = sorted_output[0]
print(f"most significant output: {most_significant_output}")


#converted output: [['A', '0'], ['B', '1'], ['C', '0'], ['D', '1']]
#sorted output: [['D', '1'], ['B', '1'], ['C', '0'], ['A', '0']]
#most significant output: ['D', '1']&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If your types are actually not letters but e.g. species, you have to define how to rank them and then use the &lt;STRONG&gt;list.index(element)&lt;/STRONG&gt; method in the sort:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;output = "Pig = 1, Lamb = 1, Chicken = 0, Duck = 1, Cow = 0"

# specify the ranking of the types, starting from lowest
ranked_types = ["Cow", "Pig", "Duck", "Horse", "Lamb", "Chicken"]

# change to [ [type, count] ]
converted_output = output.replace(" ", "").split(",")
converted_output = [tc.split("=") for tc in converted_output]
print(f"converted output: {converted_output}")

# sort by count and type (both descending), return first element
sorted_output = sorted(converted_output, key=lambda r: (r[1], ranked_types.index(r[0])), reverse=True)
print(f"sorted output: {sorted_output}")
most_significant_output = sorted_output[0]
print(f"most significant output: {most_significant_output}")


#converted output: [['Pig', '1'], ['Lamb', '1'], ['Chicken', '0'], ['Duck', '1'], ['Cow', '0']]
#sorted output: [['Lamb', '1'], ['Duck', '1'], ['Pig', '1'], ['Chicken', '0'], ['Cow', '0']]
#most significant output: ['Lamb', '1']&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 May 2022 07:45:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178263#M26350</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-05-30T07:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: Python to compare values for ranking</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178622#M26353</link>
      <description>&lt;P&gt;You are asking about the situation when 2 of the types have the same value, but it is hard for people to provide any code samples when they don't know what is supposed to be returned in general.&amp;nbsp; Are you trying to return the type with the highest count from the data?&amp;nbsp; And when the highest count is shared between types, you want only the most important type?&lt;/P&gt;</description>
      <pubDate>Tue, 31 May 2022 15:46:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1178622#M26353</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2022-05-31T15:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Python to compare values for ranking</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1179678#M26357</link>
      <description>&lt;P&gt;&lt;A href="https://community.esri.com/t5/user/viewprofilepage/user-id/294341" target="_blank"&gt;@Johannes&lt;/A&gt;,&lt;/P&gt;&lt;P&gt;Your script interprets exactly what I am after, but I have one final issue.&lt;/P&gt;&lt;P&gt;My list of tuple values 'lst' works for the majority of returned values, however I occasionally have the instance where the value sorted and ranked is incorrect such as below:&lt;/P&gt;&lt;P&gt;lst = [['Rabbit', '7'], ['Dog', '3'], ['Bird', '17'], ['Cat', '0']]&lt;BR /&gt;rnk_lst = ['Cat', 'Bird', 'Dog', 'Rabbit']&lt;BR /&gt;sorted_output = sorted(lst, key=lambda r: (r[1], rnk_lst.index(r[0])), reverse=True)&lt;BR /&gt;print(f"sorted output: {sorted_output}")&lt;/P&gt;&lt;P&gt;most_significant_output = sorted_output[0]&lt;BR /&gt;print(most_significant_output[0])&lt;/P&gt;&lt;PRE&gt;sorted output: [['Rabbit', '7'], ['Dog', '3'], ['Bird', '17'], ['Cat', '0']]
Rabbit&lt;/PRE&gt;&lt;P&gt;If the maximum value is ['Bird', 17] that is the value I expected, yet script equates Rabbit.&lt;/P&gt;&lt;P&gt;Not sure why it works for most of analysis process, but trips up on a few response. Any ideas?&lt;/P&gt;&lt;P&gt;Craig&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2022 00:17:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1179678#M26357</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-06-03T00:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Python to compare values for ranking</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1180916#M26359</link>
      <description>&lt;P&gt;It's because Python sorts the counts as strings (because they are), not as numbers. And in string sorting, "7" is greater than "17".&lt;/P&gt;&lt;P&gt;To solve that, you have to cast the string to int in the sorted() call.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;lst = [['Rabbit', '7'], ['Dog', '3'], ['Bird', '17'], ['Cat', '0']]
rnk_lst = ['Cat', 'Bird', 'Dog', 'Rabbit']
sorted_output = sorted(lst, key=lambda r: (int(r[1]), rnk_lst.index(r[0])), reverse=True)
print(f"sorted output: {sorted_output}")

most_significant_output = sorted_output[0]
print(most_significant_output[0])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;sorted output: [['Bird', '17'], ['Rabbit', '7'], ['Dog', '3'], ['Cat', '0']]
Bird&lt;/PRE&gt;</description>
      <pubDate>Wed, 08 Jun 2022 06:12:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1180916#M26359</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-06-08T06:12:53Z</dc:date>
    </item>
    <item>
      <title>Re: Python to compare values for ranking</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1181304#M26360</link>
      <description>&lt;P&gt;That solves my issue perfectly. Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 22:44:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/python-to-compare-values-for-ranking/m-p/1181304#M26360</guid>
      <dc:creator>CPoynter</dc:creator>
      <dc:date>2022-06-08T22:44:25Z</dc:date>
    </item>
  </channel>
</rss>

