Python - create tree from dictionary - python

I have a problem, I must create a function that create a tree from a dictionary of any numbers of elements.
Example of dictionaries:
D1 = {'name': 'musica',
'children': [
{'name': 'rock',
'children': [
{'name': 'origini', 'children': []},
{'name': 'rock&roll', 'children': []},
{'name': 'hard rock', 'children': []}]},
{'name': 'jazz',
'children': [
{'name': 'origini',
'children': [{'name': '1900',
'children': [{'name': 'origini', 'children': []}]}]},
{'name': 'ragtime', 'children': []},
{'name': 'swing', 'children': []}]}]}
D2 = {'name': 'html',
'children': [
{'name': 'head',
'children': [
{'name': 'meta', 'children': []},
{'name': 'title', 'children': []},
{'name': 'style', 'children': []}]},
{'name': 'body',
'children': [
{'name': 'h1', 'children': []},
{'name': 'section',
'children': [
{'name': 'p',
'children': [
{'name': 'strong', 'children': []},
{'name': 'b', 'children': []},
{'name': 'em', 'children': []},
{'name': 'i', 'children': []}]},
{'name': 'p',
'children': [
{'name': 'q', 'children': []},
{'name': 'code', 'children': []},
{'name': 'kbd', 'children': []}]},
{'name': 'p',
'children': [
{'name': 'sup', 'children': []},
{'name': 'sub', 'children': []}]},
{'name': 'p',
'children': [
{'name': 'span', 'children': []}]}]},
{'name': 'footer',
'children': [
{'name': 'a',
'children': [
{'name': 'strong', 'children': []}]},
{'name': 'a',
'children': [
{'name': 'strong', 'children': []}]}]}]}]}
D3 = {'name': 'Giovanni di Bicci',
'children': [
{'name': 'Cosimo il vecchio',
'children': [
{'name': 'Piero il gottuso',
'children': [
{'name': 'Lorenzo il magnifico',
'children': [
{'name': 'Piero II',
'children': [
{'name': 'Lorenzo II', 'children': []}]},
{'name': 'Papa Leone X', 'children': []},
{'name': 'Giuliano', 'children': []}]}]},
{'name': 'Giovanni Carlo', 'children': []}]},
{'name': 'Lorenzo',
'children': [
{'name': 'Pierfrancesco',
'children': [
{'name': 'Lorenzo', 'children': []},
{'name': 'Giovanni',
'children': [
{'name': 'Giovanni dalle Bande Nere',
'children': [
{'name': 'Lorenzino', 'children': []},
{'name': 'Cosimo I',
'children': [
{'name': 'Francesco I',
'children': [
{'name': 'Maria', 'children': []}]},
{'name': 'Ferdinando I',
'children': {}}]}]}]}]}]}]}
Any solution?
Thanks a lot
ADDITION
Thank you all for the answers.
Now I write the full exercise so you can better understand and answer me: Actually I have implemented a class TNode made in these methods:
class TNode(object):
def __init__(self, name, Sinistra= None, Destra= None):
self._name = name
self.Destra = Destra
self.Sinistra = Sinistra
self._children = []
self._copy = []
self.c = c
def add(self, c):
self._children.append(c)
def children(self):
self._copy = self._children
return self._copy
def height(self):
h = 1
for node in self._children:
h = max(h, node.height() + 1)
return h
def count(self):
c = 1
for node in self._children:
c += node.count()
return c
def count_by_name(self, name):
lst = []
if self._name == name:
lst += [self]
for node in self._children:
lst += node.count_by_name(name)
return lst
def leaves(self):
leaves_s = []
if not self._children:
leaves_s.append(self.c)
for node in self._children:
leaves_s.update(node.leaves())
return len(leaves_s)
def paths(self, name):
paths_s = set()
if self._name == name:
paths_s.add((name,))
for node in self._children:
for j in node.paths(name):
paths_s.add((self._name,)+j)
return paths_s
I also need to create a function create_tree(d) that, taken a Dictionary "d" that represents a tree, creates the corresponding tree with nodes of type TNode and returns the root. The function must add the children in the same order as they are listed in the lists of the keys 'children'.
Sorry if initially I did not write all that.
I fail to create the function, referred to the class, that create a tree from a dictionary.
I use Python 2.7
Thanks.

You can create a tree with defaultdict:
from collections import defaultdict
def Tree():
return defaultdict(Tree)
Then using it:
>>> tree = Tree()
>>> tree['house']['car']['red']['hubcap'] = 1950

Related

Dataframe to Nested Dictionaries in Python

