Naming dictionaries arbitrarily - list

I have list=[gender,employment type] and i wanted to create a dictionary named gender and another dictionary named employment type. Can i name my dictionaries something as follows:
> list[0] = {key1: value}
> list[1] = {key2: value}
I wanted to name my dictionary arbitrarily depending on certain input.Is it possible to declare a dictionary using a string value from a list?

You may want to look into a combination of map and enumerate. I've linked to the 2.x docs because of your tag, but I believe the functions are almost identical in 3.x.
Using a map:
>>> list=["gender", "employment type"]
>>> mapped=map(lambda x: {"key":x})
[{'key': 'gender'}, {'key': 'employment type'}]
with enumerate:
>>> list=["gender", "employment type"]
>>> map(lambda (i, x): {"key"+str(i):x}, enumerate(list))
[{'key0': 'gender'}, {'key1': 'employment type'}]
Given more deeply-nested structures — something like list=[[gender], [employment type]] — you can define a more intricate function as your mapper.
Alternatively, if you are looking at an array of [gender, employment type] tuples (something that more closely resembles [(gender, employment type)]) — you may want to look into "unzipping" the data. See the SO question: A Transpose/Unzip Function in Python (inverse of zip).

Related

Gremlin: how do I find vertices whose properties *contain* a certain values

Imagine I have vertices with properties whose values are lists, e.g:
g.addV('v').property('prop',['a','b','c'])
How do I find cases where prop contains a certain value?
This seemed the obvious thing to try:
g.V().has(P.within('prop'),'a')
But it doesn't work:
gremlin_python.driver.protocol.GremlinServerError: 599: Could not locate method: DefaultGraphTraversal.has([within([a]), test])
If you use the VertexProperty list cardinality (see multi-properties in the docs), you can accomplish it like this:
>>> g.addV('v').property(list_, 'prop', 'a').property(list_, 'prop', 'b').property(list_, 'prop', 'c').next()
v[0]
>>> g.V().has('prop', within('a')).toList()
[v[0]]
Note that list_ is coming from an enum via from gremlin_python.process.traversal import Cardinality.
If it's a real list (not a multi-valued property), then you'll have to unfold the value:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('v').property('prop',['a','b','c'])
==>v[0]
gremlin> g.V().filter(values('prop').unfold().is('a'))
==>v[0]
// or
gremlin> g.V().has('prop', unfold().is('a'))
==>v[0]
Note that this filter requires a full scan over all vertices as individual list entries cannot be indexed. Hence you should take a look at Jason's answer as multi-properties are usually a much better choice.

How to maintain order of insertion in dictionary in python? [duplicate]

