python - 类型错误:不可散列类型:'slice' [python] [dictionaries]
问题描述
os.chdir("Güvenlik_Hesaplar")
files = ''
dictionary = {}
ant = os.listdir()
dict_number = 1
i = 0
while i < len(ant): # If the variable 'i' is less than the length of my files
dictionary["{}" : ant[i].format(dict_number)] #e.g = '1': 'myfile.txt'
dict_number += 1
i += 1
错误:
File "C:\Users\Barış\Desktop\Python\prg.py", line 20, in passwordrequest
dictionary["{}" : ant[i].format(dict_number)]
TypeError: unhashable type: 'slice'
你能帮我解决这个问题吗?我正在使用 Windows x64
解决方案
这是一个更好的方法:
import os
os.chdir("Güvenlik_Hesaplar")
dictionary = {str(k + 1): filename for k, filename in enumerate(os.listdir())}
推荐阅读
- css - Angular,Clarity Design ..如何停止影响外部数据网格内选项卡内的内部数据网格?
- r - log10 逆变换:-10*log10(x)
- python - 如何使用 Python 中的 open 函数读取文件中存在的所有链接?
- html - 将图像的一部分定位为不可见
- javascript - 如何在 ReteJs 中发出自定义事件?
- java - Poll() 的 Java PriorityQueue 意外顺序
- flutter - 如何限制flutter web中的chrome大小?
- reactjs - 如何在 React js 上获取 DateRangePicker (syncfusion) 的值
- javascript - javascript:推断一个数字数组
- java - 使用 JLabel 在 JFrame 中打印的问题