Having a bit of trouble here.. I need to take a dataframe
import pandas as pd
region = ['A','A','A','B','B','B']
sub_region = ['1','2','2','3','3','4']
state = ['a','b','c','d','e','f']
pd.DataFrame({"region":region,"sub_region":sub_region,"state":state})
and convert into a nested dictionary with the following format:
[{name: "thing", children: [{name:"sub_thing",children:[{...}] }]}]
so a list of nested dictionaries where the key value pairs are always name:"", children:[{}], but childless children don't have children in their dict.. so the final desired output would be...
[{"name":"A",
"children":[{"name":"1","children":[{"name":"a"}]},
{"name":"2","children":[{"name":"b"},{"name":"c"}]}]
},
{"name":"B",
"children":[{"name":"3","children":[{"name":"d"},{"name":"e"}]},
{"name":"4","children":[{"name":"f"}]}]
}
]
Assume a generalized framework where the number of levels can vary.
I don't think you can do better than looping through the rows of the dataframe. That is, I don't see a way to vectorize this process. Also, if the number of levels can vary within the same dataframe, then the update function should be modified to handle nan entries (e.g. adding and not np.isnan(row[1]) to if len(row) > 1).
That said, I believe that the following script should be satisfactory.
import pandas as pd
region = ['A','A','A','B','B','B']
sub_region = ['1','2','2','3','3','4']
state = ['a','b','c','d','e','f']
df = pd.DataFrame({"region":region,"sub_region":sub_region,"state":state})
ls = []
def update(row,ls):
for d in ls:
if d['name'] == row[0]:
break
else:
ls.append({'name':row[0]})
d = ls[-1]
if len(row) > 1:
if not 'children' in d:
d['children'] = []
update(row[1:],d['children'])
for _,r in df.iterrows():
update(r,ls)
print(ls)
The resulting list ls:
[{'name': 'A',
'children': [{'name': '1', 'children': [{'name': 'a'}]},
{'name': '2', 'children': [{'name': 'b'}, {'name': 'c'}]}]},
{'name': 'B',
'children': [{'name': '3', 'children': [{'name': 'd'}, {'name': 'e'}]},
{'name': '4', 'children': [{'name': 'f'}]}]}]
Here's a version where childless children have 'children':[] in their dict, which I find a bit more natural.
import pandas as pd
region = ['A','A','A','B','B','B']
sub_region = ['1','2','2','3','3','4']
state = ['a','b','c','d','e','f']
df = pd.DataFrame({"region":region,"sub_region":sub_region,"state":state})
ls = []
def update(row,ls):
if len(row) == 0:
return
for d in ls:
if d['name'] == row[0]:
break
else:
ls.append({'name':row[0], 'children':[]})
d = ls[-1]
update(row[1:],d['children'])
for _,r in df.iterrows():
update(r,ls)
print(ls)
The resulting list ls:
[{'name': 'A',
'children': [{'name': '1', 'children': [{'name': 'a', 'children': []}]},
{'name': '2',
'children': [{'name': 'b', 'children': []},
{'name': 'c', 'children': []}]}]},
{'name': 'B',
'children': [{'name': '3',
'children': [{'name': 'd', 'children': []},
{'name': 'e', 'children': []}]},
{'name': '4', 'children': [{'name': 'f', 'children': []}]}]}]

Multiple Nested Dictionaries into Dataframe as one single row

