list转map树状结构
时间: 2024-03-16 15:39:43 浏览: 29
将列表转换为树状结构的Map可以通过递归的方式实现。下面是一个示例代码:
```python
def list_to_tree(lst):
tree = {}
for item in lst:
current_node = tree
for key in item[:-1]:
current_node = current_node.setdefault(key, {})
current_node[item[-1]] = {}
return tree
```
这个函数接受一个列表作为输入,列表中的每个元素都是一个由键组成的路径,最后一个键是节点的值。函数会遍历列表中的每个元素,并将其转换为树状结构的Map。
例如,给定以下列表:
```python
lst = [
['A', 'B', 'C'],
['A', 'B', 'D'],
['A', 'E'],
['F']
]
```
调用`list_to_tree(lst)`会返回以下树状结构的Map:
```python
{
'A': {
'B': {
'C': {},
'D': {}
},
'E': {}
},
'F': {}
}
```
相关问题
js列表结构转树状结构详细步骤
将列表结构转换为树状结构的具体步骤如下:
1. 遍历列表结构,获取每一个节点的 id 和 parentId(也就是其父节点的 id)。
2. 将这些节点按照 parentId 进行分组,得到一个以 parentId 为键,节点数组为值的对象。
3. 从根节点开始,递归遍历这个对象,将每个节点的子节点数组设置为这个对象中以该节点 id 为 parentId 的节点数组。
4. 返回根节点即可。
下面是一个示例代码实现:
```javascript
function listToTree(list) {
const map = {};
const roots = [];
// 遍历列表,生成以 id 为键,节点为值的对象
for (const node of list) {
map[node.id] = { ...node, children: [] };
}
// 将节点按照 parentId 进行分组
for (const node of Object.values(map)) {
if (node.parentId) {
map[node.parentId].children.push(node);
} else {
roots.push(node);
}
}
// 递归设置子节点数组
function setChildren(node) {
for (const child of node.children) {
setChildren(child);
}
if (!node.children.length) {
delete node.children;
}
}
// 从根节点开始递归
for (const root of roots) {
setChildren(root);
}
return roots.length > 1 ? { roots } : roots[0];
}
```
在这个示例代码中,假设传入的列表结构为一个数组,每个节点都有一个 id 和 parentId 属性,以及其他属性。函数返回一个树状结构,如果有多个根节点,则返回一个包含这些根节点的对象。
java id pid,JAVA8 两行代码实现ID-PID转树状结构
假设ID和PID分别为int类型的属性,可以使用Java 8的流式编程和Lambda表达式来实现ID-PID转树状结构的操作。
首先,定义一个节点类,包含ID和PID属性以及一个子节点列表:
```java
class TreeNode {
int id;
int pid;
List<TreeNode> children;
TreeNode(int id, int pid) {
this.id = id;
this.pid = pid;
this.children = new ArrayList<>();
}
}
```
接下来,假设我们已经有一个包含所有节点的列表,可以使用以下代码将它们转换成树状结构:
```java
List<TreeNode> nodes = ...; // 所有节点列表
// 将节点按照PID分组
Map<Integer, List<TreeNode>> nodeMap = nodes.stream().collect(Collectors.groupingBy(node -> node.pid));
// 递归构建树状结构
TreeNode root = nodes.stream()
.filter(node -> node.pid == 0) // 找到根节点
.findFirst()
.map(rootNode -> buildTree(rootNode, nodeMap))
.orElseThrow(() -> new IllegalStateException("No root node found"));
// 递归函数,将一个节点及其子节点构建成树状结构
private static TreeNode buildTree(TreeNode node, Map<Integer, List<TreeNode>> nodeMap) {
List<TreeNode> children = nodeMap.getOrDefault(node.id, Collections.emptyList());
node.children.addAll(children);
node.children.forEach(child -> buildTree(child, nodeMap));
return node;
}
```
以上代码首先将节点列表按照PID分组,然后找到根节点(PID为0),递归地将根节点及其子节点构建成树状结构。最终返回的是根节点。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)