Python Forum
Sorting list of lists with string and int
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting list of lists with string and int
#1
So I'm trying to solve this exercise: https://open.kattis.com/problems/cups

Although the problem is simple and does not require much work I'd like to improve my code because it looks pretty bad:

#!/usr/bin/python3

n = int(input())

a = [input().split() for i in range(n)]

for i in a:
    if(i[0].isdigit()):
        i[0] = int(i[0])/2
        i[1], i[0] = i[0], i[1]
    elif(i[1].isdigit()):
        i[1] = int(i[1])

a.sort(key=lambda x: x[1])
[print(i[0]) for i in a]
The main concern is that I don't know if the string will be in a form int string or string int so I need to check for it.
Reply


Messages In This Thread
Sorting list of lists with string and int - by Otbredbaron - May-06-2018, 04:26 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with sorting lists as a beginner Realist1c 1 759 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 930 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,079 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,083 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,678 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  List Sorting Problem ZZTurn 5 1,351 Sep-22-2022, 11:23 PM
Last Post: ZZTurn
  Sorting List finndude 9 2,487 Jan-27-2022, 09:37 PM
Last Post: Pedroski55
  How to efficiently average same entries of lists in a list xquad 5 2,150 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,896 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,835 Aug-11-2021, 03:45 PM
Last Post: jamesaarr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020