python 中列表与树状结构的转换

简介: python 中列表与树状结构的转换

前言

我们再 html 端json 以及 mongodb 存储的时候 ,经常会遇到 json的嵌套树状结构,但是在 python 处理数据时 ,list 有很方便,两者的转换通常是难以避免的


而且,很多时候,我们需要把一个 list 数据嵌入到一个 树状结构中去。

所以实现了,这个把 list 作为一条路径嵌入 dict 中的方法

数组结构与树状结构

数组结构 = [ [中国,河南,鹤壁] ,

[中国,河南,郑州] ,

[中国,北京,朝阳] ,

]

树状结构 = { 中国 :{ 河南 :{ 鹤壁,郑州 } , 北京 :{ 朝阳 } } }


python 实现

将一个数组元素添加到,树状结构中

# 把一个 路径集合 变成一个 树状字典
# list 转 dict
class MyTree:
    def __init__(self):
        self.tree={}
    # onepoint 是 list
    def append_Point_to_tree(self,  onepoint):
        nowPositon = self.tree
        index = 0
        while index < len(onepoint):
            if nowPositon.__contains__(onepoint[index]):
                nowPositon = nowPositon[onepoint[index]]
                index += 1
            else:
                #创建新节点
                nowPositon[onepoint[index]] = {}
        return self.tree
  # 把【 【路径1 a,b,c,d】 ,【路径2】 】
  # 多条路径一次性插入到树中
    def insert_list_to_tree(self, pointlist):
        for onepoint in pointlist:
            self.append_Point_to_tree(onepoint)


如何把一棵树还原成一个list

# 树状字典转  路由 list
# 遍历树
def traverse_tree(onedict):
    data_list=[]
    start_list=list(onedict.keys())
    deep_list =[0 for item in start_list]
    temp_dict={}
    now_deep=0
    now_dict=onedict
    while start_list!=[]:
        one_key=start_list.pop()
        now_deep=deep_list.pop()
        if now_dict[one_key]!={}:
            now_dict = now_dict[one_key]
            temp_dict[now_deep]=one_key
            for one_key2 in now_dict:
                start_list.append(one_key2)
                deep_list.append(now_deep+1)
        else:
            temp_dict[now_deep]=one_key
            # one_key = start_list.pop()
            data_list.append(list(temp_dict.values()))
            if deep_list==[]:
                break
            now_deep = deep_list[-1]
            # print(temp_dict)
            # print(one_key)
            # print(now_deep)
            now_dict=onedict
            for i in range(now_deep):
                now_dict = now_dict[temp_dict[i]]
            # now_dict=now_dict[one_key]
            # time.sleep(1)
  return data_list
#    for item in data_list:
#        print(item)
#    print(len(data_list))


相关文章
|
5天前
|
索引 Python
Python 中寻找列表最大值位置的方法
本文介绍了Python中找列表最大值及其位置的三种方法:1) 使用内置`max()`和`index()`函数;2) 通过循环遍历;3) 利用`enumerate()`函数和生成器表达式。每种方法均附有示例代码,其中`enumerate()`方法在保证效率的同时代码更简洁。
29 2
|
5天前
|
存储 运维 数据挖掘
Python列表中每个元素前面连续重复次数的数列统计
Python列表中每个元素前面连续重复次数的数列统计
13 1
|
5天前
|
存储 JSON 数据库
Python中列表数据的保存与读取:以txt文件为例
Python中列表数据的保存与读取:以txt文件为例
18 2
|
2天前
|
数据采集 数据挖掘 Python
10个python小技巧,优雅地书写人生_python列表遍历奇数偶数
10个python小技巧,优雅地书写人生_python列表遍历奇数偶数
|
2天前
|
存储 索引 Python
【python学习】列表、元组、字典、集合,秋招是不是得到处面试
【python学习】列表、元组、字典、集合,秋招是不是得到处面试
|
5天前
|
存储 机器学习/深度学习 数据可视化
基于Python的数据分组技术:将数据按照1, 2, 3规则分为三个列表
基于Python的数据分组技术:将数据按照1, 2, 3规则分为三个列表
9 1
|
5天前
|
数据挖掘 计算机视觉 Python
Python实现对规整的二维列表中每个子列表对应的值求和
Python实现对规整的二维列表中每个子列表对应的值求和
11 0
|
5天前
|
存储 数据采集 数据可视化
Python列表到Excel表格第一列的转换技术详解
Python列表到Excel表格第一列的转换技术详解
9 0
|
5天前
|
Python
【Python操作基础】——列表操作
【Python操作基础】——列表操作
|
5天前
|
索引 Python
Python中的列表、元组和字典各具特色
【5月更文挑战第11天】Python中的列表、元组和字典各具特色:列表是可变的,元组不可变,字典亦可变;列表和元组有序,字典无序(但在Python 3.7+保持插入顺序);元素类型上,列表和元组元素任意,字典需键不可变;列表用方括号[],元组用圆括号(),字典用大括号{}表示。列表不适合作字典键,元组可以。选择数据结构应依据实际需求。
22 2