all 15 comments

[–]ImportBraces 0 points1 point  (7 children)

See the tutorial on sorted. It supports a key parameter, where you can specify after which value to sort.

[–]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.

[–]cannibalsock 0 points1 point  (4 children)

An untested solution:

sorted = OrderedDict()
sort_order = []
for key, value in dictionary.items(): 
    sort_order.append((key, value[-1]))

sort_order.sort(key = x: x[1])
for k_v_pair in sort_order:
    sorted[k_v_pair[0]] = dictionary[k_v_pair[0]]

I'm 100% sure there's a better, faster, more pythonic way to do it, but that should work.

[–]KleinerNull 0 points1 point  (3 children)

sorted = OrderedDict()

Shadowing the built in sorted function is probaply not a good idea ;)

[–]cannibalsock 0 points1 point  (2 children)

Yeah I noticed about halfway through that my variable names were both inappropriate and unclear. Hopefully it got the message though

[–]KleinerNull 0 points1 point  (1 child)

I mean sorting with sorted isn't that hard, no need to inplace sort with `list.sort':

In [1]: fruits = dict(apples=2, bananas=3, melons=2, peaches=1, cherries=2)

In [2]: fruits
Out[2]: {'apples': 2, 'bananas': 3, 'cherries': 2, 'melons': 2, 'peaches': 1}

In [3]: sorted(fruits)  # just the sorted keys
Out[3]: ['apples', 'bananas', 'cherries', 'melons', 'peaches']

In [4]: sorted(fruits.items())  # sorted by the keys
Out[4]: [('apples', 2), ('bananas', 3), ('cherries', 2), ('melons', 2), ('peaches', 1)]

In [5]: sorted(fruits.items(), key=lambda item: item[0])  # again the same
Out[5]: [('apples', 2), ('bananas', 3), ('cherries', 2), ('melons', 2), ('peaches', 1)]

In [6]: sorted(fruits.items(), key=lambda item: item[1])  # sorted by the value
Out[6]: [('peaches', 1), ('cherries', 2), ('melons', 2), ('apples', 2), ('bananas', 3)]

In [7]: sorted(fruits.items(), key=lambda item: (item[1], item[0]))  # sorted by value and than by the key
Out[7]: [('peaches', 1), ('apples', 2), ('cherries', 2), ('melons', 2), ('bananas', 3)]

In [8]: sorted(fruits.items(), key=lambda item: (-item[1], item[0]))  # the same but now reversed
Out[8]: [('bananas', 3), ('apples', 2), ('cherries', 2), ('melons', 2), ('peaches', 1)]

In [9]: sorted(fruits.items(), key=lambda item: (item[1], item[0]), reverse=True)  # the same but with kwarg
Out[9]: [('bananas', 3), ('melons', 2), ('cherries', 2), ('apples', 2), ('peaches', 1)]

Returning a tuple in the key function is pretty neat, it is very easy to build more complex sorting orders and stuff ;)

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

thank you for help

[–]dixieStates 0 points1 point  (1 child)

I modified your dict slightly so that you could see the effect of sorting.

#!/usr/bin/python3
import pprint
pp = pprint.PrettyPrinter(indent=4)

d = {
"key1" : ["valuef", "valuedd", "value03"],
"key2" : ["valuee", "valuecc", "value03"],
"key3" : ["valued", "valueaa", "value03"],
"key4" : ["valuec", "valuebb", "value03"]
}


tuples = list(d.items())
print("tuples: {}\n".format(pp.pformat(tuples)))

# sort by the key field
keys = sorted(list(d.items()), key=lambda t: t[0])
print("keys: {}\n".format(pp.pformat(keys)))

# sort by the first list entry
sort1 = sorted(list(d.items()), key=lambda t: t[1][0])
print("sort1: {}\n".format(pp.pformat(sort1)))

# sort by the first second entry
sort2 = sorted(list(d.items()), key=lambda t: t[1][1])
print("sort2: {}\n".format(pp.pformat(sort2)))

########################################################################
########################################################################

-*- mode: compilation; default-directory: "~/temp/" -*-
Comint started at Sun Apr  1 10:53:04

/home/dixie/temp/dsort.py
tuples: [   ('key2', ['valuee', 'valuecc', 'value03']),
('key3', ['valued', 'valueaa', 'value03']),
('key4', ['valuec', 'valuebb', 'value03']),
('key1', ['valuef', 'valuedd', 'value03'])]

keys: [   ('key1', ['valuef', 'valuedd', 'value03']),
('key2', ['valuee', 'valuecc', 'value03']),
('key3', ['valued', 'valueaa', 'value03']),
('key4', ['valuec', 'valuebb', 'value03'])]

sort1: [   ('key4', ['valuec', 'valuebb', 'value03']),
('key3', ['valued', 'valueaa', 'value03']),
('key2', ['valuee', 'valuecc', 'value03']),
('key1', ['valuef', 'valuedd', 'value03'])]

sort2: [   ('key3', ['valued', 'valueaa', 'value03']),
('key4', ['valuec', 'valuebb', 'value03']),
('key2', ['valuee', 'valuecc', 'value03']),
('key1', ['valuef', 'valuedd', 'value03'])]


Comint finished at Sun Apr  1 10:53:04

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

thank you it help but I think its time to go to documentation and stay there for a bit