How to flatten a nested dictionary in python 2.7x - python

I have a nested dict that looks like the below. How do I flatten that so there is a row for each botten level as a list of lists?
e.g
[[en,Chome,ChromeOS,null,180, '0', '0', '0', '0',
[en, Linux,null,27868, '0', '0', '0', '0'],
...
]
input
test = {u'en': {'Chrome': {'ChromeOS': {u'null': [180, '0', '0', '0', '0']},
'Linux': {u'null': [27868, '0', '0', '0', '0']},
'Macintosh': {u'null': [330991, '0', '0', '0', '0']},
'Windows': {u'null': [3296819, '0', '0', '0', '0']}},
'Firefox': {'Linux': {u'null': [18076, '0', '0', '0', '0']},
'Macintosh': {u'null': [168444, '0', '0', '0', '0']},
'Windows': {u'null': [1517775, '0', '0', '0', '0']}},
'Konqueror': {'Linux': {u'null': [9, '0', '0', '0', '0']}},
'Microsoft Internet Explorer': {'Windows': {u'null': [3060154,
'0',
'0',
'0',
'0']}},
'Opera': {'Linux': {u'null': [2274, '0', '0', '0', '0']},
'Macintosh': {u'null': [1573, '0', '0', '0', '0']},
'Windows': {u'null': [38589, '0', '0', '0', '0']}},
'Opera Mobile': {'Linux': {u'null': [5807, '0', '0', '0', '0']},
'Windows': {u'null': [1, '0', '0', '0', '0']}},
'Safari': {'Blackberry': {u'null': [530, '0', '0', '0', '0']},
'Linux': {u'null': [292862, '0', '0', '0', '0']},
'Macintosh': {u'null': [618641, '0', '0', '0', '0']},
'Windows': {u'null': [36359, '0', '0', '0', '0']}},
'SeaMonkey': {'Linux': {u'null': [120, '0', '0', '0', '0']},
'Macintosh': {u'null': [224, '0', '0', '0', '0']},
'Windows': {u'null': [1890, '0', '0', '0', '0']}},
'WOSBrowser': {'Linux': {u'null': [687, '0', '0', '0', '0']}},
'null': {'null': {u'null': [15559, '0', '0', '0', '0']}}}}

def flatten_dict(d):
for k,v in d.items():
if isinstance(v, dict):
for item in flatten_dict(v):
yield [k]+item
else:
yield v
flattened = list(flatten_dict(test))

If the terminal node is not a list, then you should "yield [v]" instead of "yield v".

Related

append key if matches specific index and keyword?

titleValues = {'Movie 1 (1998)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Movie 2 (1994)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']}
categories={'unknown': '0', 'Action': '1', 'Adventure': '2', 'Animation': '3', "Children's": '4', 'Comedy': '5', 'Crime': '6', 'Documentary': '7', 'Drama': '8', 'Fantasy': '9', 'Film-Noir': '10', 'Horror': '11', 'Musical': '12', 'Mystery': '13', 'Romance': '14', 'Sci-Fi': '15', 'Thriller': '16', 'War': '17', 'Western': '18'}
selectedCol = 1
titles=[]
for key, value in titleValues.items():
for num in value:
if num == '1':
valIdx = value.index(num)
if valIdx == selectedCol:
titles.append(key)
else:
continue
print(titles)
output:
['Movie 1 (1998)', 'Movie 1 (1998)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)']
I think it appears 6 times because of the six '1' occurrences. However, how can I only obtain two names as for both lists '1' appears at index 1.
['Movie 1 (1998)', 'Movie 2 (1994)']
only when titleValues contains a one, put key once in a list:
titles = [k for k,v in zip(titleValues.keys(),titleValues.values()) if '1' in v]
result will be
print(titles)
# ['Movie 1 (1998)', 'Movie 2 (1994)']
Explanation
Create two iterables (keys, values)
print(titleValues.keys())
#dict_keys(['Movie 1 (1998)', 'Movie 2 (1994)'])
print(titleValues.values())
#dict_values([['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']])
zip, is a function to iterate element-wise and simultanouesly over two iterables
print(list(zip(titleValues.keys(),titleValues.values())))
# [('Movie 1 (1998)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']), ('Movie 2 (1994)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0'])]
in a for-comprehension you can access elements of both iterables (specified variable name: k for element in first iterable and v for element in second)

quotation marks elimination from shift array

