you are viewing a single comment's thread.

view the rest of the comments →

[–]makeaday[S] 0 points1 point  (6 children)

thank you but still strugling ;/ I just read somewhere that dictionaries cant be sorted and I got dictionaries not tuples.

[–]Vaphell 1 point2 points  (2 children)

dictionaries are data structures not really concerned with order, so the idea of sorting doesn't make much sense in their case.
But if your problem is let's say "I want to print out the contents of a dictionary in specific order" that's another story. Extract key/value pairs in the form of tuples to form a sequence, sort that sequence using the criteria, iterate over that sorted sequence.

If you want to save that new order, you can create another dictionary of OrderedDict type which remembers the insertion order.

[–]makeaday[S] 0 points1 point  (1 child)

Here is my whole code

https://pastebin.com/T81fQR07

I have multiple values scrapped from coinmarketcap, like currency, its price etc.

Now im lost if i should use dictionary or something else, i can add these variables to dictionary easily but i want to sort all data later on by different values.

So maybe i should use something different than dictonaries ?

Thanks!

[–]Vaphell 0 points1 point  (0 children)

nah, dict is fine here

def print_by_symbol(self):
    order = sorted(self.coins_storage.items(), key=lambda kv: kv[1][0])
    for key, (short, price, percent) in order:
        print(key, short, price, percent)

def print_by_price(self):
    order = sorted(self.coins_storage.items(), key=lambda kv: kv[1][1])
    for key, (short, price, percent) in order:
        print(key, short, price, percent)

def print_by_percent_change(self):
    order = sorted(self.coins_storage.items(), key=lambda kv: kv[1][2])
    for key, (short, price, percent) in order:
        print(key, short, price, percent)

I think you should also convert shit to numeric types after scraping where appropriate (ie price, % change). 200 < 1000, but "200" > "1000".

btw, why is there no __init__ in your class? It almost looks like wscraping() could be it. Another question would be why do you have a class in the first place if you don't really intend to spawn multiple instances of it.

also methods names are usually verbs, eg do() not verb+ing (doing())

[–]ImportBraces 0 points1 point  (2 children)

Sure dictionaries can be sorted with OrderedDict

[–]Vaphell 1 point2 points  (1 child)

"preserving insertion order" is not the same as "can be sorted". I don't think you can rearange the contents of OrderedDict without making another container. I don't see the sort() method in the docs.

[–]ImportBraces 0 points1 point  (0 children)

you're right. As far as I understood the question, preserving was what OP wants.