我想知道为什么我会收到类型错误:类型为"int"的参数不可迭代



我正试图根据值列表中的最后一个数字制作名称列表。新列表将根据最高数字到最低数字排序,但它是一个名称列表。

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))

我正试图获得按列表中每个人属性的最后一个值从高到低排序的名称列表。

我将key参数用于sorted函数

sorted(folks, key=lambda x: folks[x][-1])[::-1]
  • sorted从字典中取出键
  • key=lambda x: folks[x][-1]决定如何排序这些键
  • [::-1]将列表反转。

:

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

相关内容

  • 没有找到相关文章

最新更新