Python List - Sort Integers, and then Strings [duplicate]










0















This question already has an answer here:



  • Sorting a mixed list of ints and strings

    2 answers



I need to use the sorted() function in order to get from here:



test2 = [1,'2',3,'4',5,'A']


to here (create a new list which first has the sorted integers, and then has the sorted strings):



test2 = [1,3,5,'2','4','A']









share|improve this question













marked as duplicate by Austin, smci, Community Nov 11 at 6:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Is this python3 or python2. In python2 you could just do sorted(test2).
    – Red Cricket
    Nov 11 at 6:14










  • @Austin: it is a duplicate, but that asking is a terrible target; which should we close into which? I just edited that asking to more clear.
    – smci
    Nov 11 at 6:23











  • @smci, sure; should be closed as long as that answers OP. :)
    – Austin
    Nov 11 at 6:33















0















This question already has an answer here:



  • Sorting a mixed list of ints and strings

    2 answers



I need to use the sorted() function in order to get from here:



test2 = [1,'2',3,'4',5,'A']


to here (create a new list which first has the sorted integers, and then has the sorted strings):



test2 = [1,3,5,'2','4','A']









share|improve this question













marked as duplicate by Austin, smci, Community Nov 11 at 6:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Is this python3 or python2. In python2 you could just do sorted(test2).
    – Red Cricket
    Nov 11 at 6:14










  • @Austin: it is a duplicate, but that asking is a terrible target; which should we close into which? I just edited that asking to more clear.
    – smci
    Nov 11 at 6:23











  • @smci, sure; should be closed as long as that answers OP. :)
    – Austin
    Nov 11 at 6:33













0












0








0








This question already has an answer here:



  • Sorting a mixed list of ints and strings

    2 answers



I need to use the sorted() function in order to get from here:



test2 = [1,'2',3,'4',5,'A']


to here (create a new list which first has the sorted integers, and then has the sorted strings):



test2 = [1,3,5,'2','4','A']









share|improve this question














This question already has an answer here:



  • Sorting a mixed list of ints and strings

    2 answers



I need to use the sorted() function in order to get from here:



test2 = [1,'2',3,'4',5,'A']


to here (create a new list which first has the sorted integers, and then has the sorted strings):



test2 = [1,3,5,'2','4','A']




This question already has an answer here:



  • Sorting a mixed list of ints and strings

    2 answers







python sorting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 6:00









Maiia S.

4119




4119




marked as duplicate by Austin, smci, Community Nov 11 at 6:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Austin, smci, Community Nov 11 at 6:27


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Is this python3 or python2. In python2 you could just do sorted(test2).
    – Red Cricket
    Nov 11 at 6:14










  • @Austin: it is a duplicate, but that asking is a terrible target; which should we close into which? I just edited that asking to more clear.
    – smci
    Nov 11 at 6:23











  • @smci, sure; should be closed as long as that answers OP. :)
    – Austin
    Nov 11 at 6:33
















  • Is this python3 or python2. In python2 you could just do sorted(test2).
    – Red Cricket
    Nov 11 at 6:14










  • @Austin: it is a duplicate, but that asking is a terrible target; which should we close into which? I just edited that asking to more clear.
    – smci
    Nov 11 at 6:23











  • @smci, sure; should be closed as long as that answers OP. :)
    – Austin
    Nov 11 at 6:33















Is this python3 or python2. In python2 you could just do sorted(test2).
– Red Cricket
Nov 11 at 6:14




Is this python3 or python2. In python2 you could just do sorted(test2).
– Red Cricket
Nov 11 at 6:14












@Austin: it is a duplicate, but that asking is a terrible target; which should we close into which? I just edited that asking to more clear.
– smci
Nov 11 at 6:23





@Austin: it is a duplicate, but that asking is a terrible target; which should we close into which? I just edited that asking to more clear.
– smci
Nov 11 at 6:23













@smci, sure; should be closed as long as that answers OP. :)
– Austin
Nov 11 at 6:33




@smci, sure; should be closed as long as that answers OP. :)
– Austin
Nov 11 at 6:33












3 Answers
3






active

oldest

votes


















4














sorted(test2, key=lambda x: (isinstance(x, str), x))


This relies on the fact that False < True and that tuples are sorted lexicographically.



More details:



Tuples are often used to sort by more than one metric.
Consider this:



key=lambda x: isinstance(x, str)


The key keyword argument to sorted tells it to sort values by the results of the given callable and not by the vaules themselves.
This key will return False for integers and True for strings. Since False is equivalent to 0 and True to 1, False is considered smaller and all the integers will be sorted first in the list.



This is only half of what we want because the integers and strings are not sorted among themselves. This is were tuples come in. The above key function:



key=lambda x: (isinstance(x, str), x)


Returns a tuple for each element. Tuples are first sorted by their first element. If two tuples have an equivalent first element, their second elements are compared and so on. This is called lexigoraphic sorting.
Let's say we have this list:



