行业递归

Posted yuanjia8888

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了行业递归相关的知识,希望对你有一定的参考价值。

#上下游递归寻找

import csv
import psycopg2
class IO_rw(object):

def __init__(self):

self.conn = psycopg2.connect(database="postgres", user="postgres", password="123456", host="127.0.0.1", port="5432")
self.cur = self.conn.cursor()
self.read = []
self.datadict = {}
#
def process_item(self):

self.cur.execute("select id,name,pid from bjzs_big_data.baoji_industry_level where pid = 0")
rows = self.cur.fetchall()

#拿到所有的一级分类
dict = {}
for row in rows:
row = list(row)
dict[row[0]] = row[1]
li = list(dict.items())
return li

#下游
def sql_dowm(self,i):

self.cur.execute("select downid from bjzs_big_data.baoji_industry_chain where upid = {}".format(i))
rows = self.cur.fetchall()
downList = []
for row in rows:
downList.append(row[0])
return downList

#上游
def sql_up(self,i):
self.cur.execute("select upid from bjzs_big_data.baoji_industry_chain where downid = {}".format(i))
rows = self.cur.fetchall()
downList = []
for row in rows:
downList.append(row[0])
return downList


def getAllDowm(self, id):
temp = {}

#把当前的id放在 全局的read里面。用于纺织重新查找
self.read.append(id)

#查找它的下游
downList = self.sql_dowm(id)
#self.getName(downList)

#查找它的上游
upList = self.sql_up(id)
#self.getName(upList)

temp[‘up‘] = upList
temp[‘down‘] = downList

self.datadict[id] = temp

#判断拿到的 id 是否存在
for down in downList:
self.getName(down)
if down in self.read:
pass
else:
downList = self.getAllDowm(down)

for up in upList:
self.getName(up)
if up in self.read:
pass
else:
downList = self.getAllDowm(up)

def getName(self,id):
if id == ‘‘:
return 0
self.cur.execute("select name from bjzs_big_data.baoji_industry_level where id = {}".format(id))
rows = self.cur.fetchall()



if __name__ == ‘__main__‘:
r = IO_rw()

li = r.process_item()

for i in li:
r.getAllDowm(i[0])
print(r.datadict)
r.datadict = {}
r.cur.close()
r.conn.close()



































































































以上是关于行业递归的主要内容,如果未能解决你的问题,请参考以下文章

executePendingTransactions 的递归入口

nodejs常用代码片段

JavaScript 代码片段

48个值得掌握的JavaScript代码片段(上)

哈斯克尔。我很困惑这个代码片段是如何工作的

JavaSE 方法的使用