<p>我对你的代码做了一些修改
(一)</p>
<pre><code>v1=database[int(profiles[j])][0]
v2=database[int(profiles[j-1])][0]
</code></pre>
<p>而不是</p>
^{pr2}$
<p>因为j有profiles的索引,但是我们需要的是profiles中的实际值,这是字典的关键。在</p>
<p>然后使用bubble j和j+1,只考虑从0到len(profiles)-1的值。这个-1会使你错过最后一个值。所以我考虑了从1到len(轮廓)的值,并使用了bubble j和j-1</p>
<p>这是完整的代码</p>
<pre><code>database={
0:['Ninna','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999'],
1:['Yela','Gregorio','201506070','09984548540','UP Diliman','yelagregorio@gmail.com','19','04/18/1999'],
2:['Denise','Gregorio','201506070','09984548540','UP Diliman','yelagregorio@gmail.com','19','04/18/1999'],
3:['Alia','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999'],
4:['Keeno','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999']
}
profiles=['0','1','2','3','4']
for x in range(len(profiles)):
print(profiles)
for j in range(1,len(profiles)-x):
v1=database[int(profiles[j])][0]
v2=database[int(profiles[j-1])][0]
if v1>v2:
sorted=False
temp=profiles[j-1]
profiles[j-1]=profiles[j]
profiles[j]=temp
for x in profiles:
print(database[int(x)][0],",",database[int(x)][1])
</code></pre>
<p>希望这对你有用</p>
<p>输出如下</p>
<pre><code>['0', '1', '2', '3', '4']
['1', '0', '2', '4', '3']
['1', '0', '4', '2', '3']
['1', '0', '4', '2', '3']
['1', '0', '4', '2', '3']
Yela , Gregorio
Ninna , Layug
Keeno , Layug
Denise , Gregorio
Alia , Layug
</code></pre>