I am working on a nested dictionary and not able to put it in a dataframe.
This is what it looks like:
{'props': {'initialProps': {'statusCode': 0, '$isMobile': False, '$isIOS': None, '$isAndroid': False, '$host': 'www.tiktok.com', '$pageUrl': '/#alanwalkermusic/video/6816594007829859589', '$language': 'en', '$originalLanguage': 'en', '$languageList': [{'value': 'id', 'alias': 'id-ID', 'label': 'Bahasa Indonesia', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'de', 'alias': 'de-DE', 'label': 'Deutsch', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'en', 'alias': 'en', 'label': 'English', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'es', 'alias': 'es', 'label': 'Español', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'fr', 'alias': 'fr', 'label': 'Français', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'it', 'alias': 'it-IT', 'label': 'Italiano', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pl', 'alias': 'pl-PL', 'label': 'Polski', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pt_BR', 'alias': 'pt-BR', 'label': 'Português', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'vi', 'alias': 'vi-VN', 'label': 'Tiếng Việt', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'tr', 'alias': 'tr-TR', 'label': 'Türkçe', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ru', 'alias': 'ru-RU', 'label': 'Русский', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'hi', 'alias': 'hi-IN', 'label': 'हिन्दी', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ko', 'alias': 'ko-KR', 'label': '한국어', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ja', 'alias': 'ja-JP', 'label': '日本語', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'zh_Hant', 'alias': 'zh-Hant-TW', 'label': '繁體中文', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ar', 'alias': 'ar', 'label': 'العربية', 'children': [{'value': 'default', 'label': ''}]}], '$region': 'DE', '$appId': 1233, '$os': 'windows', '$baseURL': 'm.tiktok.com', '$downloadLink': {'amazon': {'visible': True, 'normal': 'https://www.amazon.com/dp/B0117U0G3M/'}, 'google': {'visible': True, 'normal': 'https://www.tiktok.com/download-link/af/com.zhiliaoapp.musically'}, 'apple': {'visible': True, 'normal': 'https://www.tiktok.com/download-link/af/id835599320'}}, '$abTestVersion': {'clientParameters': '{}', 'clientVersionName': '', 'versionName': '1618717', 'parameters': '{}', 'startTime': '', 'endTime': ''}, '$appType': 'm', '$gray': {'upload': False, 'tea': False}, '$reflowType': 't', '$legalList': [{'title': 'TikTok.com Cookies Policy', 'key': 'tiktok-website-cookies-policy', 'href': 'https://www.tiktok.com/legal/tiktok-website-cookies-policy?lang=en'}, {'title': 'Open Source', 'key': 'open-source', 'href': 'https://www.tiktok.com/legal/open-source?lang=en'}, {'title': 'Virtual Items', 'key': 'virtual-items', 'href': 'https://www.tiktok.com/legal/virtual-items?lang=en'}, {'title': 'Intellectual Property Policy', 'key': 'copyright-policy', 'href': 'https://www.tiktok.com/legal/copyright-policy?lang=en'}, {'title': 'Law Enforcement', 'key': 'law-enforcement', 'href': 'https://www.tiktok.com/legal/law-enforcement?lang=en'}, {'title': 'Privacy Policy', 'key': 'privacy-policy', 'href': 'https://www.tiktok.com/legal/privacy-policy?lang=en'}, {'title': 'Terms of Service', 'key': 'terms-of-use', 'href': 'https://www.tiktok.com/legal/terms-of-use?lang=en'}], '$botType': 'others', '$config': {'covidBanner': {'open': True, 'url': 'https://www.tiktok.com/safety/resources/covid-19', 'background': 'rgba(125,136,227,1)'}}, '$wid': '6819614602332063233'}, 'pageProps': {'serverCode': 200, 'pageState': {'regionAppId': 1233, 'os': 'windows', 'region': 'DE', 'baseURL': 'm.tiktok.com', 'appType': 't', 'fullUrl': 'https://www.tiktok.com/node/share/video/#alanwalkermusic/6816594007829859589'}, 'videoData': {'itemInfos': {'id': '6816594007829859589', 'video': {'urls': ['https://v19.muscdn.com/afefda4362dccd13b93a4d8e79ab5338/5ea477c7/video/tos/useast2a/tos-useast2a-ve-0068c004/7f922f16b9534b80be6523cbb8ba2ce2/?a=1233&br=2652&bt=1326&cr=0&cs=0&dr=0&ds=3&er=&l=202004251147400101150770371E5160EE&lr=tiktok_m&qs=0&rc=MzY3cXc1OjxwdDMzNjczM0ApNDtnNTY1Ojw2N2k8NDU4ZWdoZXBmLjE0ZGBfLS0vMTZzczZjYDEvL2BgMDAzXy0uMC86Yw%3D%3D&vl=&vr='], 'videoMeta': {'width': 540, 'height': 960, 'ratio': 11, 'duration': 11}}, 'covers': ['https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/8187e5fe9a0f684529910363f5dbe428'], 'authorId': '84453035275386880', 'coversOrigin': ['https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/7dd18717d8aa4916abc5699c25c09b3e_1587111976'], 'text': 'I’m that friend who can’t stand still when taking photos', 'commentCount': 300, 'diggCount': 14145, 'playCount': 91447, 'shareCount': 29, 'createTime': '1587111974', 'isActivityItem': False, 'warnInfo': [], 'liked': False, 'commentStatus': 0, 'showNotPass': False}, 'authorInfos': {'verified': True, 'secUid': 'MS4wLjABAAAAhS4j-WG_Zq5WJfdV35QEqorNPzc_kkFVNPQmGM6mQIWEg6FOgjS-Dl9eISlYvPDc', 'uniqueId': 'alanwalkermusic', 'userId': '84453035275386880', 'nickName': 'Alan Walker', 'covers': ['https://p16.muscdn.com/img/musically-maliva-obj/1649466056705029~c5_100x100.jpeg']}, 'musicInfos': {'musicId': '6805465027164768258', 'musicName': 'Heading Home', 'authorName': 'Alan Walker, Ruben', 'covers': ['https://p16.muscdn.com/img/musically-maliva-obj/1649466056705029~c5_100x100.jpeg']}, 'authorStats': {'followerCount': 2147815, 'heartCount': '10903873'}, 'challengeInfoList': [], 'duetInfo': '0', 'textExtra': []}, 'shareUser': {'secUid': '', 'userId': '', 'uniqueId': '', 'nickName': '', 'signature': '', 'covers': [], 'coversMedium': [], 'coversLarger': [], 'isSecret': False}, 'shareMeta': {'title': 'Alan Walker on TikTok', 'desc': 'I’m that friend who can’t stand still when taking photos', 'image': {'url': 'http://p16-va-default.akamaized.net/img/tos-maliva-p-0068/8187e5fe9a0f684529910363f5dbe428~tplv-tiktok-play.image', 'width': 540, 'height': 960}}, 'statusCode': 0, 'langList': [{'value': 'id', 'alias': 'id-ID', 'label': 'Bahasa Indonesia', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'de', 'alias': 'de-DE', 'label': 'Deutsch', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'en', 'alias': 'en', 'label': 'English', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'es', 'alias': 'es', 'label': 'Español', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'fr', 'alias': 'fr', 'label': 'Français', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'it', 'alias': 'it-IT', 'label': 'Italiano', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pl', 'alias': 'pl-PL', 'label': 'Polski', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'pt_BR', 'alias': 'pt-BR', 'label': 'Português', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'vi', 'alias': 'vi-VN', 'label': 'Tiếng Việt', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'tr', 'alias': 'tr-TR', 'label': 'Türkçe', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ru', 'alias': 'ru-RU', 'label': 'Русский', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'hi', 'alias': 'hi-IN', 'label': 'हिन्दी', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ko', 'alias': 'ko-KR', 'label': '한국어', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ja', 'alias': 'ja-JP', 'label': '日本語', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'zh_Hant', 'alias': 'zh-Hant-TW', 'label': '繁體中文', 'children': [{'value': 'default', 'label': ''}]}, {'value': 'ar', 'alias': 'ar', 'label': 'العربية', 'children': [{'value': 'default', 'label': ''}]}], 'webId': '6819614602168567298', 'requestId': '23892479587815260433', 'videoObjectPageProps': {'videoProps': {'url': 'https://www.tiktok.com/#alanwalkermusic/video/6816594007829859589', 'name': 'Alan Walker(#alanwalkermusic) on TikTok I’m that friend who can’t stand still when taking photos', 'description': 'Alan Walker(#alanwalkermusic) has created a short video on TikTok with music Heading Home. I’m that friend who can’t stand still when taking photos', 'keywords': 'alanwalkermusic, Alan Walker,', 'thumbnailUrl': ['https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/8187e5fe9a0f684529910363f5dbe428', 'https://p16-va-default.akamaized.net/obj/tos-maliva-p-0068/7dd18717d8aa4916abc5699c25c09b3e_1587111976'], 'uploadDate': '2020-04-17T08:26:14.000Z', 'contentUrl': 'https://v19.muscdn.com/afefda4362dccd13b93a4d8e79ab5338/5ea477c7/video/tos/useast2a/tos-useast2a-ve-0068c004/7f922f16b9534b80be6523cbb8ba2ce2/?a=1233&br=2652&bt=1326&cr=0&cs=0&dr=0&ds=3&er=&l=202004251147400101150770371E5160EE&lr=tiktok_m&qs=0&rc=MzY3cXc1OjxwdDMzNjczM0ApNDtnNTY1Ojw2N2k8NDU4ZWdoZXBmLjE0ZGBfLS0vMTZzczZjYDEvL2BgMDAzXy0uMC86Yw%3D%3D&vl=&vr=', 'embedUrl': 'https://www.tiktok.com/embed/v2/6816594007829859589', 'commentCount': '300', 'duration': 'PT11S', 'audio': {'name': 'Heading Home - Alan Walker, Ruben', 'author': 'Alan Walker, Ruben', 'mainEntityOfPage': {'#type': 'ItemPage', '#id': 'https://www.tiktok.com/music/Heading-Home-6805465027164768258'}}, 'creator': {'#type': 'Person', 'name': 'Alan Walker', 'alternateName': 'alanwalkermusic', 'url': 'https://www.tiktok.com/#alanwalkermusic', 'interactionStatistic': [{'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/LikeAction'}, 'userInteractionCount': '10903873'}, {'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/FollowAction'}, 'userInteractionCount': 2147815}]}, 'width': 540, 'height': 960, 'interactionStatistic': [{'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/WatchAction'}, 'userInteractionCount': 91447}, {'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/LikeAction'}, 'userInteractionCount': 14145}, {'#type': 'InteractionCounter', 'interactionType': {'#type': 'http://schema.org/ShareAction'}, 'userInteractionCount': 29}]}, 'pageProps': {'type': 'ItemPage', 'id': 'https://www.tiktok.com/#alanwalkermusic/video/6816594007829859589'}, 'breadcrumbProps': {'urlList': [{'name': 'TikTok', 'url': 'https://www.tiktok.com'}, {'name': 'Alan Walker(#alanwalkermusic) Official | TikTok', 'url': 'https://www.tiktok.com/#alanwalkermusic'}, {'name': 'Alan Walker(#alanwalkermusic) on TikTok I’m that friend who can’t stand still when taking photos', 'url': 'https://www.tiktok.com/#alanwalkermusic/video/6816594007829859589'}]}}, 'metaParams': {'title': 'Alan Walker(#alanwalkermusic) on TikTok I’m that friend who can’t stand still when taking photos', 'keywords': 'alanwalkermusic, Alan Walker,', 'description': 'Alan Walker(#alanwalkermusic) has created a short video on TikTok with music Heading Home. I’m that friend who can’t stand still when taking photos', 'robotsContent': 'index, follow'}, 'isSSR': True, 'pageOptions': {'header': {'showUpload': True, 'type': 'webapp'}, 'headOptions': None}}, 'pathname': '/share/video'}, 'page': '/share/video', 'query': {'uniqueId': 'alanwalkermusic', 'id': '6816594007829859589', '$initialProps': {'$wid': '6819614602332063233'}, 'webtoken': 'tac=\'i+2gv2ay5eehis!i#piis"yZl!%s"l"u&kLs#l l#vr*charCodeAtx0[!cb^i$1em7b*0d#>>>s j\\uffeel s#0y\\xe8g,&qnfme|ms l vk.}l ,(dfijxdaamvk5}l ,(dfijxdaam,$lwcamk\\xadl ,(dfijxdaam,$lwcams!l!v,\\\'nfmosCkmx,*~bgyad>r}~[!c0b<k$t jql!v,\\\'nfmosCkmx,%cokm3[!c0d#===k$t jMl!v,\\\'nfmosCkmx,0xefc.:9&*.4+2-0.[!c0d#===k"t f z[ cb|1d<y\\u01ea,(|fY\\x7f~d`hs g,(lfi~ah`{ms!g,&qnfme|ms"g,)gk}ejo{\\x7fcms#,)|doikgauus$ul"d\\\',typeofl$d#===v!kA}l"vl mx[ c,/T\\x7fsxvwa6#qw~tk#d#!==v!k\\\\}gr&Object,)yxdxbzv`tml mvr$callxl"[!c,/T\\x7fsxvwa6#qw~tk#d#!==v!k4}ul!d\\\',typeofl$d#===v!kG}l!vl mx[ cv,\\\'nfmosCkmx,(Lfi~ah`{[!c0b<v!k4}ul#d\\\',typeofl$d#===v!kD}l#vl mx[ c,2I|v\\x7fstl9Tzjty~TNP~d#!==v!k=}ugr(locationd\\\',typeofl$d#===k"t fv!k4}l!,,hbmz}t|gYzrrm!!!kjg,\\\'oaz~d~tms%ul%d\\\',typeofl$d#===v!kB}l%vl mx[ c,0K~pyqvb7PpiosogBd#!==k"t f z[ cb|1d<y\\xe2g,&qnfme|ms l vk-}l ,\\\'dggyd`hmvk7}l ,\\\'dggyd`hm,\\\'aa{oiyjmk"t ugr.InstallTriggerd\\\',typeof,)|doikgauud#!==kwl vkn}l ,*e~xh|Xyuf{ml ,*cebh|Xyuf{mb-\\x94b>v!kF}l ,+dyyk}Xt{t|aml ,+bbck}Xt{t|amb-\\x94b>k"t f z[ cb|1d<y\\xa0g,&akgkkgms g,&Iebli\\x7fms!ul d\\\',typeof,)|doikgauud#!==vkh}l!,)yxdxbzv`tm,(|fY\\x7f~d`hmvr$callxl ,\\\'wzfin\\x7f~m[!c,0K~pyqvb7hkuxynmBd#=== z[ cb|1d<y\\u011eg,(lfi~ah`{ms g,)gk}ejo{\\x7fcms!g,&qnfme|ms"fv!k4}l ,,hbmz}t|gYzrrm!!!k\\xd7,\\\'wd|mbb~l!d"in!v!kI}l!,\\\'wd|mbb~mg,+[`xif~P`aulmd*instanceof!v!k1},(Wybjbyabl"d"inv!k4},+hmab_xp|g{xl"d"inv!k4},+TScghxe\\x7frfpl"d"inv!kP},%Dscafl"d"in,8[xtm}nLzNEGQMKAdGG^NTY\\x1ckl"d"inb< f z[ cb|1d<y\\u02a1g,&qnfme|ms g,)gk}ejo{\\x7fcms!g,&Iebli\\x7fms"l!,)~oih\\x7fgyucmk"t (\\x80,.jjvx|vDgyg}knbl"d"inkfl"v,.jjvx|vDgyg}knbmxl!,)~oih\\x7fgyucgr&Objectn vuq%valuevfq(writable[#c}) %{s#t ,4KJarz}hrjxl#EWCOQDRB,3LKfs{}wsnqB{iAMWBP#,;DCj{}DSKUAWyTK[C[XrHZ^RFZ[[,7HGn\\x7fyxowiES}PGWOW\\\\vL^BN,5JI`}{~iuk{m\\x7fRAQMURxNG,3LKsnsjpl~nB{iAMWBP#,2MLpg\\x7fa}kEnrjl~PQGG,5JI`}{~iuk{m\\x7fTLTVDVWMM,1NMwf|`rjF\\x7fm}qk~TD,4KJert|tripAjNVPBTUCC,4KJpo|ksmyoAjNVPBTUCC[+s#,)Vyn`h`fe|,,olbcCt~vz|cz,6ID}u\\x7fuuhs#ieg|v#EHZMOY[#s$l$*%s%l%u&k4s&l$l&ms\\\'l l\\\'mk"t j\\x06l#*%s%l%u&k?s&l#l&ms\\\'l ,(lfi~ah`{ml\\\'mk"t j\\ufffbl ,(lfi~ah`{m*%s%l%u&kls&l&vr%matchxgr&RegExp$*\\\\$[a-z]dc_$ n"[!cvk:}l ,(lfi~ah`{ml&m,&efkaoTmk"t j\\uffcef z[ cb|1d<y\\u024fg,&qnfme|ms gr&RegExp$+constructor$!in"vr$testxl ,+CX#BJ|t\\x7fvzam[!cv!k\\xb2}yZl vr(toStringx[ c,AzMAN#ES\\x08zKMM_G}U\\\\]GQ{YCQ_SX]IWP.\\x1cd#=== l ,&ufnhxbm!v!kd}ugr&safarid\\\',typeof,)|doikgauud#!==vk=}gr&safari,0`da{Zzb~~pyzhtqqm&k\\u010e(=l v,,c}kaTpfrvtermxzzzz[$c}) %{s!t (\\x85l ,.}jcb{|zFbxjx}~mvr\\\'setItemx,+xc`kDuhZvfp, ["c}l ,.}jcb{|zFbxjx}~mvr*removeItemx,+xc`kDuhZvfp[!c}) \\x7f{s!l!,$gjbbmgr,DOMException,2CF[AWH]AY^YY[[\\x7fdpqmd#===vkC}l ,.}jcb{|zFbxjx}~m,&jbfn~cm0d#===k"t f fv!k>}g,(lfi~ah`{m,,hbmz}t|gYzrrm!!k`l ,)`doiukkTSm!vkJ}l ,,\\\\bgadt`Vbpxcmv!k4}l ,.C\\\\#~{}`pdRn|tomk"t f z[ cb|1d<y\\u0165g,(lfi~ah`{ms g,)gk}ejo{\\x7fcms!,(|fY\\x7f~d`hs",\\\'nfmosCks#fv!k4}l ,,hbmz}t|gYzrrm!!!k\\u0113l v,-n|jqewVxp{rvmmx,&eff\\x7fkx[!cs$l$,)}eOmyoZB]mvl"mx[ cv,\\\'umyfjohmxgr&RegExp$#\\\\s*$!gn"$ ["cvl#mx,*djxdxjs~vv[!c0b<v!kh}l!l"mvl"mx[ cvr\\\'replacexgr&RegExp$#\\\\s*$!gn"$ ["cvl#mx,*djxdxjs~vv[!c0b<v!kP}l!,\\\'wd|mbb~mvl"mx[ c,4Ozt}}zn;LqkxIOcQVD_zd#!== f z[ cb|1d<1b|\'', '0fea6a13c52b4d4725368f24b045ca84': {}, 'aa59d67c2123f094d0d6798ffe651c4d': {}}, 'buildId': '1.0.1.1363', 'assetPrefix': '//s16.tiktokcdn.com/tiktok/falcon', 'isFallback': False, 'customServer': True}
I can reach any values by writing so:
import json
parsed = json.loads(source_code)
props = parsed['props']['pageProps']['videoData']
follower = props['authorStats']['followerCount']
heartcount = props['authorStats']['heartCount']
AuthorID = props['itemInfos']['authorId']
Commentcount = pandas.DataFrame(props['itemInfos']['commentCount']
CreateTime = props['itemInfos']['createTime']
But I was not able to put all of them into a dataframe with one row and all of those follower,heartcount etc as columns.
Thank you for your help!

Get the number of dict values in a nested dict

I have the following json object:
[{
'firstname': 'Jimmie',
'lastname': 'Barninger',
'zip_code': 12345,
'colors': ['2014-01-01', '2015-01-01'],
'ids': {
'44': 'OK',
'51': 'OK'
},
'address': {
'state': 'MI',
'town': 'Dearborn'
},
'other': {
'ids': {
'1': 'OK',
'103': 'OK'
},
}
}, {
'firstname': 'John',
'lastname': 'Doe',
'zip_code': 90027,
'colors': None,
'ids': {
'91': 'OK',
'103': 'OK'
},
'address': {
'state': 'CA',
'town': 'Los Angeles'
},
'other': {
'ids': {
'91': 'OK',
'103': 'OK'
},
}
}]
I would like to be able to get the number of unique key values that each dict has. In the above, the number would be:
address: 2 # ['state', 'town']
ids: 4 # ['44', '51', '91', '103']
other.ids 3 # ['1', '103', '91']
I've been having trouble iterating of the objects to figure this out, especially if there is an item within a list. What I've been trying thus far is something like the below, though it doesn't currently work I'm pasting it for reference:
def count_per_key(obj, _c=None):
if _c is None: unique_values_per_key = {}
if isinstance(obj, list):
return [count_per_key(l) for l in obj]
elif not isinstance(obj, dict):
pass
else:
for key, value in obj.items():
if not isinstance(value, dict):
continue
elif isinstance(value, dict):
if key not in unique_values_per_key: unique_values_per_key[key] = set()
unique_values_per_key[key].union(set(value.keys()))
return count_per_key(value)
elif isinstance(value, list):
return [count_per_key(o) for o in value]
return unique_values_per_key
You can use recursion with a generator:
from collections import defaultdict
d = [{'firstname': 'Jimmie', 'lastname': 'Barninger', 'zip_code': 12345, 'colors': ['2014-01-01', '2015-01-01'], 'ids': {'44': 'OK', '51': 'OK'}, 'address': {'state': 'MI', 'town': 'Dearborn'}, 'other': {'ids': {'1': 'OK', '103': 'OK'}}}, {'firstname': 'John', 'lastname': 'Doe', 'zip_code': 90027, 'colors': None, 'ids': {'91': 'OK', '103': 'OK'}, 'address': {'state': 'CA', 'town': 'Los Angeles'}, 'other': {'ids': {'91': 'OK', '103': 'OK'}}}]
def get_vals(d, _path = []):
for a, b in getattr(d, 'items', lambda :{})():
if a in {'ids', 'address'}:
yield ['.'.join(_path+[a]), list(b.keys())]
else:
yield from get_vals(b, _path+[a])
c = defaultdict(list)
results = [i for b in d for i in get_vals(b)]
for a, b in results:
c[a].extend(b)
_r = [[a, set(list(b))] for a, b in c.items()]
new_r = [[a, b, len(b)] for a, b in _r]
Output:
[
['ids', {'91', '44', '51', '103'}, 4],
['address', {'state', 'town'}, 2],
['other.ids', {'1', '91', '103'}, 3]
]
l= [{'firstname': 'Jimmie', 'lastname': 'Barninger', 'zip_code': 12345, 'colors': ['2014-01-01', '2015-01-01'], 'ids': {'44': 'OK', '51': 'OK'}, 'address': {'state': 'MI', 'town': 'Dearborn'}, 'other': {'ids': {'1': 'OK', '103': 'OK'}}}, {'firstname': 'John', 'lastname': 'Doe', 'zip_code': 90027, 'colors': None, 'ids': {'91': 'OK', '103': 'OK'}, 'address': {'state': 'CA', 'town': 'Los Angeles'}, 'other': {'ids': {'91': 'OK', '103': 'OK'}}}]
def find_dicts(d,parent=''):
for k,v in d.items():
if isinstance(v,dict):
if parent is not '':
identifier=str(parent)+'.'+str(k)
else:
identifier=str(k)
yield {identifier:[x for x in v.keys()]}
yield from find_dicts(v,k)
else:
pass
s=[list(find_dicts(d)) for d in l]
dict_names=[list(y.keys())[0] for y in s[0]]
final_dict={name:[] for name in dict_names}
for li in s:
for di in li:
di_key=list(di.keys())[0]
di_values=list(di.values())[0]
for k,v in final_dict.items():
if k == di_key:
for value in di_values:
if value not in final_dict[k]:
final_dict[k].append(value)
for k,v in final_dict.items():
print(k,":",len(v),v)
Output
ids : 4 ['44', '51', '91', '103']
address : 2 ['town', 'state']
other.ids : 3 ['103', '1', '91']
other : 1 ['ids']

Remove duplicates from list of dictionaries within list of dictionaries

I have list:
my_list = [{'date': '10.06.2016',
'account': [{'name': 'a'},
{'name': 'a'},
{'name': 'b'},
{'name': 'b'}]},
{'date': '22.06.2016',
'account': [{'name': 'a'},
{'name': 'a'}]}]
I want to remove duplicates from the list of dictionaries in 'account':
my_list = [{'date': '10.06.2016',
'account': [{'name': 'a'},
{'name': 'b'}]},
{'date': '22.06.2016',
'account': [{'name': 'a'}]}]
When using set, I get the following error:
TypeError: unhashable type: 'dict'
Can anybody help me with this problem?
This structure is probably over complicated, but it gets the job done.
my_list = [{'date': '10.06.2016',
'account': [{'name': 'a'},
{'name': 'a'},
{'name': 'b'},
{'name': 'b'}]},
{'date': '22.06.2016',
'account': [{'name': 'a'},
{'name': 'a'}]}]
>>> [{'date': date,
'account': [{'name': name} for name in group]
} for group, date in zip([set(account.get('name')
for account in item.get('account'))
for item in my_list],
[d.get('date') for d in my_list])]
[{'account': [{'name': 'a'}, {'name': 'b'}], 'date': '10.06.2016'},
{'account': [{'name': 'a'}], 'date': '22.06.2016'}]
def deduplicate_account_names(l):
for d in l:
names = set(map(lambda d: d.get('name'), d['account']))
d['account'] = [{'name': name} for name in names]
# even shorter:
# def deduplicate_account_names(l):
# for d in l:
# d['account'] = [{'name': name} for name in set(map(lambda d: d.get('name'), d['account']))]
my_list = [{'date': '10.06.2016',
'account': [{'name': 'a'},
{'name': 'a'},
{'name': 'b'},
{'name': 'b'}]},
{'date': '22.06.2016',
'account': [{'name': 'a'},
{'name': 'a'}]}]
deduplicate_account_names(my_list)
print(my_list)
# [ {'date': '10.06.2016',
# 'account': [ {'name': 'a'},
# {'name': 'b'} ] },
# {'date': '22.06.2016',
# 'account': [ {'name': 'a'} ] } ]
Sets can only have hashable members and neither lists nor dicts are - but they can be checked for equality.
you can do
def without_duplicates(inlist):
outlist=[]
for e in inlist:
if e not in outlist:
outlist.append(e)
return outlist
this can be slow for really big lists
Give this code a try:
for d in my_list:
for k in d:
if k == 'account':
v = []
for d2 in d[k]:
if d2 not in v:
v.append(d2)
d[k] = v
This is what you get after running the snippet above:
In [347]: my_list
Out[347]:
[{'account': [{'name': 'a'}, {'name': 'b'}], 'date': '10.06.2016'},
{'account': [{'name': 'a'}], 'date': '22.06.2016'}]

Duplicate python dict for each value

In a list containing dictionaries, how do I split it based on unique values of dictionaries? So for instance, this:
t = [
{'name': 'xyz', 'value': ['K','L', 'M', 'N']},
{'name': 'abc', 'value': ['O', 'P', 'K']}
]
becomes this:
t = [
{'name': 'xyz', 'value': 'K'},
{'name': 'xyz', 'value': 'L'},
{'name': 'xyz', 'value': 'M'},
{'name': 'xyz', 'value': 'N'},
{'name': 'abc', 'value': 'O'},
{'name': 'xyz', 'value': 'P'},
{'name': 'xyz', 'value': 'K'}
]
You can do this with a list comprehension. Iterate through each dictionary d, and create a new dictionary for each value in d['values']:
>>> t = [ dict(name=d['name'], value=v) for d in t for v in d['value'] ]
>>> t
[{'name': 'xyz', 'value': 'K'},
{'name': 'xyz', 'value': 'L'},
{'name': 'xyz', 'value': 'M'},
{'name': 'xyz', 'value': 'N'},
{'name': 'abc', 'value': 'O'},
{'name': 'abc', 'value': 'P'},
{'name': 'abc', 'value': 'K'}]

Categories