FAQ Database Discussion Community


How do I remove the sorting that comes when I use unstack in R?

r,list,ordereddictionary
I have created a list in R, along the lines of: alist <- as.list(unstack(DF, DF[,1]~DF[,2])) I am doing so using unstack, which I've learned applies sorting -- meaning the keys are sorted alphnumerically. Unfortunately, later, I need to access the values by position in an lapply. I need to access...

Sorting DefaultDict() using key values in Python3.x

python,sorting,python-3.x,ordereddictionary
I have a dict which contains values like DefaultListOrderedDict([('29.970', [1, 0, 0, 0, 0]), ('100.000', [0, 1, 0, 0, 1]), ('200.000', [0, 0, 1, 0, 0]), ('60.000', [0, 0, 0, 1, 0]), ('0.750', [0, 0, 1, 0, 0]), ('25.000', [0, 0, 0, 0, 1]), ('48.000', [0, 0, 1, 0, 0])])...

Reformatting Dictionary output in python

python,csv,formatting,ordereddictionary
I have an OrderedDict and I've exported it to a csv but I want it to be formatted differently. My code for reading, sorting, and making the dictionary: from collections import defaultdict, OrderedDict counts = defaultdict(lambda: {"User": 0, "Equipment": 0, "Neither": 0}) with open('sorterexample.csv', 'rb') as fh: reader = csv.reader(fh,...

Creating a restaurant/pizza menu in python, with a good layout

python,list,dictionary,ordereddictionary
I am new to python and would like to create a Pizza ordering system using python, that looks something like this. 1 Hawaiian $7.50 2 Champagne Ham & Cheese $7.50 3 Beef & Onion $7.50 4 Pepperoni $7.50 5 Simply Cheese $7.50 6 Bacon & Mushroom $7.50 7 Italiano $7.50...

How to build a nested ordered dict from a csv?

python,csv,dictionary,nested,ordereddictionary
How can I get a nested dictionary, where both the keys and the subkeys are precisely in the same order as in the csv file? I tried import csv from collections import OrderedDict filename = "test.csv" aDict = OrderedDict() with open(filename, 'r') as f: csvReader = csv.DictReader(f) for row in...

python ordered dict issue

python,dictionary,ordereddictionary
If I have a CSV file that has a dictionary value for each line (with columns being ["Location"], ["MovieDate"], ["Formatted_Address"], ["Lat"], ["Lng"]), I have been told to use OrderDict if I want to group by Location and append on all the MovieDate values that share the same Location value. ex...

Equivalent of list.index() for an OrderedDict? [duplicate]

python,dictionary,ordereddictionary
This question already has an answer here: getting the key index in a Python OrderedDict? 3 answers Given that by its very nature, the items in an OrderedDict are ordered, one might expect it to have a function similar to the index function of lists. It does not. What...

OrderedDict reinitialisation: order is lost

python,ordereddictionary
My class is inherited from OrderedDict and I want to to reinitialise the dictionary. But simplified code below changes only values of keys - order of elements remains the same: from collections import OrderedDict class Example(OrderedDict): def __init__(self,d): OrderedDict.__init__(self,d) #something that should be done only once - at instance creation...

Slicing a Python OrderedDict

python-2.7,slice,ordereddictionary
In my code I frequently need to take a subset range of keys+values from a Python OrderedDict (from collections package). Slicing doesn't work (throws TypeError: unhashable type) and the alternative, iterating, is cumbersome: from collections import OrderedDict o = OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4)]) # want to...