I have a dictionary that I declared in a particular order and want to keep it in that order all the time. The keys/values can't really be kept in order based on their value, I just want it in the order that I declared it.
So if I have the dictionary:
d = {'ac': 33, 'gw': 20, 'ap': 102, 'za': 321, 'bs': 10}
It isn't in that order if I view it or iterate through it. Is there any way to make sure Python will keep the explicit order that I declared the keys/values in?
From Python 3.6 onwards, the standard dict type maintains insertion order by default.
Defining
d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}
will result in a dictionary with the keys in the order listed in the source code.
This was achieved by using a simple array with integers for the sparse hash table, where those integers index into another array that stores the key-value pairs (plus the calculated hash). That latter array just happens to store the items in insertion order, and the whole combination actually uses less memory than the implementation used in Python 3.5 and before. See the original idea post by Raymond Hettinger for details.
In 3.6 this was still considered an implementation detail; see the What's New in Python 3.6 documentation:
The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon (this may change in the future, but it is desired to have this new dict implementation in the language for a few releases before changing the language spec to mandate order-preserving semantics for all current and future Python implementations; this also helps preserve backwards-compatibility with older versions of the language where random iteration order is still in effect, e.g. Python 3.5).
Python 3.7 elevates this implementation detail to a language specification, so it is now mandatory that dict preserves order in all Python implementations compatible with that version or newer. See the pronouncement by the BDFL. As of Python 3.8, dictionaries also support iteration in reverse.
You may still want to use the collections.OrderedDict() class in certain cases, as it offers some additional functionality on top of the standard dict type. Such as as being reversible (this extends to the view objects), and supporting reordering (via the move_to_end() method).
from collections import OrderedDict
OrderedDict((word, True) for word in words)
contains
OrderedDict([('He', True), ('will', True), ('be', True), ('the', True), ('winner', True)])
If the values are True (or any other immutable object), you can also use:
OrderedDict.fromkeys(words, True)
Rather than explaining the theoretical part, I'll give a simple example.
>>> from collections import OrderedDict
>>> my_dictionary=OrderedDict()
>>> my_dictionary['foo']=3
>>> my_dictionary['aol']=1
>>> my_dictionary
OrderedDict([('foo', 3), ('aol', 1)])
>>> dict(my_dictionary)
{'foo': 3, 'aol': 1}
Note that this answer applies to python versions prior to python3.7. CPython 3.6 maintains insertion order under most circumstances as an implementation detail. Starting from Python3.7 onward, it has been declared that implementations MUST maintain insertion order to be compliant.
python dictionaries are unordered. If you want an ordered dictionary, try collections.OrderedDict.
Note that OrderedDict was introduced into the standard library in python 2.7. If you have an older version of python, you can find recipes for ordered dictionaries on ActiveState.
Dictionaries will use an order that makes searching efficient, and you cant change that,
You could just use a list of objects (a 2 element tuple in a simple case, or even a class), and append items to the end. You can then use linear search to find items in it.
Alternatively you could create or use a different data structure created with the intention of maintaining order.
I came across this post while trying to figure out how to get OrderedDict to work. PyDev for Eclipse couldn't find OrderedDict at all, so I ended up deciding to make a tuple of my dictionary's key values as I would like them to be ordered. When I needed to output my list, I just iterated through the tuple's values and plugged the iterated 'key' from the tuple into the dictionary to retrieve my values in the order I needed them.
example:
test_dict = dict( val1 = "hi", val2 = "bye", val3 = "huh?", val4 = "what....")
test_tuple = ( 'val1', 'val2', 'val3', 'val4')
for key in test_tuple: print(test_dict[key])
It's a tad cumbersome, but I'm pressed for time and it's the workaround I came up with.
note: the list of lists approach that somebody else suggested does not really make sense to me, because lists are ordered and indexed (and are also a different structure than dictionaries).
You can't really do what you want with a dictionary. You already have the dictionary d = {'ac':33, 'gw':20, 'ap':102, 'za':321, 'bs':10}created. I found there was no way to keep in order once it is already created. What I did was make a json file instead with the object:
{"ac":33,"gw":20,"ap":102,"za":321,"bs":10}
I used:
r = json.load(open('file.json'), object_pairs_hook=OrderedDict)
then used:
print json.dumps(r)
to verify.
from collections import OrderedDict
list1 = ['k1', 'k2']
list2 = ['v1', 'v2']
new_ordered_dict = OrderedDict(zip(list1, list2))
print new_ordered_dict
# OrderedDict([('k1', 'v1'), ('k2', 'v2')])
Another alternative is to use Pandas dataframe as it guarantees the order and the index locations of the items in a dict-like structure.
I had a similar problem when developing a Django project. I couldn't use OrderedDict, because I was running an old version of python, so the solution was to use Django's SortedDict class:
https://code.djangoproject.com/wiki/SortedDict
e.g.,
from django.utils.datastructures import SortedDict
d2 = SortedDict()
d2['b'] = 1
d2['a'] = 2
d2['c'] = 3
Note: This answer is originally from 2011. If you have access to Python version 2.7 or higher, then you should have access to the now standard collections.OrderedDict, of which many examples have been provided by others in this thread.
Generally, you can design a class that behaves like a dictionary, mainly be implementing the methods __contains__, __getitem__, __delitem__, __setitem__ and some more. That class can have any behaviour you like, for example prividing a sorted iterator over the keys ...
if you would like to have a dictionary in a specific order, you can also create a list of lists, where the first item will be the key, and the second item will be the value
and will look like this
example
>>> list =[[1,2],[2,3]]
>>> for i in list:
... print i[0]
... print i[1]
1
2
2
3
You can do the same thing which i did for dictionary.
Create a list and empty dictionary:
dictionary_items = {}
fields = [['Name', 'Himanshu Kanojiya'], ['email id', 'hima#gmail.com']]
l = fields[0][0]
m = fields[0][1]
n = fields[1][0]
q = fields[1][1]
dictionary_items[l] = m
dictionary_items[n] = q
print dictionary_items

