将字典转换为Python中的类用来创建对象的列表

2024-09-24 22:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

def calc (allWords):
    d = {}                          
    for word in allWords:           
        if word in d.keys():        
            d[word] = d[word] + 1     
        else:
            d[word] = 1
        print (word , d[word])

def retriveFile():
    FileObject = open ("a.txt")     
    data = FileObject.read()        
    abc = data.split()              
    calc(abc)                    
    return abc

retriveFile()

我正在尝试使用类将字典转换为2个对象的列表

第一个对象必须有实例变量(字符串) 第二个对象必须有字数(int)

我似乎不明白怎样才能在课堂上工作,但我是这样开始的:

class list:
    def __init__(self, words, antal):
        self.words = words
        self.antal = antal
    for words, antal in d.items()
        if words

Tags: 对象inselffordataifdefcalc