![](/img/trans.png)
[英]How to make undersampling to have 25% of imput fo category 0 and does not changes in category 1 in Python?
[英]How to make a category inside a category with python?
我想构建一个应用程序,将您的照片分类管理,您只需首先放置它们的路径,然后使用关键字访问这些照片。 当我必须创建一个子类别时,我被卡住了。 我试过了
path_and_image = {user_answer: new_list, new_list: path_LiL}
其中 user_answer 是主要类别,new_list 是子类别/类别,path_LiL 是它们的路径。 它给了我回溯TypeError: unhashable type: 'list' 。 我不知道如何解决这个问题,我希望有人能解决我的问题。
我也尝试了其他的东西,比如来自 stackoverflow 的 function:
[lista_noua],
[lista_noua] ,
[path_LiL]]
def make_trie(path_and_image):
root = dict()
for i in path_and_image:
current_dict = root
for letter in i:
current_dict = current_dict.setdefault(letter, {})
return root
trie = make_trie(path_and_image)
print(trie)
它仍然给出同样的错误。
您尝试将列表作为字典中的键。 特别是字典第二个元素中的new_list
。 那是你的错误。
一个很好的解决方案:
path_and_image = {user_answer: new_list, 'paths': path_LiL}
您注意到'paths'
是一个字符串。
为什么不使用嵌套字典? (列表不能作为键,因为它不是可散列的,即它的值可以随时间变化,只有字符串、元组等不可变对象可以用作键)
考虑一下——
Category Colors
| Red
| Pink
| Blue
Category Material
| Matte
| Polish
| Plain
Category Other
| ...
colors = ['Red', 'Pink', 'Blue']
material = ['Matte', 'Polish', 'Plain']
categories = {'colors': colors, 'material': material}
即使您有更多级别-您也应该像 go 一样...
Category Colors
| Red
| Dark Red
| Light Red
| Pink
| Dark Pink
| Light and Dark Pink
| Blue
Category Material
| Matte
| Polish
| Plain
Category Other
| ...
colors = {'Red': ['Dark Red', 'Light Red'],
'Pink': ['Dark Pink', 'Dark & Light Pink'],
'Blue': ['Blue']}
material = {'Matte': ['Matte'],
'Polish': ['Polish'],
'Plain': ['Plain']}
categories = {'colors': colors, 'material': material}
当然,它值得更好的回答——但现在你可以开始使用它了。
但是请记住,应该有一致性,如果您在一个类别中有多个级别,那么其他类别也应该具有相同的数据结构,否则在迭代它们时会遇到问题。
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.