this = ["a", 5, "b", 3]


The key function would return, in order:



(True, "a"), (False, 5), (True, "b"), (False, 3)


The tuples will be sorted first by their first element, the booleans:



(False, 5), (False, 3), (True, "a"), (True, "b")


Now, we have two pairs of tuples which are equal by their first element. Each pair will be sorted internally by their second element:



(False, 3), (False, 5), (True, "a"), (True, "b")


The final result is not the tuples themselves but the corresponding input to the key function for each tuple:



[3, 5, "a", "b"]


In reality, the algorithm doesn't need to sort "twice" but it's conceptually the same.






share|improve this answer






















  • Thank you! Could you please explain the logic briefly?
    – Maiia S.
    Nov 11 at 6:12


















0














try this:



test2 = sorted([i for i in test2 if isinstance(i, int)]) + 
sorted([i for i in test2 if isinstance(i, str)])





share|improve this answer




























    0














    Filter number and string separately and then concatenate them.



    num=list(filter( lambda x: type(x)==int , test2 )) 
    string=list(filter( lambda x: type(x)==str , test2 ))
    sorted(num)+sorted(string)





    share|improve this answer



























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      sorted(test2, key=lambda x: (isinstance(x, str), x))


      This relies on the fact that False < True and that tuples are sorted lexicographically.



      More details:



      Tuples are often used to sort by more than one metric.
      Consider this:



      key=lambda x: isinstance(x, str)


      The key keyword argument to sorted tells it to sort values by the results of the given callable and not by the vaules themselves.
      This key will return False for integers and True for strings. Since False is equivalent to 0 and True to 1, False is considered smaller and all the integers will be sorted first in the list.



      This is only half of what we want because the integers and strings are not sorted among themselves. This is were tuples come in. The above key function:



      key=lambda x: (isinstance(x, str), x)


      Returns a tuple for each element. Tuples are first sorted by their first element. If two tuples have an equivalent first element, their second elements are compared and so on. This is called lexigoraphic sorting.
      Let's say we have this list:



      this = ["a", 5, "b", 3]


      The key function would return, in order:



      (True, "a"), (False, 5), (True, "b"), (False, 3)


      The tuples will be sorted first by their first element, the booleans:



      (False, 5), (False, 3), (True, "a"), (True, "b")


      Now, we have two pairs of tuples which are equal by their first element. Each pair will be sorted internally by their second element:



      (False, 3), (False, 5), (True, "a"), (True, "b")


      The final result is not the tuples themselves but the corresponding input to the key function for each tuple:



      [3, 5, "a", "b"]


      In reality, the algorithm doesn't need to sort "twice" but it's conceptually the same.






      share|improve this answer






















      • Thank you! Could you please explain the logic briefly?
        – Maiia S.
        Nov 11 at 6:12















      4














      sorted(test2, key=lambda x: (isinstance(x, str), x))


      This relies on the fact that False < True and that tuples are sorted lexicographically.



      More details:



      Tuples are often used to sort by more than one metric.
      Consider this:



      key=lambda x: isinstance(x, str)


      The key keyword argument to sorted tells it to sort values by the results of the given callable and not by the vaules themselves.
      This key will return False for integers and True for strings. Since False is equivalent to 0 and True to 1, False is considered smaller and all the integers will be sorted first in the list.



      This is only half of what we want because the integers and strings are not sorted among themselves. This is were tuples come in. The above key function:



      key=lambda x: (isinstance(x, str), x)


      Returns a tuple for each element. Tuples are first sorted by their first element. If two tuples have an equivalent first element, their second elements are compared and so on. This is called lexigoraphic sorting.
      Let's say we have this list:



      this = ["a", 5, "b", 3]


      The key function would return, in order:



      (True, "a"), (False, 5), (True, "b"), (False, 3)


      The tuples will be sorted first by their first element, the booleans:



      (False, 5), (False, 3), (True, "a"), (True, "b")


      Now, we have two pairs of tuples which are equal by their first element. Each pair will be sorted internally by their second element:



      (False, 3), (False, 5), (True, "a"), (True, "b")


      The final result is not the tuples themselves but the corresponding input to the key function for each tuple:



      [3, 5, "a", "b"]


      In reality, the algorithm doesn't need to sort "twice" but it's conceptually the same.






      share|improve this answer






















      • Thank you! Could you please explain the logic briefly?
        – Maiia S.
        Nov 11 at 6:12













      4












      4








      4






      sorted(test2, key=lambda x: (isinstance(x, str), x))


      This relies on the fact that False < True and that tuples are sorted lexicographically.



      More details:



      Tuples are often used to sort by more than one metric.
      Consider this:



      key=lambda x: isinstance(x, str)


      The key keyword argument to sorted tells it to sort values by the results of the given callable and not by the vaules themselves.
      This key will return False for integers and True for strings. Since False is equivalent to 0 and True to 1, False is considered smaller and all the integers will be sorted first in the list.



      This is only half of what we want because the integers and strings are not sorted among themselves. This is were tuples come in. The above key function:



      key=lambda x: (isinstance(x, str), x)


      Returns a tuple for each element. Tuples are first sorted by their first element. If two tuples have an equivalent first element, their second elements are compared and so on. This is called lexigoraphic sorting.
      Let's say we have this list:



      this = ["a", 5, "b", 3]


      The key function would return, in order:



      (True, "a"), (False, 5), (True, "b"), (False, 3)


      The tuples will be sorted first by their first element, the booleans:



      (False, 5), (False, 3), (True, "a"), (True, "b")


      Now, we have two pairs of tuples which are equal by their first element. Each pair will be sorted internally by their second element:



      (False, 3), (False, 5), (True, "a"), (True, "b")


      The final result is not the tuples themselves but the corresponding input to the key function for each tuple:



      [3, 5, "a", "b"]


      In reality, the algorithm doesn't need to sort "twice" but it's conceptually the same.






      share|improve this answer














      sorted(test2, key=lambda x: (isinstance(x, str), x))


      This relies on the fact that False < True and that tuples are sorted lexicographically.



      More details:



      Tuples are often used to sort by more than one metric.
      Consider this:



      key=lambda x: isinstance(x, str)


      The key keyword argument to sorted tells it to sort values by the results of the given callable and not by the vaules themselves.
      This key will return False for integers and True for strings. Since False is equivalent to 0 and True to 1, False is considered smaller and all the integers will be sorted first in the list.



      This is only half of what we want because the integers and strings are not sorted among themselves. This is were tuples come in. The above key function:



      key=lambda x: (isinstance(x, str), x)


      Returns a tuple for each element. Tuples are first sorted by their first element. If two tuples have an equivalent first element, their second elements are compared and so on. This is called lexigoraphic sorting.
      Let's say we have this list:



      this = ["a", 5, "b", 3]


      The key function would return, in order:



      (True, "a"), (False, 5), (True, "b"), (False, 3)


      The tuples will be sorted first by their first element, the booleans:



      (False, 5), (False, 3), (True, "a"), (True, "b")


      Now, we have two pairs of tuples which are equal by their first element. Each pair will be sorted internally by their second element:



      (False, 3), (False, 5), (True, "a"), (True, "b")


      The final result is not the tuples themselves but the corresponding input to the key function for each tuple:



      [3, 5, "a", "b"]


      In reality, the algorithm doesn't need to sort "twice" but it's conceptually the same.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 11 at 6:56

























      answered Nov 11 at 6:07









      roeen30

      44629




      44629











      • Thank you! Could you please explain the logic briefly?
        – Maiia S.
        Nov 11 at 6:12
















      • Thank you! Could you please explain the logic briefly?
        – Maiia S.
        Nov 11 at 6:12















      Thank you! Could you please explain the logic briefly?
      – Maiia S.
      Nov 11 at 6:12




      Thank you! Could you please explain the logic briefly?
      – Maiia S.
      Nov 11 at 6:12













      0














      try this:



      test2 = sorted([i for i in test2 if isinstance(i, int)]) + 
      sorted([i for i in test2 if isinstance(i, str)])





      share|improve this answer

























        0














        try this:



        test2 = sorted([i for i in test2 if isinstance(i, int)]) + 
        sorted([i for i in test2 if isinstance(i, str)])





        share|improve this answer























          0












          0








          0






          try this:



          test2 = sorted([i for i in test2 if isinstance(i, int)]) + 
          sorted([i for i in test2 if isinstance(i, str)])





          share|improve this answer












          try this:



          test2 = sorted([i for i in test2 if isinstance(i, int)]) + 
          sorted([i for i in test2 if isinstance(i, str)])






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 6:05









          mehrdad-pedramfar

          4,19511235




          4,19511235





















              0














              Filter number and string separately and then concatenate them.



              num=list(filter( lambda x: type(x)==int , test2 )) 
              string=list(filter( lambda x: type(x)==str , test2 ))
              sorted(num)+sorted(string)





              share|improve this answer

























                0














                Filter number and string separately and then concatenate them.



                num=list(filter( lambda x: type(x)==int , test2 )) 
                string=list(filter( lambda x: type(x)==str , test2 ))
                sorted(num)+sorted(string)





                share|improve this answer























                  0












                  0








                  0






                  Filter number and string separately and then concatenate them.



                  num=list(filter( lambda x: type(x)==int , test2 )) 
                  string=list(filter( lambda x: type(x)==str , test2 ))
                  sorted(num)+sorted(string)





                  share|improve this answer












                  Filter number and string separately and then concatenate them.



                  num=list(filter( lambda x: type(x)==int , test2 )) 
                  string=list(filter( lambda x: type(x)==str , test2 ))
                  sorted(num)+sorted(string)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 6:19









                  PIG

                  1146




                  1146













                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo