So you want to change the list's content but you don't want anyone to be
able to detect the difference? That doesn't make sense.
> I must have to use list of tuples.
> Have any way to do it using list of tuples.
Why don't you explain what you want to achieve instead of asking for a
solution that doesn't make sense?
Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
from itertools import starmap, izip, imap
list(imap(dict, starmap(izip, d)))
[{'a': 9, 's': 5}, {'u': 9, 'w': 2}]
l.sort(key=lambda x:(-x[0][0], x[1][0])) # sorting cpu_util asc and
disk_util desc
suppose i changed order that is l = [((mem_util,cpu_util),
(disk_util)), ((mem_util,cpu_util),(disk_util))]
So i have to change the sort code to l.sort(key=lambda x:(-x[0][1],
x[1][0])) # sorting cpu_util asc and disk_util desc
I want to use same (common) sort code, that must work even if i
changed tuple order.
Use namedtuples:
from collections import namedtuple
Pair = namedtuple("Pair", "pair disk_util")
A = namedtuple("A", "cpu_util mem_util")
B = namedtuple("B", "mem_util cpu_util")
# you can safely omit this step
for class_ in A, B, Pair:
del class_.__repr__
items = [((30,50), 70), ((50,20),20)]
a = [Pair(A(x, y), z) for (x, y), z in items]
b = [Pair(B(y, x), z) for (x, y), z in items]
def sort(items):
items.sort(key=lambda x: x.pair.mem_util, reverse=True)
items.sort(key=lambda x: x.pair.cpu_util)
sort(a)
print a
sort(b)
print b
Peter
If you have a sort function that works just leave it as it is and
rearrange your data before passing it to the sort function. Eg.
----------------------------------
d1 = [
((30,50),(90,)),
((30,50),(70,)),
((50,20),(20,)),
((20,20),(50,)),
((20,20),(60,)),
((20,20),(10,)),
]
d2 = [
((50,30),(90,)),
((50,30),(70,)),
((20,50),(20,)),
((20,20),(50,)),
((20,20),(60,)),
((20,20),(10,)),
]
def _sort_the_tuples(iterable):
#print list(iterable)
return sorted(iterable, key=lambda t: (-t[0][0], t[1][0]))
def sort_the_tuples(iterable, rearrange=lambda X: X):
return _sort_the_tuples(rearrange(t) for t in iterable)
assert sort_the_tuples(d1) == \
sort_the_tuples(d2, lambda X: ((X[0][1], X[0][0]), (X[1][0],)))
--------------------------------
The point of my previous response was to deal with a list of
dictionaries and sort by attribute rather than index, but whatever
works.
Regards
One thing here: Without knowing what special meaning the indices have, this
code is impossible to understand. In my opinion, that code should rather be
l.sort(key=lambda x: (-x.cpu_util, x.disk_util))
You could then save the comment, which btw may or may not describe the
actual code.
> suppose i changed order that is l = [((mem_util,cpu_util),
> (disk_util)), ((mem_util,cpu_util),(disk_util))]
> So i have to change the sort code to l.sort(key=lambda x:(-x[0][1],
> x[1][0])) # sorting cpu_util asc and disk_util desc
>
> I want to use same (common) sort code, that must work even if i
> changed tuple order.
You don't have to use a lambda function. You can as well define the sorting
order in the same place of your code where you define the order inside the
elements, that way you would only have to adjust the sorting in one place.
Otherwise, you will have to embed the order of the tuple inside the tuple or
the containing list. One approach would be to have a dictionary (which has
no order), or you use a separate type instead:
class record(object):
pass
r = record()
r.cpu_util = 12
r.disk_util = 42
l.append(r)
However, this basically all boils down to either "don't use the sorting
(dict)" or "keep records sorted", so it doesn't really answer your
question. However, I'm afraid there is no answer to your question as it
stands. Maybe I could help you better if you explained why the order
changes etc, because normally I don't deal with data that has such a
volatile meaning, I rather normalize it on input and then use it in the
same way afterwards.
[((cpu_util,mem_util),(disk_util)),((cpu_util,mem_util),(disk_util))]
i want to find the loaded machine based on cpu and mem and desk
utilization by changing this order.
I created a UI using that i can change the order of item in the tuple.
But the problem is asc and desc sorting
How about only changing the order in which the elements are displayed? In
other words, the internal data is fixed, but both the sorting of the list
and the order in which the parts of the records are displayed can be
changed.
Just my 2c