Jupyter Notebook用fuzzyset 0.0.9报错,求字典值转列表方法
Hey there! Let's work through this problem together. It sounds like you're hitting an error with fuzzyset 0.0.9 because it doesn't play nicely with dictionaries, and you need to convert those dictionary entries into list format instead. Here's how to adjust your code:
Option 1: Directly append entries as [URI, Label] lists
This is the simplest fix—instead of adding a dictionary to agrovocSimple, just add a list containing your URI and label values:
agrovocSimple = [] with open('agrovocLabels.json') as data_file: agrovoc = json.load(data_file) results = agrovoc["results"]["bindings"] for entry in results: uri = entry["uri"]["value"] label = entry["label"]["value"] # Swap the dictionary for a list agrovocSimple.append([uri, label])
Option 2: Convert dictionary values to a list first
If you still want to construct the original dictionary (maybe for other uses) before converting it to a list, you can use list(entry_dict.values()) to extract its values into a list:
agrovocSimple = [] with open('agrovocLabels.json') as data_file: agrovoc = json.load(data_file) results = agrovoc["results"]["bindings"] for entry in results: uri = entry["uri"]["value"] label = entry["label"]["value"] entry_dict = {"uri": uri , "name": label} # Convert the dictionary's values to a list and append agrovocSimple.append(list(entry_dict.values()))
Option 3: If fuzzyset only needs a flat list of labels
If the fuzzyset operation only requires the label strings (not the URIs), you can simplify even further by just collecting the labels directly:
agrovocSimple = [] with open('agrovocLabels.json') as data_file: agrovoc = json.load(data_file) results = agrovoc["results"]["bindings"] for entry in results: label = entry["label"]["value"] agrovocSimple.append(label)
The error with fuzzyset 0.0.9 almost certainly comes from the library expecting simple list elements (like strings or basic value lists) instead of dictionary objects. Switching to one of these formats should resolve that issue.
内容的提问来源于stack exchange,提问作者sparklingstar

