flatten

Python – How can I move a nested json dictionary up to its own index?

Python – How can I move a nested json dictionary up to its own index? Question: I have a json dataset where each item/index can contain 2 nested dictionaries. The problem is that one of these nested dictionaries contains all of the exact key:value pairs as its parent dictionary. To put it in other words, …

Total answers: 2

Apply outer tag attribute to inner tag – Flattening xml in python

Apply outer tag attribute to inner tag – Flattening xml in python Question: I have a xml file like: <plays format="tokens"> <period number="1"> <play/> <play/> <play/> </period> <period number="2"> <play/> <play/> <play/> </period> Each play tag contains a bunch of variables, but I would also like to add the period number as a variable to …

Total answers: 1

Expanding Nested lists in Python without fully flattening

Expanding Nested lists in Python without fully flattening Question: Suppose I have a list with nested lists such of strings such as: items = [‘Hello’, [‘Ben’, ‘Chris’, ‘Linda’], ‘! The things you can buy today are’, [‘Apples’, ‘Oranges’]] I want a list of strings that combine and flatten the nested lists into all possibilities such …

Total answers: 2

Merge dictionary of tuple items based only on first term of tuple key

Merge dictionary of tuple items based only on first term of tuple key Question: I am trying to convert a dictionary of tuples to a list of tuples. However, I need to do so based on the first value of the tuple key. I have worked out most of the steps to accomplish this, but …

Total answers: 1

How to extract non-empty elements from python for number list

How to extract non-empty elements from python for number list Question: I currently have a list that looks like this: [ [0], [2], [4], [6], [7], [9], [20], [24], [], [26], [], [27], [], [], [], [], [], [], [], [], [], [], [], [], [], [] ] How can I transform it to look …

Total answers: 2

Python 3: Flattening nested dictionaries and lists within dictionaries

Python 3: Flattening nested dictionaries and lists within dictionaries Question: I am dealing with a complex nested dictionary and list data structure. I need to flatten the data and bring all nested items to level 0. See below example for more clarity : {a:1,b:2,c:{c1:[{c11:1,c12:2,c13:3},{c21:1,c22:2,c23:3}],d1:[{d11:1,d12:2,d13:3},{d21:1,d22:2,d23:3}]},x:1,y:2} i need to flatten this to: {a:1,b:2,c_c1_c11:1, c_c1_c12:2,c_c1_c13:3,c_c1_c21:1,c_c1_c22:2,c_c1_c23:3, c_d1,d11:1…and so on} …

Total answers: 3

How to flatten a pandas dataframe with some columns as json?

How to flatten a pandas dataframe with some columns as json? Question: I have a dataframe df that loads data from a database. Most of the columns are json strings while some are even list of jsons. For example: id name columnA columnB 1 John {“dist”: “600”, “time”: “0:12.10”} [{“pos”: “1st”, “value”: “500”},{“pos”: “2nd”, “value”: …

Total answers: 4

How do I unnest a tuple with mixed types?

How do I unnest a tuple with mixed types? Question: I’m trying to flatten a tuple with mixed types into a list. The following function does not produce the desired output: a = (1, 2, 3, [‘first’, ‘second’]) def flatten(l): return flatten(l[0]) + (flatten(l[1:]) if len(l) > 1 else []) if type(l) is list else …

Total answers: 1

What is the difference between flatten and ravel functions in numpy?

What is the difference between flatten and ravel functions in numpy? Question: import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two different functions …

Total answers: 3