I have a question. I have this array but I don't know how to eliminate single quotation marks. I have this, can you help me with it?
array([['0.3', '0.3', '0.3', '0', '0', '0', '0', '0', '0', '0'],
['0.0', '0.0', '0.0', '0', '0', '0', '0', '0', '0', '0'],
['1.0', '1.0', '1.0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0.7', '0.7', '0.7', '0', '0', '0', '0', '0', '0'],
['0', '0.0', '0.0', '0.0', '0', '0', '0', '0', '0', '0'],
['0', '0.3', '0.3', '0.3', '0', '0', '0', '0', '0', '0'],
['0', '0', '0.7', '0.7', '0.7', '0', '0', '0', '0', '0'],
['0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0', '0'],
['0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0.3', '0.3', '0.3', '0', '0', '0', '0'],
['0', '0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0'],
['0', '0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0.3', '0.3', '0.3', '0', '0', '0'],
['0', '0', '0', '0', '0.3', '0.3', '0.3', '0', '0', '0'],
['0', '0', '0', '0', '0.0', '0.0', '0.0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0.0', '0.0', '0.0', '0', '0'],
['0', '0', '0', '0', '0', '0.0', '0.0', '0.0', '0', '0'],
['0', '0', '0', '0', '0', '0.7', '0.7', '0.7', '0', '0'],
['0', '0', '0', '0', '0', '0', '0.3', '0.3', '0.3', '0'],
['0', '0', '0', '0', '0', '0', '0.3', '0.3', '0.3', '0'],
['0', '0', '0', '0', '0', '0', '0.0', '0.0', '0.0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0.3', '0.3', '0.3'],
['0', '0', '0', '0', '0', '0', '0', '0.0', '0.0', '0.0'],
['0', '0', '0', '0', '0', '0', '0', '0.0', '0.0', '0.0']],
dtype=object)
I tried this :
dfff1 = dfff.replace("'", " ") but I got this error:
AttributeError: 'numpy.ndarray' object has no attribute 'replace'
Should I use "for" to gedd rid of it? something like: for mar1 in dfff:
for mar2 in mar1:
mar2.replace("''", "")
the problem with this is that I don't know how to make them an array again ( with the same order)

list position replacement with if statement

Can we perform replace on python list?
I have that list that is imported from csv file:
[['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['2', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']]
fist index(mark as bold) from list above will became costumer id followed by the item that they buy, from list above we can see costumer 1 purchased item in column 12 that marked by italic.
desire out put is :
costumer 1 purchased item 12 and item 22 and so it's for costumer 2 and
3
Note that I have try used panda and not work, and I not sure how to use if statement inside the for loop.
Also, I have used rappid minner and they replaced column by column and they included the [0][0] to be replaced. Is there any other solution beside python?
Here is my code:
import csv
csvfile = open("tran.csv", 'r')
reader = csv.reader(csvfile, delimiter=',')
my_list = list(reader)
a = len(my_list[1])
b = (my_list)
x=0
y=0
for name in my_list:
print ("costummer", my_list[0][0], "buy", my_list[n][g])
update for csv writer:
csvdict = {words[0]:words[1:] for words in csv}
for x in csvdict.keys(): # iterate over the keys '1', '2', ....
products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed
f = open("trans.csv", 'w')
result = ("costummer", x, "buy", products)
resultstr = (','.join([str(x) for x in hasil]))#I try to convert it to str.
print (resultstr) #to check whether the conversion from list into str is working or not.
f.write(resultstr) #try to write to csv file
Here is a "starting point", there will be an extra index for the customer 1, that is something you can play around with. It might not be important. You can add a if clause to take care of it.
csv = [['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['2', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']]
csvdict = {words[0]:words[1:] for words in csv}
for x in sorted(csvdict.keys()): # iterate over the keys '1', '2', ....
products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed
print "costummer", x, "purchased", ' '.join(['"item '+str(i)+'"' for i in B])

Having troubles with list

