Home > Software design >  I'm wondering why I'm getting a TypeError: argument of type 'int' is not iterabl
I'm wondering why I'm getting a TypeError: argument of type 'int' is not iterabl

Time:12-04

I'm trying to make a list of names based off the last number in the values list. The new list will be ordered based on highest number to lowest number but is a list of the names.

folks = {'Leia': [28, 'F', 'W', False, True, 'Unemployed',1], 
        'Junipero': [15, 'M', 'E', False, False, 'Teacher', 0.21158336054026594], 
        'Sunita': [110, 'D', 'E', True, False, 'Business', 0.9834949767416051], 
        'Issur': [17, 'F', 'O', True, False, 'Service', 0.7599396397686616], 
        'Luitgard': [0, 'D', 'U', True, True, 'Unemployed', 0.8874638219100845], 
        'Rudy': [112, 'M', 'W', True, True, 'Tradesperson', 0.6035917636433216], 
        'Ioudith': [20, 'D', 'W', True, True, 'Medical', 0.24957574519928294], 
        'Helmi': [109, 'D', 'M', False, False, 'Service', 0.20239906854483214], 
        'Katerina': [108, 'M', 'W', False, True, 'Student', 0.3046268530221382], 
        'Durai': [106, 'M', 'U', True, False, 'Business', 0.32332997497778493], 
        'Euphemios': [83, 'M', 'L', True, True, 'Banker', 0.17369577419188664], 
        'Lorinda': [8, 'F', 'E', False, True, 'Retail', 0.6667783756618852], 
        'Lasse': [30, 'D', 'U', True, True, 'Business', 0.6716420300452077], 
        'Adnan': [117, 'D', 'U', True, False, 'Banker', 0.7043759366238305], 
        'Pavica': [112, 'F', 'L', False, False, 'Business', 0.5875152728319836], 
        'Adrastos': [118, 'F', 'L', False, True, 'Service', 0.0660146284846359], 
        'Kobus': [49, 'D', 'S', False, False, 'Service', 0.4738056051140088], 
        'Daniel': [115, 'D', 'L', False, True, 'Service', 0.5182765931408372], 
        'Samantha': [97, 'D', 'W', True, True, 'Medical', 0.07082409148069169], 
        'Sacagawea': [28, 'F', 'U', True, True, 'Medical', 0.29790328657890996], 
        'Ixchel': [26, 'F', 'S', False, False, 'Business', 0.22593704520870372], 
        'Nobutoshi': [31, 'M', 'W', False, True, 'Business', 0.37923896100469956], 
        'Gorou': [55, 'M', 'B', True, True, 'Banker', 0.8684653864827863], 
        'Keiko': [34, 'M', 'L', False, True, 'Student', 0.02499269016601946], 
        'Seong-Su': [1, 'M', 'M', False, True, 'Retail', 0.3214997836868769], 
        'Aya': [41, 'M', 'B', True, True, 'Teacher', 0.3378161065313626], 
        'Okan': [11, 'D', 'W', True, True, 'Banker', 0.35535128959244744], 
        'Mai': [31, 'F', 'M', False, False, 'Service', 0.7072299366468716], 
        'Chaza-el': [84, 'D', 'E', True, True, 'Teacher', 0.263795143996962], 
        'Estera': [79, 'M', 'U', True, False, 'Tradesperson', 0.09970175216521693], 
        'Dante': [82, 'M', 'L', True, False, 'Unemployed', 0.2126494288577333], 
        'Leofric': [68, 'F', 'B', True, False, 'Unemployed', 0.19591887643941486], 
        'Anabelle': [63, 'M', 'B', False, False, 'Teacher', 0.3558324357405023], 
        'Harsha': [119, 'D', 'O', False, True, 'Retail', 0.3359989642837887], 
        'Dionisia': [92, 'F', 'B', True, False, 'Doctor', 0.42704604164789706], 
        'Rajesh': [55, 'F', 'M', True, False, 'Doctor', 0.485752225148387], 
        'Scilla': [60, 'F', 'M', False, False, 'Student', 0.7294089528796434], 
        'Arsenio': [10, 'D', 'L', False, True, 'Teacher', 0.0819890866210915]}


def generate_prioritized_list(unordered_people):
    nums=[]
    for i in folks:
        nums.append(folks[i][6])
    nums.sort(reverse=True)
    for i in nums:
        names=[]
        for name in folks:
            if i in folks[name][6]:
                names.append(folks[i])
    for i in names:
        print(i)
        

print(generate_prioritized_list(folks))

I'm trying to get a list of the names ordered highest to lowest by the last value in the list each persons attributes.

CodePudding user response:

I would use the key argument to the sorted function

sorted(folks, key=lambda x: folks[x][-1])[::-1]
  • sorted pulls the keys out of the dictionary
  • key=lambda x: folks[x][-1] determines how to sort those keys
  • [::-1] reverses the list.

I get:

['Leia',
 'Sunita',
 'Luitgard',
 'Gorou',
 'Issur',
 # ... 
 'Estera',
 'Arsenio',
 'Samantha',
 'Adrastos',
 'Keiko']

CodePudding user response:

Here is one way to do it:

# Get the values of the last item in each sublist
last_values = [folks[name][-1] for name in folks]

# Sort the values in descending order
sorted_values = sorted(last_values, reverse=True)

# Create a list of names, sorted by the last value in their sublist
sorted_names = [name for value in sorted_values
                for name in folks
                if folks[name][-1] == value]

# Print the resulting list
print(sorted_names)

This code uses two nested list comprehensions to create the sorted list of names. The first list comprehension gets the values of the last item in each sublist, and the second list comprehension uses those values to create the sorted list of names.

  • Related