Converting nested list into dictionary - list

Learning a new thing here - I've been trying to tackle a problem all day and haven't had much success. The idea is to loop through a nested list and return a dictionary. However, the first element of the list contains the column headers for the dictionary values. So here is the nested list, or table_data:
table_data = [
["first_name", "last_name", "city", "state"],
["Elisabeth", "Gardenar", "Toledo", "OH"],
["Jamaal", "Du", "Sylvania", "OH"],
["Kathlyn", "Lavoie", "Maumee", "OH"]
]
convert_table(table_data)
I want to convert the nested list into a dictionary as seen below. Basically, I'd like a function to take in the nested list then spit out the output as shown below.
[
{"first_name": "Elisabeth", "last_name": "Gardenar", "city": "Toledo", "state": "OH"},
{"first_name": "Jamaal", "last_name": "Du", "city": "Sylvania", "state": "OH"},
{"first_name": "Kathlyn", "last_name": "Lavoie", "city": "Maumee", "state": "OH"}
]
Here is some of the code I've been fiddling with so far, but am kind of stuck on how to get the elements of the first index of the list to repeat and become key's for the values in the rest of the dictionary.
Thank you!
for i in range(len(table_data)):
for j in table_data[0]:
print(j)
for i in table_data:
for j in i:
print(j)

Being aware that Python dictionaries are inherently unordered you can do it this way:
lst = []
for row in table_data[1:]:
lst.append(dict(zip(table_data[0],row)))
If you need to preserve order use an OrderedDict like so:
import collections as co
lst_ordered = []
for row in table_data[1:]:
lst_ordered.append(co.OrderedDict(zip(table_data[0],row)))

Bernie got this right. Now I'm going to study it. Thank you.
lst = []
for row in table_data[1:]:
lst.append(dict(zip(table_data[0],row)))

Related

How do I make row-based tuples from column range or list?

This is probably really simple but I'm not too clear.
Let's say I have a data frame and a list of column references. My goal is to make a list of tuples that give that row number's values for only the columns contained in my list.
raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
'sex': ['male', 'female', 'male', 'female', 'female'],
'dog': ['Burt','Kane','Billy','Bob','Thorton'],
'cat': ['Evil','PurrEvil','Rorry','Meowth','Killer'],
'fish': ['Johhny','Nemo','Dorry','Jacob','Pinky']}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'sex'])
colref = ['dog','cat','fish']
I want to make a list of tuples like [['Burt','Evil','Johhny'],['Kane','PurrEvil','Nemo'],...]
but I want to do so without hardcoding column names or numbers. The real data set I am performing this on is much larger and variable in size but my list colref will always include all of the columns I want in my tuple list. Does anyone have any tips for me?
I think I may have figured it out.. lol
tuples = [tuple(x) for x in df[colref].values]
If there is a better solution though please let me know. I'm having fun looking at other people's solutions to the problems I encounter as a noob.

Modify dictionary in Python

I have the following dictionary:
DATA = {"records": [{"key1": "AAA", "key2": "BBB", "key3": "CCC",
"key4": "AAA"}]}
I want to change "records" with for example "XX" so I will have the following:
DATA = {"XX": [{"key1": "AAA", "key2": "BBB", "key3": "CCC", "key4":
"AAA"}]}
How can I do this? Thank you.
You can't change a string dictionary key directly in the way you have presented. The reason for this is that the key itself- which again, is a string, or str object- is immutable. This would not necessarily be true for other types of keys, such as for a user-defined object.
Your only option is to add the new key to the existing dictionary, or create a new dictionary with the key you want.
You can assign a new dictionary to the same name, DATA, and add the entry you want to the new dictionary (the old one will eventually be garbage collected):
DATA = {'XX': DATA['records']}
IMPORTANT: Note that any other references to the old DATA dictionary will NOT be updated! For this reason, the new dictionary approach is probably not what you want.
Alternatively, if it is acceptable to keep the records key in your dictionary, you can simply add the XX member to the dictionary and point it to the same value:
DATA['XX'] = DATA['records']
Lastly, if you both want to keep the original dictionary AND remove the records key from it, you will have to do that in two steps:
DATA['XX'] = DATA['records']
del DATA['records']
OR:
DATA['XX'] = DATA.pop('records')
Note that the last suggestion still occurs in two steps even though it is one line of code.

How do I get unique values of a column in AWS Dynamo?

