Python: How to unpack tuples in dictionary - python

I'm currently storing data into a dictionary as a tuple, but I don't know how to unpack the tuple from the dictionary itself. I get a ValueError saying too many values to unpack in the way I am trying to do it. Here is the code:
for row in csvReader:
if row['de_description'] and row['nh_description']:
if 'XT2R' in row['de_description']:
id = (row['de_description'], row['nh_description']:
if ('TCN' in row['de_description'] and '77880' in row['src_dp']:
rounded_time = int(float(row['rr_polltime']))
dataDict[id].append((rounded_time, row['rr_age']))
timeSet.add(rounded_time)
fileHandle.close()
#unpacking tuple?
for id, (valX,valY) in dataDict.iteritems():
ageSet.add(valY)
print "ageSet=", ageSet
I also realize there is a lot of redundancy in my code but that is not currently my issue. If anyone has ever worked with unpacking tuples from a dictionary, pointing me in the right direction would be great.

Referring to
dataDict[id].append((rounded_time, row['rr_age']))
your dataDict seems to be a dictionary of lists, since you append values (tuples, in this case) to dataDict[id].
dataDict.iteritems(), however, returns an iter object of key-value-pairs, which are the dictionary key and the list.
Trying to unpack the list into (valX, valY) results in the ValueError you experience.

Related

Navigating nested dictionary

I'm trying to wrap my head around how to navigate a series of dictionaries nested in a list.
For example: mydict = {'first':[{'second':2, 'third':3}, {'fourth':4}]}
When I type mydict.get('first'), I get the whole list.
I can't use indexing to get each individual dictionaries in the list (i.e. mydict.get(['first'][0] returns the whole list, and mydict.get(['first'][1]) returns an IndexError).
mydict.get(['first'][0]['second']) andmydict.get(['first']['second']) return TypeErrors.
So, if I wanted to call 'second' or 'fourth' or assign their values to variables, how would I do it?
For second:
mydict['first'][0]['second]
['first'] returns the array
[0] returns the first object of the array
['second'] gets the 'second' object
Perhaps try reshaping your data to something more convenient?
In your example, mydict isn't a series of dictionaries nested in a list. It is a dictionary that contains lists that, in turn, contain dictionaries.
So assuming you don't know which inner dictionary will contain the key you're looking for, you'd have to iterate over all the entries in the parent dictionary to find it. Something like:
desiredKey = 'second'
for listOfDict in mydict.values():
for childDict in listOfDict:
if desiredKey in childDict:
print(childDict[desiredKey])
This will only work if the key you're looking for is always in the inner most dictionaries.

Unhashable type: 'list' in dictionary

I wrote some code to somewhat grab new values from a second dictionary and add to a new dictionary.
These are the two dictionaries:
a = {armani: jeans, dolce: gabbana}
b = {jeans: robo, jobs: versace}
This is the code:
{k:b[v] for k,v in a.items() if v in b}
However, I am getting the following error: unhashable type: 'list' I understand that it might be because my values for b[v] are like [[20], [30], [35]]
My head has been going crazy for the past few hours. I am sure it is something so small
It's not something so small. You need to understand some concepts before continuing with your process.
Dictionaries allow you to access their values with keys such as strings or integers rather than indexes (like lists) right ? Well there should be a mechanism behind the curtains to make this happen which is called a hash.
Every time you put a key:value pair inside a dictionary, a hash value is calculated for that key. With immutable objects (Objects that cannot be changed after creation, that can only be re-created), the hash value always the same for the same string or integer or other immutable objects, this is how the dictionary accesses its values with a key.
Mutable objects on the other hand, can't be hashed. Therefore is not fit to be used as a key inside a dictionary. And list is one of them.
If you must, you can convert the list into a tuple to use it in a dictionary as a key.
lst = [1, 2, 3]
tup = tuple(lst)
Keep in mind that you can't change the elements of a tuple after creation, such as;
tup[0] = 1
Which is why it is immutable. You can only replace it with another tuple, if you require its values to be changed.
Note: The tuple cannot contain lists as elements as well, if it is required to be used for hashing (Which would make it mutable).

Using a for loop to print keys and/or values in a dictionary for python. Looking for logical thinking explanation thanks :D

My problem is understanding why these certain lines of code do what they do. Basically why it works logically. I am using PyCharm python 3 I think.
house_Number = {
"Luca": 1, "David": 2, "Alex": 3, "Kaden": 4, "Kian": 5
}
for item in house_Number:
print(house_Number[item]) # Why does this print the values tied with the key?
print(item) # Why does this print the key?
This is my first question so sorry I don't know how to format the code to make it look nice. My question is why when you use the for loop to print the dictionary key or value the syntax to print the key is to print every item? And what does it even mean to print(house_Number[item]).
They both work to print key or value but I really want to know a logical answer as to why it works this way. Thanks :D
I'm not working on any projects just starting to learn off of codeacademey.
In Python, iteration over a dictionary (for item in dict) is defined as iteration over that dictionary's keys. This is simply how the language was designed -- other languages and collection classes do it differently, iterating, for example, over key-value tuples, templated Pair<X,Y> objects, or what have you.
house_Number[item] accesses the value in house_Number referenced by the key item. [...] is the syntax for indexing in Python (and most other languages); an_array[2] gives the third element of an_array and house_Number[item] gives the value corresponding to the key item in the dictionary house_Number.
Just a side note: Python naming conventions would dictate house_number, not house_Number. Capital letters are generally only used in CamelCasedClassNames and CONSTANTS.
In python values inside a dictionary object are accessed using dictionay_name['KEY']
In your case you are iterating over the keys of dictionary
Hope this helps
for item in dic:
print(item) # key
print(dic[item]) # value
Dictionaries are basically containers containing some items (keys) which are stored by hashing method. These keys just map to the values (dic[key]).
Like in set, if you traverse using for loop, you get the keys from it (in random order since they are hashed). Similarly, dictionaries are just sets with a value associated with it. it makes more sense to iterate the keys as in sets (too in random order).
Read more about dicionaries here https://docs.python.org/3/tutorial/datastructures.html#dictionaries and hopefully that will answer your question. Specifically, look at the .items() method of the dictionary object.
When you type for item in house_Number, you don’t specify whether item is the key or value of house_Number. Then python just thinks that you meant the key of house_Number.
So when you do the function print(house_Number[item]), you’re printing the value because your taking the key and finding the value. In other words, you taking each key once, and finding their values, which are 1, 2, 3, 4, 5, 6
The print(item) is just to print the item, which are the keys, "Luca", "David", "Alex", "Kaden", "Kian"
Because the print(house_Number[item]) and print(item) alternating, you get the keys and values alternating, each on a new line.

How to store an array in a dictionary using python

I am currently attempting to modify a series of programs by utilizing dictionaries as opposed to arrays. I have columns of raw information in a file, which is then read into an ASCII csv file. I need to convert this file into a dictionary, so that it can be fed into another program.
I used a numpy.genfromtxt to pull out the information i need from the csv file, following this format:
a,b,c,d = np.genfromtxt("file",delimiter = ',', unpack = true)
this step works completely fine.
I then attempt to build a dictionary:
ouputDict = dict([a,a],[b,b],[c,c],[d,d])
As i understand it, this should make the key "a" in the dictionary a correspond to the array "a".
thus if:
a = [1,2,3,4]
then:
outputDict[a][0] = 1
However, when i attempt to create this dictionary i receive the following error:
TypeError: unhashable type: 'numpy.ndarray'
Why can't I construct an array in this fashion and what is the workaround, if any? Any help will be greatly appreciated!
You can do this even with using collections
Declare your dictionary as:
Dictionary = {}; // {} makes it a key, value pair dictionary
add your value for which you want an array as a key by declaring
Dictionary[a] = [1,2,3,4]; // [] makes it an array
So now your dictionary will look like
{a: [1,2,3,4]}
Which means for key a, you have an array and you can insert data in that which you can access like dictionary[a][0] which will give the value 1 and so on. :)
Btw.. If you look into examples of a dictionary, array and key value pairs, nested dictionary, your concept will get clearer.
Copied from my comment:
Correct dictionary formats:
{'a':a, 'b':b,...}, or
dict(a=a, b=b,...)
dict([('a', a), ('b', b),...])
The goal is to make the strings 'a','b',etc the keys, not the variable values.

How do I get all the keys that are stored in the Cassandra column family with pycassa?

Is anyone having experience working with pycassa I have a doubt with it. How do I get all the keys that are stored in the database?
well in this small snippet we need to give the keys in order to get the associated columns (here the keys are 'foo' and 'bar'),that is fine but my requirement is to get all the keys (only keys) at once as Python list or similar data structure.
cf.multiget(['foo', 'bar'])
{'foo': {'column1': 'val2'}, 'bar': {'column1': 'val3', 'column2': 'val4'}}
Thanks.
try:
list(cf.get_range().get_keys())
more good stuff here: http://github.com/vomjom/pycassa
You can try: cf.get_range(column_count=0,filter_empty=False).
# Since get_range() returns a generator - print only the keys.
for value in cf.get_range(column_count=0,filter_empty=False):
print value[0]
get_range([start][, finish][, columns][, column_start][, column_finish][, column_reversed][, column_count][, row_count][, include_timestamp][, super_column][, read_consistency_level][, buffer_size])
Get an iterator over rows in a
specified key range.
http://pycassa.github.com/pycassa/api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_range
Minor improvement on Santhosh's solution
dict(cf.get_range(column_count=0,filter_empty=False)).keys()
If you care about order:
OrderedDict(cf.get_range(column_count=0,filter_empty=False)).keys()
get_range returns a generator. We can create a dict from the generator and get the keys from that.
column_count=0 limits results to the row_key. However, because these results have no columns we also need filter_empty.
filter_empty=False will allow us to get the results. However empty rows and range ghosts may be included in our result now.
If we don't mind more overhead, getting just the first column will resolve the empty rows and range ghosts.
dict(cf.get_range(column_count=1)).keys()
There's a problem with Santhosh's and kzarns' answers, as you're bringing in memory a potentially huge dict that you are immediately discarding. A better approach would be using list comprehensions for this:
keys = [c[0] for c in cf.get_range(column_count=0, filter_empty=False)]
This iterates over the generator returned by get_range, keeps the key in memory and stores the list.
If the list of keys where also potentially too large to keep it in memory all at once and you only need to iterate once, you should use a generator expression instead of a list comprehension:
kgen = (c[0] for c in cf.get_range(column_count=0, filter_empty=False))
# you can iterate over kgen, but do not treat it as a list, it isn't!

Categories