标签:res dom pre false sync order delay child append
#1.方式1 data=[ {"cat_id":1,"name":"北京","parent_id":0}, {"cat_id":2,"name":"上海","parent_id":0}, {"cat_id":3,"name":"沙河","parent_id":1}, {"cat_id":4,"name":"sb镇","parent_id":3}, {"cat_id":5,"name":"昌平","parent_id":1}, {"cat_id":6,"name":"青浦","parent_id":2}, ] def get_level(data): data_list=[] for item in data: if item[‘parent_id‘]==0: item[‘level‘]=0 else: item[‘level‘]=1 data_list.append(item) return data_list # print(1,get_level(data)) ‘‘‘ 问题: 因为前端,需要通过循环多少成来,展示多个等级 for item in data: print(item.name) for item1 in item.children item1.name 归档:我要把我的子集,都放在我的children列表里面 ‘‘‘ #方式2 data=[ {"cat_id":1,"name":"北京","parent_id":0}, {"cat_id":2,"name":"上海","parent_id":0}, {"cat_id":3,"name":"沙河","parent_id":1}, {"cat_id":4,"name":"sb镇","parent_id":3}, {"cat_id":5,"name":"昌平","parent_id":1}, {"cat_id":6,"name":"青浦","parent_id":2}, ] def get_tree(data): lists=[] tree={} for i in data: tree[i[‘cat_id‘]]=i # print(tree) for item in data: if not item[‘parent_id‘]: lists.append(tree[item[‘cat_id‘]]) else: if "children" not in tree[item[‘parent_id‘]]: tree[item[‘parent_id‘]][‘children‘]=[] tree[item[‘parent_id‘]][‘children‘].append(tree[item[‘cat_id‘]]) return lists print(2,get_tree(data)) [ {‘cat_id‘: 1, ‘name‘: ‘北京‘, ‘parent_id‘: 0, ‘children‘: [{‘cat_id‘: 3, ‘name‘: ‘沙河‘, ‘parent_id‘: 1, ‘children‘: [{‘cat_id‘: 4, ‘name‘: ‘sb镇‘, ‘parent_id‘: 3}] }, {‘cat_id‘: 5, ‘name‘: ‘昌平‘, ‘parent_id‘: 1}]}, {‘cat_id‘: 2, ‘name‘: ‘上海‘, ‘parent_id‘: 0, ‘children‘: [{‘cat_id‘: 6, ‘name‘: ‘青浦‘, ‘parent_id‘: 2}]} ] #方式3 #需求 ‘‘‘ 问题: 我们数据不是有序的,但是我们要排序,并归类,把父集放在子集的前面,辈分等级通过level标明 ‘‘‘ res=[] def get_son(data,level=0,parent_id=0,is_clear=True): if is_clear: res.clear() for item in data: if item[‘parent_id‘]==parent_id: item[‘level‘]=level res.append(item) get_son(data,level=level+1,parent_id=item[‘cat_id‘],is_clear=False) return res # son=get_son(data) # for i in son: # print("-"*i[‘level‘]+i[‘name‘]) # 1北京 0 # 2-海淀1 # 4--sb镇2 # -昌平 # 3 上海 0 # -青浦 # --徐泾镇 # -闵行 res_id=[] def get_son_id(data,parent_id=0,is_clear=True): if is_clear: res_id.clear() if parent_id : res_id.append(parent_id) for item in data: if item[‘parent_id‘]==parent_id: res_id.append(item[‘cat_id‘]) get_son_id(data,parent_id=item[‘cat_id‘],is_clear=False) return res_id # print(get_son_id(data,1)) # import time ,random # def get_order_id(): # st="012345679qwertyui" # order_id=str(time.strftime("%Y%m%d%h%M%S"))+"".join(random.sample(st,5)) # return order_id # from datetime import datetime # # from pro_celery.celery import del_order # def add_task(order_id,seconds=5): # ctime = datetime.now() # utc_ctime = datetime.utcfromtimestamp(ctime.timestamp()) # from datetime import timedelta # time_delay = timedelta(seconds=seconds) # task_time = utc_ctime + time_delay # result = del_order.apply_async(args=[order_id, ], eta=task_time)
https://www.cnblogs.com/laoyuming/p/14308448.html
标签:res dom pre false sync order delay child append
原文地址:https://www.cnblogs.com/tfzz/p/14775285.html