Order dictionary by key with numerical representation - python

I have this input, where each value has a range of 200:
d = {'600-800': 3, '1800-2000': 3, '1000-1200': 5, '400-600': 1, '2600-2800': 1}
And I am looking for this expected order:
{'400-600': 1, '600-800': 3, '1000-1200': 5, '1800-2000': 3, '2600-2800': 1}
Already tried something like this, but the order is just wrong:
import collections
od = collections.OrderedDict(sorted(d.items()))
print od

You can split the key into parts at '-' and use the first part as integer value to sort it. The second part is irrelevant for ordering because of the nature of your key-values (when converted to integer):
d = {'600-800': 3, '1800-2000': 3, '1000-1200': 5, '400-600': 1, '2600-2800': 1}
import collections
od = collections.OrderedDict(sorted(d.items(),key =lambda x: int(x[0].split("-")[0])))
print od
Output:
OrderedDict([('400-600', 1), ('600-800', 3), ('1000-1200', 5),
('1800-2000', 3), ('2600-2800', 1)])
Doku:
sorted(iterable,key)
Related:
How to sort a list of objects based on an attribute of the objects? for more "sort by key" examples
Are dictionaries ordered in Python 3.6+? .. which lets you omit the OrderedDict from 3.7+ on (or 3.6 CPython)

If you want to order your dictionary by the first year first (and then by the second year if needed, which is unnecessary in the given example, but feels more natural), you need to convert to integers and set a custom key:
d = {'600-800': 3, '1800-2000': 3, '1000-1200': 5, '400-600': 1, '2600-2800': 1}
sorted(d.items(), key=lambda t: tuple(map(int, t[0].split("-"))))
# [('400-600', 1),
# ('600-800', 3),
# ('1000-1200', 5),
# ('1800-2000', 3),
# ('2600-2800', 1)]
The conversion to integers is needed because e.g. "1000" < "200", but 1000 > 200. This list can be passed to OrderedDict afterwards like in your code, if needed.

Related

Get index range of the repetitive elements in the list

Suppose I have a list a = [-1,-1,-1,1,1,1,2,2,2,-1,-1,-1,1,1,1] in python what i want is if there is any built in function in python in which we pass a list and it will return which element are present at what what index ranges for example
>>> index_range(a)
{-1 :'0-2,9-11', 1:'3-5,12-14', 2:'6-8'}
I have tried to use Counter function from collection.Counter library but it only outputs the count of the element.
If there is not any built in function can you please guide me how can i achieve this in my own function not the whole code just a guideline.
You can create your custom function using itertools.groupby and collections.defaultdict to get the range of numbers in the form of list as:
from itertools import groupby
from collections import defaultdict
def index_range(my_list):
my_dict = defaultdict(list)
for i, j in groupby(enumerate(my_list), key=lambda x: x[1]):
index_range, numlist = list(zip(*j))
my_dict[numlist[0]].append((index_range[0], index_range[-1]))
return my_dict
Sample Run:
>>> index_range([-1,-1,-1,1,1,1,2,2,2,-1,-1,-1,1,1,1])
{1: [(3, 5), (12, 14)], 2: [(6, 8)], -1: [(0, 2), (9, 11)]}
In order to get the values as string in your dict, you may either modify the above function, or use the return value of the function in dictionary comprehension as:
>>> result_dict = index_range([-1,-1,-1,1,1,1,2,2,2,-1,-1,-1,1,1,1])
>>> {k: ','.join('{}:{}'.format(*i) for i in v)for k, v in result_dict.items()}
{1: '3:5,12:14', 2: '6:8', -1: '0:2,9:11'}
You can use a dict that uses list items as keys and their indexes as values:
>>> lst = [-1,-1,-1,1,1,1,2,2,2,-1,-1,-1,1,1,1]
>>> indexes = {}
>>> for index, item in enumerate(lst):
... indexes.setdefault(value, []).append(index)
>>> indexes
{1: [3, 4, 5, 12, 13, 14], 2: [6, 7, 8], -1: [0, 1, 2, 9, 10, 11]}
You could then merge the index lists into ranges if that's what you need. I can help you with that too if necessary.

