gpt4 book ai didi

python - 从 python 中的列表列表创建列表

转载 作者:太空宇宙 更新时间:2023-11-04 02:30:16 24 4
gpt4 key购买 nike

我正在创建一个树状结构,其中每个叶节点都有 5 个文档。要获取父节点的文档,子节点的所有文档都将分配给它。

例如A 是父节点,B、C 是子节点,它们各有 5 个文档。因此,A 的文档将是 5+5=10。类似地,A 的父节点将获得 A 的 10 个文档 + A 的兄弟节点的一个文档的编号。我们将重复此操作,直到到达根节点。

我想将 A 的文档存储为大小为 10 的列表,并且类似地,将 A 的父级存储为其子级的文档总数。但它将其存储为大小为 2 的列表,并且在每个列表下,每个都有 5 个文档。 A 的父级还将 A 的文档存储为 3 的列表,而不是我想要的 3*5=15

如何将每个节点的文档存储为文档总数而不是列表列表?下面是我正在使用的代码。

from anytree import Node, RenderTree
import pandas as pd
import numpy as np

class Node(Node):
Node.documents = None
Node.vector = None

### Creating tree by giving documnets to leaf ###
### Tree Creation ###
# L1
Finance = Node("Finance")
# L2
Credit_and_Lending = Node("Credit and Lending", parent=Finance)
# L3
Credit_Cards = Node("Credit Cards", parent=Credit_and_Lending)

Loans = Node("Loans", parent=Credit_and_Lending)

# L4
Low_Interest_and_No_Interest_Credit_Cards = Node("Low Interest & No Interest Credit Cards", parent=Credit_Cards, documents=[(fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='low interest & no interest credit cards') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])

Rewards_Cards = Node("Rewards Cards", parent=Credit_Cards, documents=[(fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='rewards cards') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])

Student_Credit_Cards = Node("Student Credit Cards", parent=Credit_Cards, documents=[(fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='student credit cards') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])

Auto_Financing = Node("Auto Financing", parent=Loans, documents=[(fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='auto financing') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])
Commercial_Lending = Node("Commercial Lending", parent=Loans, documents=[(fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==1)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==2)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==3)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==4)])['vocab'].tolist()[0]
, (fvc.loc[(fvc['keyword']=='commercial lending') & (fvc['organic_rank']==5)])['vocab'].tolist()[0]])

##### Visualizing the created tree #####
for pre, fill, node in RenderTree(Finance):
print("%s%s" % (pre, node.name))

##### Getting documents for parent nodes #####
def get_documents(node):
if node.documents is not None:
return node.documents
else:
child_nodes = node.children
lis = []
for child in child_nodes:
child_docs = get_documents(child)
lis.append(child_docs)
node.documents = lis
return lis


get_documents(Finance)

最佳答案

您可以使用以下语法:

lis = lis + child_docs

代替

 lis.append(child_docs)

关于python - 从 python 中的列表列表创建列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com