Filtering or matching in nested list

My data structure was original a big Map. But I read that we should not use too big maps, to not run out of atoms. So my new data structure looks like that.
countries = [[{'name', 'Germany'}, {'code', 'DE'}], [{'name', 'Austria'}, {'code', 'AT'}]]
I want to make a filter_by/3 method, to filter this nested list for the country list by attributes name or code
Should I transform the Tuples to Maps or is there another way to filter this?
You could use a list of maps. Maps are very performant when retrieving elements, especially when the keys in a map are very few.
In your example:
countries = [%{name: "Germany", code: "DE"},
%{name: "Austria", code: "AT"}]
Note that even if you'll use thousands of such maps in a list, you'll never run out of atoms since :name and :code will always be the only two allocated atoms (since each atom is exactly is value, so writing :a and :a is like writing 3 and 3).
Once you have a similar list in place, you can filter it with a function like:
def filter_by(countries, key, value) do
Enum.filter(countries, fn(country) -> country[key] == value end)
end
filter_by(countries, :code, "AT")

list of lists to either autovivify or recursive data structure

I have a list of lists in the format below. This is data coming from a csv and I am trying to emulate the data review function that excel has in python. The only reason I can't do it directly in excel is this document is almost 1GB and has 1.1 mil row.
((a1,b1,c1,d1,e1),(a1,b2,c1,d2,e2),(a1,b1,c2,d3,e3),(a2,b1,c1,d3,e4),(a2,b2,c2,d3,e5)...)
I want to convert it into a single data structure something like a multidimensional array. like below
((a1:(b1:(c1:(),c2:()),b2:(),b3:()),a2:(b1:(c1:()),b2:(c2:()),b3:())))
I use autovivify class for other purposes but I can't use it here because some of the keys I want to use are strings. Appreciate help here.
If I understand your question correctly, you want to transform that list into a tree-like structure, where each tuple in the list represents one path down the tree. You can do this using nested dictionaries:
def add_to_dict(d, t):
if t:
first, rest = t[0], t[1:]
nested = d.setdefault(first, {})
add_to_dict(nested, rest)
Given a dictionary d (initially empty) and one of those tuples t, if that tuple is not empty, it takes the first element from the tuple, adds a nested dictionary to the original dictionary using this element as key (or takes one that already exists in this place), and adds the rest of the tuple to that dictionary in the same way.
Example using your data:
data = (('a1','b1','c1','d1','e1'),
('a1','b2','c1','d2','e2'),
('a1','b1','c2','d3','e3'),
('a2','b1','c1','d3','e4'),
('a2','b2','c2','d3','e5'))
d = {}
for t in data:
add_to_dict(d, t)
The resulting dictionary d looks like this:
{'a1': {'b1': {'c1': {'d1': {'e1': {}}},
'c2': {'d3': {'e3': {}}}},
'b2': {'c1': {'d2': {'e2': {}}}}},
'a2': {'b1': {'c1': {'d3': {'e4': {}}}},
'b2': {'c2': {'d3': {'e5': {}}}}}}

Using integer as dictionary key using dict()

I'm trying to use numbers as my dict key. Is there anyway to initiate the dictionary using dict() method?
This works
mydict = { '100':'hundred', '200':'two hundred'}
This doesn't work?
mydict = dict( 100='hundred' )
The error says 'keyword can't be an expression' and I couldn't find any solution.
Thank you.
I can't understand your question exactly, but you mentioned to use number as dict key right? you just directly initiate it using integer instead string like this..
a = {1:'one',100:'hundered'}
print a
{1: 'one', 100: 'hundrered'}
No, it mist be a valid python identifier, so it cannot start with a number.
You can read where i found it at here in the part about dict
https://docs.python.org/2/library/stdtypes.html#typesmapping
Like the comment above says you can use an int, since dictionaries just hash the string and you get an int anyways, in the case of an int it just hashes to itself. But it doesnt work with dict ()
On that page it shows you can do
mydict = dict (zip ([1], ["one"]))
Which is kinda ugly imo, but seems to get the job done
To use the dict method you need to feed it a list or tuple of lists or tuples.
>>> dict([(100, 'hundred'), (200, 'two hundred')])
{200: 'two hundred', 100: 'hundred'}