<p>您的代码有很多问题,但关键的一点是,您在创建每一行时对它进行排序,而不是列表列表。你知道吗</p>
<p>此外:</p>
<ul>
<li><p><code>importantparts()</code>不返回任何内容(因此<code>salarysort()</code>不返回任何内容)。</p></li>
<li><p>您需要将<code>Salary</code>字段强制转换为<code>int</code>,以便按值正确排序(它们的字段宽度不同,因此字母数字排序将不正确)。</p></li>
<li><p>最后,您不需要使用<code>for i in range(1, len(thing)):</code>,您可以直接在<code>thing</code>上迭代,使用<a href="https://stackoverflow.com/questions/509211/explain-pythons-slice-notation">slice</a>删除第一个元素<sup>1</sup>。</p></li>
</ul>
<p><sup>1</sup><sub>请注意,最后一个本身并不是错误的,但直接迭代iterable被认为更“Pythonic”。</sub></p>
<p/>
<pre><code>def salary_sort(thing):
def importantparts(thing):
unsortedlist = []
for item in thing[1:]:
a=item.split(':')
unsortedlist.append([a[1],a[0],int(a[8])])
print unsortedlist
sortedlist = sorted(unsortedlist, key = lambda item: item[2], reverse=True)
return (sortedlist)
return importantparts(thing)
employee_data = ["FName LName Tel Address City State Zip Birthdate Salary",
"Arthur:Putie:923-835-8745:23 Wimp Lane:Kensington:DL:38758:8/31/1969:126000",
"Barbara:Kertz:385-573-8326:832 Ponce Drive:Gary:IN:83756:12/1/1946:268500",
"Betty:Boop:245-836-8357:635 Cutesy Lane:Hollywood:CA:91464:6/23/1923:14500"]
print salary_sort(employee_data)
</code></pre>
<p>输出:</p>
<pre><code>[['Kertz', 'Barbara', 268500], ['Putie', 'Arthur', 126000], ['Boop', 'Betty', 14500]]
</code></pre>