简体   繁体   中英

How I can change a list using 'item' to dictionary in python?

I want to run a code from Github. In the following piece of the code:

def read_table(table_file):
    
        table = dict()
        
        with open(table_file, 'rb') as handle:
            while True:
                   try:
                           table = pickle.load(handle)
                   except EOFError:
                           break
        
        f_set=set()
        
        for k,v in list.items():
            for feature in v[DATA]:
                f_set.add(feature)
               
        return table , f_set

I got this error: AttributeError: 'list' object has no attribute 'items' How can I change the list to dict in this code? Can anyone please help me?

I try to change the list using filter or dir() function but I got new errors.

You're trying to iterate over list . list is a builtin type in Python. What you probably meant to do is iterate over table , which is a dictionary and does have a .items() method.

Here's the revised snippet:

import pickle

def read_table(table_file):
    table = dict()
    with open(table_file, 'rb') as handle:
        while True:
            try:
                table = pickle.load(handle)
            except EOFError:
                break
    f_set = set()

    for k, v in table.items():
        for feature in v[DATA]:
            f_set.add(feature)

    return table, f_set

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM