如何使用Bubb在Python中对字典进行排序

2024-07-03 05:49:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个当前的算法来排序我的字典,但它不会工作,如果你比较4个或更多的元素。我做错什么了?在

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(len(profiles)-1-x):
        v1=database[j][0]
        v2=database[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])

Tags: incomcityforrangeprofilesdatabasegmail
2条回答

我对你的代码做了一些修改 (一)

v1=database[int(profiles[j])][0]
v2=database[int(profiles[j-1])][0]

而不是

^{pr2}$

因为j有profiles的索引,但是我们需要的是profiles中的实际值,这是字典的关键。在

然后使用bubble j和j+1,只考虑从0到len(profiles)-1的值。这个-1会使你错过最后一个值。所以我考虑了从1到len(轮廓)的值,并使用了bubble j和j-1

这是完整的代码

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

希望这对你有用

输出如下

['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

这两条线是错的。您使用的是j中的j索引,而不是存储在这些索引中的配置文件密钥。它停止工作,因为无论交换,总是比较相同的元素。在

v1=database[j][0]
v2=database[j+1][0]

它们应该是这样的:

^{pr2}$

这是跑第一关的结果。顶部的v1/v2是错误的值,而底部的v1/v2是正确的值。在

x = 0                                                                    
print(['0', '1', '2', '3', '4'])                                         
j = 0        | j = 1             | j = 2             | j = 3             
v1 = 'Ninna' | v1 = 'Yela'       | v1 = 'Denise'     | v1 = 'Alia'       
v2 = 'Yela'  | v2 = 'Denise'     | v2 = 'Alia'       | v2 = 'Keeno'      
             |                   |                   |                   
v1 = 'Ninna' | v1 = 'Yela'       | v1 = 'Yela'       | v1 = 'Yela'       
v2 = 'Yela'  | v2 = 'Denise'     | v2 = 'Alia'       | v2 = 'Keeno'      
             |                   |                   |                   
             |                   |                   |                   
             | sorted = False    | sorted = False    | sorted = False    
             | temp = '2'        | temp = '3'        | temp = '4'        
             | profiles[2] = '1' | profiles[3] = '1' | profiles[4] = '1' 
             | profiles[1] = '2' | profiles[2] = '3' | profiles[3] = '4' 

另外,这是在Python中交换的方式:

profiles[j], profiles[j+1] = profiles[j+1], profiles[j]

这里有一个更好的算法:

swapped = False
while not swapped:
    swapped = True
    print(profiles)
    for j in range(len(profiles) - 1):
        v1 = database[int(profiles[j])][0]
        v2 = database[int(profiles[j+1])][0]
        if v1 > v2:
            swapped = False
            profiles[j], profiles[j+1] = profiles[j+1], profiles[j]

下面是如何使用标准方法来实现:

profiles = list(sorted(profiles, key=lambda x: database[int(x)]))

相关问题 更多 >