循环遍历列表
#coding=utf8 tp = [(1,221,331),(2,222,332),(3,223,333), (4,224,334), (5,225,335),(6,226,336)] ids = [1,2,3,4,5,6] i_ids = [1,2,4] u_ids = [3,5,6] i_list, u_list = [],[] def generate(i_ids, u_ids, dataList): #flag:标识,1则mainIds为insert, 0则mainIds为update #dataList:数据列表 #i_ids:insert id 列表 #u_ids:update id 列表 #@return (insert_list, update_list) data_list = dataList if len(i_ids) == 0: update_list = data_list insert_list = [] elif len(u_ids) == 0: insert_list = data_list update_list = [] else: insert_list = [] update_list = [] mainIds,seconIds,flag = (u_ids,i_ids, 0) if len(i_ids) > len(u_ids) else (i_ids,u_ids, 1) def loops(main_ids, d_list): m_list, s_list = [], [] for i in main_ids: for j in d_list: if i == j[0]: m_list.append(d_list.pop(d_list.index(j))) break s_list = d_list return m_list, s_list a,b = loops(mainIds, data_list) insert_list, update_list = (a, b) if flag == 1 else (b, a) return insert_list, update_list print generate(i_ids, u_ids, tp)