Okey, I'm currently writing Conway's Game of Life and I have reached a point that I don't know how to solve.
Edit here are the different lists I'm having trouble with:
for repeticiones in range(0,5):
nuevo_mapa = mapax
print(mapax)
if '#' in mapax:
print('Jell')
if mapax is mapa_juego:
print('God')
for y in range(0,10):
for x in range(0,10):
if mapa_juego[y][x] == '#':
viva_muerta = True
elif mapa_juego[y][x] == '0':
viva_muerta = False
for i in range(0,8):
try:
if mapa_juego[y + check_listy[i]][x + check_listx[i]] == '#':
sum += 1
except IndexError:
pass
if viva_muerta == True and sum in [0,1]:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum >= 4:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum in [2,3]:
nuevo_mapa[y][x] = '#'
elif viva_muerta == False and sum == 3:
nuevo_mapa[y][x] = '#'
sum = 0
mapa_juego = nuevo_mapa
print('\n\n')
mapax is a list of lists full of 0.
I want to do this:
nuevo_mapa = mapax
nuevo_mapa is modified
mapa_juego = nuevo_mapa
And start over and over again.
But the problem comes here, after the first iteration mapax is no more a full list of 0.
The output is something like this(I want publish it all because it's so messy and to avoid confusions)
>>> [0,0,0,0,0,0,0] First print of mapax
>>> [0,0,#,0,0,0,0] Second print of mapax and I want to avoid this.
If you see anything in the code let me know, thanks.
Edit: here are the different lists I'm having trouble with:
mapa_juego = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','#','0','0','0','0','0','0','0',],
['0','0','0','#','0','0','0','0','0','0',],
['0','#','#','#','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
mapax = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
2 Edit: Here is full code:
mapa_juego = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','#','0','0','0','0','0','0','0',],
['0','0','0','#','0','0','0','0','0','0',],
['0','#','#','#','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
mapax = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
viva_muerta = None
check_listy = [0,0,1,-1,-1,1,-1,1]
check_listx = [1,-1,0,0,1,1,-1,-1]
sum = 0
#Reglas
# Una célula muerta con exactamente 3 células vecinas vivas "nace" (es decir, al turno siguiente estará viva).
# Una célula viva con 2 ó 3 células vecinas vivas sigue viva, en otro caso muere o permanece muerta (por "soledad" o "superpoblación").
# Célula viva = True muerta = False
# for line in mapa_juego:
# print(line)
for repeticiones in range(0,5):
nuevo_mapa = mapax
print(mapax)
if '#' in mapax:
print('Jell')
if mapax is mapa_juego:
print('God')
for y in range(0,10):
for x in range(0,10):
if mapa_juego[y][x] == '#':
viva_muerta = True
elif mapa_juego[y][x] == '0':
viva_muerta = False
for i in range(0,8):
try:
if mapa_juego[y + check_listy[i]][x + check_listx[i]] == '#':
sum += 1
except IndexError:
pass
if viva_muerta == True and sum in [0,1]:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum >= 4:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum in [2,3]:
nuevo_mapa[y][x] = '#'
elif viva_muerta == False and sum == 3:
nuevo_mapa[y][x] = '#'
sum = 0
mapa_juego = nuevo_mapa
print('\n\n')
Edit 3: Real output
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God

How do you replace empty strings in sublists with '0'?

I have a list of sublists with each sublist having items within that are sometimes blank. I'd like to replace all of the blank items with '0'.
Original_List = [['Thomas Jefferson', '0.0', '0', '0', '6.0', '0', '12', '177', '0', '', '0', '', '', '', '24000.0', '', '', '0'], ['George Washington', '0.0', '0', '0', '0.0', '0', '0', '214', '6', '', '0', '', '', '', '', '', '', '0'], ['Barack Obama', '2.0', '0', '0', '2.0', '0', '0', '525', '3', '', '0', '', '', '', '24000.0', '20000.0', '94000.0', '114000.0']]
Desired_List = [['Thomas Jefferson', '0.0', '0', '0', '6.0', '0', '12', '177', '0', '0', '0', '0', '0', '0', '24000.0', '0', '0', '0'], ['George Washington', '0.0', '0', '0', '0.0', '0', '0', '214', '6', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['Barack Obama', '2.0', '0', '0', '2.0', '0', '0', '525', '3', '0', '0', '0', '0', '0', '24000.0', '20000.0', '94000.0', '114000.0']]
I've tried:
return [map(lambda List_Item: re.sub('','0',List_Item),subli) for subli in lst]
This ends up replacing the wrong items with zeros.
Empty string is falsy value.
Using or operator, you can get '0' for empty string:
>>> '' or '0'
'0'
>>> '123' or '0'
'123'
>>> Original_List = [
['Thomas Jefferson', '0.0', '0', '0', '6.0', '0', '12', '177', '0', '', '0', '', '', '', '24000.0', '', '', '0'],
['George Washington', '0.0', '0', '0', '0.0', '0', '0', '214', '6', '', '0', '', '', '', '', '', '', '0'],
['Barack Obama', '2.0', '0', '0', '2.0', '0', '0', '525', '3', '', '0', '', '', '', '24000.0', '20000.0', '94000.0', '114000.0']
]
>>> [[x or '0' for x in xs] for xs in Original_List]
[['Thomas Jefferson', '0.0', '0', '0', '6.0', '0', '12', '177', '0', '0', '0', '0', '0', '0', '24000.0', '0', '0', '0'],
['George Washington', '0.0', '0', '0', '0.0', '0', '0', '214', '6', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['Barack Obama', '2.0', '0', '0', '2.0', '0', '0', '525', '3', '0', '0', '0', '0', '0', '24000.0', '20000.0', '94000.0', '114000.0']]

Categories

Resources