Add Python dictionary values and output them as strings

Asked 2 years ago, Updated 2 years ago, 86 views

 class HashMap:
    def __init__(self):
        self.vals = {}
    def insert(self, key, val):
        self.vals[key]= val
    def __str__(self):
        result = ''
        for i in self.vals.items():
            result = result + str(i) + ','
        return '{' + result[:-1] + '}'

hashMap = HashMap()
hashMap.insert("grade", 1)
hashMap.insert("number", 60171234)
hashMap.insert("name", "Gildong Hong")
print(hashMap)

Run the above code and {"grade" : 1, "number" : 60171234, 'name' : 'Gildong Hong'} I want to make sure that these results are printed as strings, but how do I fix them? Also, we need to create a scoreSort function and use sorted to sort the value value of the dictionary, which method should we use?

I'd appreciate it if you could answer!

python dictionary

2022-09-20 16:02

1 Answers

Is there a need to make a class??

a = {}
a['grade'] = 1
a['number'] = 60171234
a['name'] = 'Gildong Hong'
print(a)

>> {'grade': 1, 'number': 60171234, 'name': 'Gildong Hong'}


2022-09-20 16:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.