Say, in AWS Dynamo, I have a table like this:
ID (HKey) Date (RKey) BoxName
0 1/1/2014 Box-1
1 2/1/2014 Box-1
2 3/1/2014 Box-2
3 4/1/2014 Box-3
4 5/1/2014 Box-3
5 5/1/2014 Box-1
I want to, in a single query, get the first row for each unique Box. There could be hundreds of boxes I need the first entry for at once, making individual requests inefficient.
I can't seem to find anything in the API that would allow me to do this. Is it possible? How would I do this?
You might want to consider creating a Global secondary index (GSI) on Boxname (hash key) and date as your range key. This will enable you to use Query API on the secondary index where you can query "Find all IDs with Boxname = $box".
See the documentation for GSI.
Hope this helps,
Swami
There's no way to query just for the first appearance of each box without creating an index for the boxes as suggested above. However, if you don't mind reading the whole table and then picking the right lines, then read the whole table into an array, and then make it unique by some simple piece of code. For example, suppose you've read the table into an array (note that you might have to make several calls to scan or query until you get them all), and the array is something like this:
l = [
{"ID": "0", "Date": "1/1/2014", "BoxName": "Box-1"},
{"ID": "1", "Date": "2/1/2014", "BoxName": "Box-1"},
{"ID": "2", "Date": "3/1/2014", "BoxName": "Box-2"},
{"ID": "3", "Date": "4/1/2014", "BoxName": "Box-3"},
{"ID": "4", "Date": "5/1/2014", "BoxName": "Box-3"},
{"ID": "5", "Date": "5/1/2014", "BoxName": "Box-1"}
]
Then, a simple code like this in python will give you the list in the variable "out":
out = []
seen = []
for line in l:
if line["BoxName"] not in seen:
seen.append(line["BoxName"])
out.append(line)

how to check if previous element is similar to next elemnt in python

I have a text file like:
abc
abc
abc
def
def
def
...
...
...
...
Now I would like o create a list
list1=['abc','abc','abc']
list2=['def','def','def']
....
....
....
I would like to know how to check if next element is similar to previous element in a python for loop.
You can create a list comprehension and check if the ith element is equal to the ith-1 element in your list.
[ list1[i]==list1[i-1] for i in range(len(list1)) ]
>>> list1=['abc','abc','abc']
>>> [ list1[i]==list1[i-1] for i in range(len(list1)) ]
[True, True, True]
>>> list1=['abc','abc','abd']
>>> [ list1[i]==list1[i-1] for i in range(len(list1)) ]
[False, True, False]
This can be written within a for loop as well:
aux_list = []
for i in range(len(list1)):
aux_list.append(list1[i]==list1[i-1])
Check this post:
http://www.pythonforbeginners.com/lists/list-comprehensions-in-python/
for i in range(1,len(list)):
if(list[i] == list[i-1]):
#Over here list[i] is equal to the previous element i.e list[i-1]
file = open('workfile', 'r') # open the file
splitStr = file.read().split()
# will look like splitStr = ['abc', 'abc', 'abc', 'def', ....]
I think the best way to progress from here would be to use a dictionary
words = {}
for eachStr in splitStr:
if (words.has_key(eachStr)): # we have already found this word
words[eachStr] = words.get(eachStr) + 1 # increment the count (key) value
else: # we have not found this word yet
words[eachStr] = 1 # initialize the new key-value set
This will create a dictionary so the result would look like
print words.items()
[('abc', 3), ('def', 3)]
This way you store all of the information you want. I proposed this solution because its rather messy to create an unknown number of lists to accommodate what you want to do, but it is easy and memory efficient to store the data in a dictionary from which you can create a list if need be. Furthermore, using dictionaries and sets allow you to have a single copy of each string (in this case).
If you absolutely need new lists let me know and I will try to help you figure it out

How to display a list in django tables 2

I have a list containing some objects that i want to display in django tables 2 but as a result i got the - in all the columns.
My list is like this format [[<Person>],[<Person>]]
Reading the documentation I've found that this format works :
data = [
{"name": "Bradley"},
{"name": "Stevie"},
]
How can I get a format like this knowing that my data is dynamic?
Update :
I tried this :
for x in person_list:
for y in x:
data=[
{"firstname": y.firstname},
{"surname":y.surname},
]
The problem is now it displays every field in a row, I mean first name in row and surname in another one. How to append to the same data ?
Try to produce your data table manually
I suppose person has first_name field
data=[{"name": x.first_name} for x in persons_list]
Ok so I found the solution to this inspired by this :
d={}
dlist=[]
for x in person_list:
for y in x:
d['firstname']=y.firstname
d['lastname']=y.lastname
dlist.append(d)
And it works like a charm !