Author Topic: python integer sorting  (Read 2993 times)

0 Members and 1 Guest are viewing this topic.

PegLegTV

  • $upporter
  • Sr. Member
  • *****
  • Posts: 497
  • Karma: 43
    • View Profile
python integer sorting
« on: March 14, 2019, 02:22:23 AM »
UPDATE:

Thanks to Jitterjames impressive help, here are some examples of sorting numbers with python.

here is a group of commands as examples for others

Replace     1, 3, 4, 2, 5       with         your numbers    that you want sorted

Python Numbers Sorting Examples
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.4.5-->
<commandGroup open="True" name="Python Number sorting Examples" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="916" name="Sorting for Lowest Number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] </param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers.sort()</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="918" name="Compressed Sorting for Lowest Number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] {CR}numbers.sort(){CR}result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="917" name="Sorting for Highest number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] </param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers.sort(reverse = True)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="950" name="Compressed Sorting for Highest number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] {CR}numbers.sort(reverse = True){CR}result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
</commandGroup>

===================================================================================================
Original Post:

I gotta say I thought this would be a simple task  :biglaugh  :bonk

I'm trying to sort a list of integers using python

list:
5, 3, 9, 4, 2, 6, 8, 7, 11, 10, 1

wanting:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

I thought I would be able to do it with python but I haven't had much luck, here's some of what I have tried

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.4.5-->
<command id="847" name="Python sorting" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Results.SetLastResult</cmdType>
    <params>
      <param>5, 3, 9, 4, 2, 6, 8, 7, 11, 10, 1</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=sorted={lastResult}</param>
    </params>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=sorted{lastResult}</param>
    </params>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=sorted({lastResult})</param>
    </params>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=sort({lastResult})</param>
    </params>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=sort{lastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

I'm needing to sort the list numerically, so I can get the highest number in the list for naming custom Youtube episodes, any help would be great  ::club  ;)
« Last Edit: March 15, 2019, 12:58:58 PM by PegLegTV »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: python integer sorting
« Reply #1 on: March 14, 2019, 10:39:27 AM »
I don't have access to a Windows PC at the moment so I can't give you the exact solution but take a look here

https://www.geeksforgeeks.org/python-list-sort/

Note that it may be easier to use more than one line of Python code.

Note that you will need square brackets around the list of nu!here as shown in the example.

Don't try to store the list in last result, first put it into a new variable like in the example.

Good luck. If you don't get it I can help later.

PegLegTV

  • $upporter
  • Sr. Member
  • *****
  • Posts: 497
  • Karma: 43
    • View Profile
Re: python integer sorting
« Reply #2 on: March 14, 2019, 01:52:43 PM »
Thank you for the link, I appreciate the response especially since your not at a windows pc, I know that helping with something like this can be frustrating with out the resources in front of you (the master  ;)) and some one who doesn't know python (the student  :bonk)

warning
I don't know much about python at all, so I'm reaching trying to even do something like this

I did some testing with the link you shared.

if I run the action below I'm able to get the sorted list to show up in the py plugin log, but I'm not sure how to get that information in my VC Command
Code: [Select]
PY.ExecString&&numbers = [1, 3, 4, 2] {CR}{CR}numbers.sort(){CR}{CR}print(numbers)
I've tried using PY.GetVar and PY.GetList but they return LastResult as "IronPython.Runtime.List"
Code: [Select]
PY.GetVar&&numbers        and        PY.GetList&&numbers

Copy of the command I've tested:
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.4.5-->
<command id="1164" name="New python sorting Test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="IronPython.Runtime.List">
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=[1, 3, 4, 2] {CR}{CR}result.sort(){CR}{CR}print(result)</param>
    </params>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>result=numbers = [1, 3, 4, 2] {CR}{CR}numbers.sort(){CR}{CR}print(numbers)</param>
    </params>
    <cmdRepeat>0</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.ExecString</cmdType>
    <params>
      <param>numbers = [1, 3, 4, 2] {CR}{CR}numbers.sort(){CR}{CR}print(numbers)</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>PY.GetVar</cmdType>
    <params>
      <param>numbers</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>System.SetClipboardText</cmdType>
    <params>
      <param>{LastResult}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>What's it like out there?</phrase>
</command>


It's not a big deal if I need to wait for your help, the command that needs this is for naming youtube videos as episodes so I can view them in Kodi later on.



also I have a py file in my python folder that I use for CPU load (made either by you or nime5ter I believe), I'm not sure if that would be helpful or not but thought it was worth noting


jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: python integer sorting
« Reply #3 on: March 15, 2019, 10:33:36 AM »
I think you are almost there.

LastResult is expecting a string and numbers is a list (of numbers).

If you give LastResult a number it will convert it to a string but with a list it doesn't know what to do so it returns the data type.

If you want the smallest number, first sort and then set LastResult to numbers[0] which should give you the first number in the list. To get the largest use a reverse sort and then do the same.

By the way it's fine to use {CR} but I think you only need one per line and also you should be able to use multiple PY.ExecString actions in a macro if you want. The Python state is not reset between calls. Even between multiple voice commands variables and other things like function definitions will persist until VoxCommando is restarted.

PegLegTV

  • $upporter
  • Sr. Member
  • *****
  • Posts: 497
  • Karma: 43
    • View Profile
Re: python integer sorting
« Reply #4 on: March 15, 2019, 12:52:20 PM »
That did it! You are awesome  8) :yay


Quote
By the way it's fine to use {CR} but I think you only need one per line
I was using {CR}{CR} instead of {CR} because one of my other python strings had the double line break so I thought it was needed but you where right only one line break ({CR}) is needed


Quote
also you should be able to use multiple PY.ExecString actions in a macro if you want. The Python state is not reset between calls. Even between multiple voice commands variables and other things like function definitions will persist until VoxCommando is restarted.

your right again, when I was testing I wasn't having any luck breaking them up so I though it had to all be with in one action, I was able to break it into 3 actions and it worked


here is a group of commands as examples for others

Replace     1, 3, 4, 2, 5       with         your numbers    that you want sorted

Python Numbers Sorting Examples
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.4.5-->
<commandGroup open="True" name="Python Number sorting Examples" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="916" name="Sorting for Lowest Number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] </param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers.sort()</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="918" name="Compressed Sorting for Lowest Number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] {CR}numbers.sort(){CR}result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="917" name="Sorting for Highest number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] </param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers.sort(reverse = True)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="950" name="Compressed Sorting for Highest number" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>numbers = [1, 3, 4, 2, 5] {CR}numbers.sort(reverse = True){CR}result=numbers[0]</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>{LastResult}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
</commandGroup>

warnerbeck

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
Re: python integer sorting
« Reply #5 on: March 26, 2019, 02:30:34 AM »

Note that it may be easier to use more than one line of Python code.


A complete guide on....Python List