import copy
import math

from databaseConn import *
def listCross(a,b):
    '''
    :param a:list1
    :param b:list2
    :return:list交叉排序
    '''
    #print(a,b)
    temp=[]
    flen=len(b) if len(a)>len(b) else len(a)
    print(flen)
    for i in range(flen):
        temp.append(a.pop(0))
        temp.append(b.pop(0))
    temp+=a
    temp+=b
    return temp

def removeRepeat(a):
    '''
    :param a:list
    :return:按顺序去掉重复词
    '''
    temp=list(set(a))
    temp.sort(key=a.index)
    return temp

def subtraction(a,b):
    if a and  b:
        return list(set(a).difference(set(b)))
    return b if b else a


def mergelist(a):
    '''
    :param a:list
    :return: 嵌套list合并成一个
    '''
    t=itertools.chain.from_iterable(a)
    return list(t)


def listGroup(tt,size=3):
    '''
    :param tt:
    :param size:没用，预留值
    :return: list分组，按照递增分组
    '''
    temp=[]
    i=1
    while True:
        temp.append(tt[:i])
        [tt.remove(ii) for ii in tt[:i]]
        i+=1
        if not tt:
            break
    return temp

def listSubgroup(tt,size=6):
    temp=[]
    for i in range(0,len(tt),size):
        print(tt[i:i+size])
        temp.append(tt[i:i+size])
    return temp

def divlist(a,num=0):
    if not num:
        return a
    divnum=int(len(a)/num)
    temp=[]
    flag=0
    for i in range(divnum):
        temp.append(a[flag:flag+divnum])
        flag+=divnum
        if divnum==i+1:
            temp.append(a[flag:])

    return temp

def split_by_length(init_list, children_list_len):
    """
    按照长度拆分给定数组
    :param init_list:
    :param children_list_len:
    :return:
    """
    #print(next(tt[0]))
    list_of_groups = zip(*(iter(init_list),) * children_list_len)
    end_list = [list(i) for i in list_of_groups]
    count = len(init_list) % children_list_len
    end_list.append(init_list[-count:]) if count != 0 else end_list
    return end_list

def split_by_num(init_list, num):
    '''
    按照num拆分给定数组
    :param init_list:
    :param num:
    :return:
    '''
    temp=[]
    flag=0
    split_by=math.ceil(len(init_list)/num)
    #print(split_by)
    for i in range(num):
        temp.append(init_list[flag:flag+split_by])
        flag+=split_by
        #print(flag,i)
    return [i for i in temp if i]




def countlist(a,num=0):
    from collections import Counter
    '''
    :param a:
    :param num:如果num不为空，则按num取前num个
    :return: 重复元素计数
    '''
    temp=Counter(a)
    if num:
        return dict(temp.most_common(num))
    return dict(temp.most_common(len(a)))

def bingji(a,b):
    """
    :param a:
    :param b:
    :return: 并集
    """
    temp=list(set(a).difference(set(b)))
    temp.sort(key=a.index)
    return temp


if __name__ == '__main__':
    a=[1,2,4,3,5,6,7,8,9,10]
    b=[10,2,4]
    print(split_by_num(b,3))
    #print(int(11/2))

    #a=[[1,2],[2,3]]
    #print(mergelist(a))

    #print(listSubgroup(a,6))