Python 2.7.3: Take an item from a list and make it a key in a dictionary

I want to take items from a list and put them into a dictionary.
Say we have the following
carList = set(zip(carWordList, carWordFreq))
note: I use set to remove duplicates
So carList would produce the following:
set([('merry', 3), ('cop', 25), ('completely', 21), ('jaw', 2), ('fiddle', 1), ('bright', 4), ('593', 1), ('asked', 22), ('additionally', 4), ('pretend', 1), ('beam', 4)])
Now I want to take the words (ex. 'merry') and make them keys in a dictionary (newDic) with the numbers as values.
Also, I want to use that new dictionary to create another dictionary (otherDic) that has two keys (one being the word (ex. 'merry') and another being "Car", which will take a value later
for key in newDic:
otherDic = [key]["Car"]
I tried to do the following:
for key in carList:
otherDic = [key]["Car"]
However, I get the following error:
TypeError: list indices must be integers, not str
When you do this
otherDic = [key]["Car"]
You are just creating a list [key] that contains only key, and then you are trying to index it with "Car" instead of with an integer, which is why you get the error.
Secondly, you can't start looping into this for key in newDic: without ever creating the newDic which I don't see you doing at any point.
Anyway, this is the wrong way to approach your problem. To turn your set into a dictionary you can simply do this:
my_dict = {key: value for key, value in carList}
That's called dictionary comprehension by the way. But as #Adam Smith pointed out, that is actually equivalent to
my_dict = dict(carList)
Which is simpler... But after that, I don't quite understand what you mean by creating another dict with 2 keys per value. You can't create a dict with 2 keys, that's not how they work. You could comment here on the desired output of this new dict and I will edit my answer.
You can use dictionary comprehension:
In [61]: l = set([('merry', 3), ('cop', 25), ('completely', 21), ('jaw', 2), ('fiddle', 1), ('bright', 4), ('593', 1), ('asked', 22), ('additionally', 4), ('pretend', 1), ('beam', 4)])
In [63]: d = {k:v for k,v in l}
In [64]: d
Out[64]:
{'593': 1,
'additionally': 4,
'asked': 22,
'beam': 4,
'bright': 4,
'completely': 21,
'cop': 25,
'fiddle': 1,
'jaw': 2,
'merry': 3,
'pretend': 1}

How to make a dictionary retain its sort order?

def positive(self):
total = {}
final = {}
for word in envir:
for i in self.lst:
if word in i:
if word in total:
total[word] += 1
else:
total[word] = 1
final = sorted(total, reverse = True)
return total
This returns
{'climate': 10, 'ecosystem': 1, 'energy': 6, 'human': 1, 'world': 2, 'renewable': 2, 'native': 2}
I want to get this dictionary back to a dictionary that is in order. How do you I sort it and return a dictionary?
An ordered dictionary would get you what you need
from collections import OrderedDict
If you want to order your items in lexicographic order, then do the following
d1 = {'climate': 10, 'ecosystem': 1, 'energy': 6, 'human': 1, 'world': 2, 'renewable': 2, 'native': 2}
od = OrderedDict(sorted(d1.items(), key=lambda t: t[0]))
Contents of od:
OrderedDict([('climate', 10),
('ecosystem', 1),
('energy', 6),
('human', 1),
('native', 2),
('renewable', 2),
('world', 2)])
If you want to specify exactly which order you want your dictionary, then store them as tuples and store them in that order.
t1 = [('climate',10), ('ecosystem', 1), ('energy',6), ('human', 1), ('world', 2), ('renewable', 2), ('native', 2)]
od = OrderedDict()
for (key, value) in t1:
od[key] = value
od is now
OrderedDict([('climate', 10),
('ecosystem', 1),
('energy', 6),
('human', 1),
('world', 2),
('renewable', 2),
('native', 2)])
In use, it is just like a normal dictionary, but with its internal contents' order specified.
Dictionaries in Python have no explicit order (except in 3.6). There is no property of 'order' in a hash table. To preserve order in Python, use a list of tuples:
unordered = (('climate', 10,), ('ecosystem', 1)) # etc
Calling sorted(unordered) on the above will give it back with the 'key' being the first item in each individual tuple. You do not need to provide any other arguments to sorted() in this case.
To iterate, use for x, y in z: where z is the list.

How to change a list to be used as a dictionary key

I have a list of lists with 4 elements in each of them.
LoL=[[1,1,1,1],[4,2,3,[1,3]],[4,5,3,[0,4]]]
The 4th elements can be a list of two parts like [0,4] in [4,5,3,[0,4]].
I need to use its elements as keys for a dictionary,
Pseudo code:
dic = { [1,1,1,1]:'a',[4,2,3,[1,3]]:'b',[4,5,3,[0,4]]:'c' }
so tried to change them to tuples.
It works for simple lists (like [1,1,1,1]), but for the ones containing another list (like [4,5,3,[0,4]]) it raises an error:
dic[tuple([1,1,1,1])]=['bla','blah']
print dic
{(1, 1, 1, 1): ['bla', 'blah']}
dic[tuple([4, 2, 3, [1, 3]])]=['blablah']
TypeError: unhashable type: 'list'
I need to reuse the keys as lists later. So trying to change elements of LoL to strings (e.g. using repr()) is not an option!
Edit:
I know why lists cannot be used as dictionary keys. Here they are not changed while in the dic. I just need some way to pass them to another module to extract them.
Just convert your nested lists to nested tuples. Here's a quick demo. It's not perfect, but it works.
#! /usr/bin/env python
LoL = [[1,1,1,1],[4,2,3,[1,3]],[4,5,3,[0,4]]]
def nested_list_to_tuple(nlist):
return tuple([nested_list_to_tuple(i) if isinstance(i, list) else i for i in nlist])
ToT = nested_list_to_tuple(LoL)
print ToT
output
((1, 1, 1, 1), (4, 2, 3, (1, 3)), (4, 5, 3, (0, 4)))
Just use tuples:
a = {}
a[(4, 2, 3, (1, 3))] = ['blablah']
print(a)
Output:
{(4, 2, 3, (1, 3)): ['blablah']}

Python OrderedDict ordered by date

I am trying to use an OrderedDict (Raymond Hettingers version for pre2.7 Python) where my keys are dates. However it does not order them correctly, I imagine it may be ordering based on the ID.
Does anyone have any suggestions of how this could be done?
In [1]: from collections import OrderedDict
In [2]: import operator
In [3]: from datetime import date
In [4]: d = {date(2012, 1, 1): 123, date(2010,2,5): 542, date(2011,3,3):76 }
In [5]: d # Good old dict
Out[5]: #it seems sorted, but it isn't guaranteed to be that way.
{datetime.date(2010, 2, 5): 542,
datetime.date(2011, 3, 3): 76,
datetime.date(2012, 1, 1): 123}
In [6]: o = OrderedDict(sorted(d.items(), key=operator.itemgetter(0)))
In [7]: o #Now it is ordered(and sorted, because we give it by sorted order.).
Out[7]: OrderedDict([(datetime.date(2010, 2, 5), 542), (datetime.date(2011, 3, 3), 76), (datetime.date(2012, 1, 1), 123)])
OrderedDict, according to its docstring, is a kind of dict that remembers insertion order.
Thus, you need to manually insert the key/value pairs in the correct order.
# assuming unordered_dict is a dict that contains your data
ordered_dict = OrderedDict()
for key, value in sorted(unordered_dict.iteritems(), key=lambda t: t[0]):
ordered_dict[key] = value
edit: See utdemir's answer for a better example. Using operator.itemgetter gives you better performance (60% faster, I use the benchmark code below) and it's a better coding style. And you can apply OrderedDict directly to sorted(...).
a = (1, 2)
empty__func = 0
def empty():
for i in xrange(N_RUNS):
empty__func
lambda_func = lambda t: t[0]
def using_lambda():
for i in xrange(N_RUNS):
lambda_func(a)
getter_func = itemgetter(0)
def using_getter():
for i in xrange(N_RUNS):
getter_func(a)

Categories

Resources