Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 赋值时函数中的变量错误_Python_Sorting_Dictionary - Fatal编程技术网

Python 赋值时函数中的变量错误

Python 赋值时函数中的变量错误,python,sorting,dictionary,Python,Sorting,Dictionary,编写Python脚本以按值对字典进行排序(升序和降序) def sort_dictionary_ascending(dict): flag = True list = [] while(len(dict)!=1): flag = True for x,y in dict.items(): if flag: min = x flag = False

编写Python脚本以按值对字典进行排序(升序和降序)

def sort_dictionary_ascending(dict):
    flag = True
    list = []
    while(len(dict)!=1):
        flag = True
        for x,y in dict.items():
            if flag:
                min = x
                flag = False
            elif min>x:
                min =x;
        list.append(min)
        dict.pop(min)
    min,value= dict.popitem()
    list.append(min)
    print(list)
def sort_dictionary_descending(dict):
    flag = True
    list = []
    while(len(dict)!=1):
        flag = True
        for x,y in dict.items():
            if flag:
                max = x
                flag = False
            elif max < x:
                max = x
        list.append(max)
        dict.pop(max)
    max,value= dict.popitem()
    list.append(max)
    print(list)
d = {1: 1, 3: 3, 4: 4, 2: 2, 5: 5}
sort_dictionary_descending(d)
sort_dictionary_ascending(d)
def sort\u dictionary\u升序(dict):
flag=True
列表=[]
而(len(dict)!=1):
flag=True
对于dict.items()中的x,y:
如果标志:
最小=x
flag=False
elif最小值>x:
min=x;
list.append(最小值)
dict.pop(分钟)
最小值,值=dict.popitem()
list.append(最小值)
打印(列表)
定义排序字典降序(dict):
flag=True
列表=[]
而(len(dict)!=1):
flag=True
对于dict.items()中的x,y:
如果标志:
最大值=x
flag=False
elif max
错误是:

/home/admin2/Desktop/two/venv/bin/python /home/admin2/Desktop/two/sort_dictionary.py
Traceback (most recent call last):
  File "/home/admin2/Desktop/two/sort_dictionary.py", line 45, in <module>
    sort_dictionary_ascending(d)
  File "/home/admin2/Desktop/two/sort_dictionary.py", line 17, in sort_dictionary_ascending
    list.append(min)
UnboundLocalError: local variable 'min' referenced before assignment
[5, 4, 3, 2, 1]
/home/admin2/Desktop/two/venv/bin/python/home/admin2/Desktop/two/sort\u dictionary.py
回溯(最近一次呼叫最后一次):
文件“/home/admin2/Desktop/two/sort_dictionary.py”,第45行,在
排序\u字典\u升序(d)
文件“/home/admin2/Desktop/two/sort\u dictionary.py”,第17行,在sort\u dictionary\u中
list.append(最小值)
UnboundLocalError:赋值前引用了局部变量“min”
[5, 4, 3, 2, 1]

在for循环中,首先对dict.key()中的x使用
而不是dict.items()中的x、y使用

因为
y
变量未在整个循环中使用。第二,我不明白为什么你要使用一个键等于值的dict,列表更合适

有更简单的方法对字典进行排序。当你第一次调用
sort\u dictionary\u descending(d)
时,你清空了原始dict
d
,所以当你调用
sort\u dictionary\u升序(d)
while(len(dict)时!=1):
从未执行过,而且从未定义过
min
。请注意,您对变量名的选择非常糟糕:您不应该使用Python内置名作为变量名,这将影响原始函数(dict、list、min、max…)不要使用
dict
作为变量或参数名,因为它是内置类型。我必须在不使用内置函数的情况下对字典进行排序