def data_merge(ques1, ques2, qlist1=None, qlist2=None, name1='ques1', name2='ques2', mergeqnum='Q0', similar_threshold=70):
    '合并两份数据\n    ques1: 列表,[data1,code1]\n    ques2: 列表,[data2,code2]\n    '
    (data1, code1) = ques1
    (data2, code2) = ques2
    if ((qlist1 is None) or (qlist2 is None)):
        qlist1 = []
        qlist2 = []
        qqlist1 = []
        qqlist2 = []
        code_distance_min = code_similar(code1, code2)
        code1_key = sorted(code1, key=(lambda x: int(re.findall('\\d+', x)[0])))
        for c1 in code1_key:
            qtype1 = code1[c1]['qtype']
            rs_qq = code_distance_min.loc[(c1, 'qnum')]
            similar_content = code_distance_min.loc[(c1, 'similar_content')]
            similar_code = code_distance_min.loc[(c1, 'similar_code')]
            if ((similar_content >= similar_threshold) and (similar_code in [1, 2])):
                print('将自动合并: {} 和 {}'.format(c1, rs_qq))
                user_qq = rs_qq
                qqlist1 += code1[c1]['qlist']
                qqlist2 += code2[user_qq]['qlist']
                qlist1.append(c1)
                qlist2.append(rs_qq)
            elif ((similar_content >= similar_threshold) and (similar_code == 3)):
                t1 = (code1[c1]['code_r'] if (qtype1 == '矩阵单选题') else code1[c1]['code'])
                t1_qlist = code1[c1]['qlist']
                t1_value = [t1[k] for k in t1_qlist]
                t2 = (code2[rs_qq]['code_r'] if (qtype1 == '矩阵单选题') else code2[rs_qq]['code'])
                t2_qlist = code2[rs_qq]['qlist']
                t2_value = [t2[k] for k in t2_qlist]
                t1_qlist_new = [q for q in t1_qlist if (t1[q] in list((set(t1_value) & set(t2_value))))]
                t2_r = dict(zip([s[1] for s in t2.items()], [s[0] for s in t2.items()]))
                t2_qlist_new = [t2_r[s] for s in [t1[q] for q in t1_qlist_new]]
                code1[c1]['qlist'] = t1_qlist_new
                code1[c1]['code'] = {k: t1[k] for k in t1_qlist_new}
                qqlist1 += t1_qlist_new
                qqlist2 += t2_qlist_new
                qlist1.append(c1)
                qlist2.append(rs_qq)
                print('将自动合并: {} 和 {} (只保留了相同的选项)'.format(c1, rs_qq))
            elif (similar_code in [1, 2]):
                print(('-' * 40))
                print('为【  {}:{} 】自动匹配到: '.format(c1, code1[c1]['content']))
                print('  【  {}:{} 】,其相似度为{:.0f}%.'.format(rs_qq, code2[rs_qq]['content'], similar_content))
                tmp = input('是否合并该组题目,请输入 yes/no (也可以输入第二份数据中其他您需要匹配的题目): ')
                tmp = re.sub('\\s', '', tmp)
                tmp = tmp.lower()
                if (tmp in ['yes', 'y']):
                    user_qq = rs_qq
                elif (tmp in ['no', 'n']):
                    user_qq = None
                else:
                    tmp = re.sub('^q', 'Q', tmp)
                    if (tmp not in code2):
                        user_qq = None
                    elif ((tmp in code2) and (tmp != rs_qq)):
                        print('您输入的是{}:{}'.format(tmp, code2[tmp]['content']))
                        user_qq = tmp
                if (user_qq == rs_qq):
                    qqlist1 += code1[c1]['qlist']
                    qqlist2 += code2[user_qq]['qlist']
                    qlist1.append(c1)
                    qlist2.append(user_qq)
                    print('将自动合并: {} 和 {}'.format(c1, rs_qq))
                elif (user_qq is not None):
                    if (('code' in code1[c1]) and (len(code1[c1]['code']) > 0)):
                        t1 = (code1[c1]['code_r'] if (qtype1 == '矩阵单选题') else code1[c1]['code'])
                        t2 = (code2[user_qq]['code_r'] if (code2[user_qq]['qtype'] == '矩阵单选题') else code2[user_qq]['code'])
                        if (set(t1.values()) == set(t2.values())):
                            qqlist1 += code1[c1]['qlist']
                            qqlist2 += code2[user_qq]['qlist']
                            qlist1.append(c1)
                            qlist2.append(user_qq)
                            print('将自动合并: {} 和 {}'.format(c1, user_qq))
                        else:
                            print('两个题目的选项不匹配,将自动跳过.')
                    else:
                        qqlist1 += [code1[c1]['qlist'][0]]
                        qqlist2 += [code2[user_qq]['qlist'][0]]
                        qlist1.append(c1)
                        qlist2.append(user_qq)
                        print('将自动合并: {} 和 {}'.format(c1, user_qq))
                else:
                    print('将自动跳过: {}'.format(c1))
                print(('-' * 40))
            else:
                print('将自动跳过: {}'.format(c1))
        tmp = input('请问您需要的题目是否都已经合并? 请输入(yes / no)： ')
        tmp = re.sub('\\s', '', tmp)
        tmp = tmp.lower()
        if (tmp in ['no', 'n']):
            print('请确保接下来您要合并的题目类型和选项完全一样.')
            while 1:
                tmp = input('请输入您想合并的题目对,直接回车则终止输入(如: Q1,Q1 ): ')
                tmp = re.sub('\\s', '', tmp)
                tmp = re.sub('，', ',', tmp)
                tmp = tmp.split(',')
                tmp = [re.sub('^q', 'Q', qq) for qq in tmp]
                if (len(tmp) < 2):
                    break
                if ((tmp[0] in qlist1) or (tmp[1] in qlist2)):
                    print('该题已经被合并，请重新输入')
                    continue
                if ((tmp[0] not in code1) or (tmp[1] not in code2)):
                    print('输入错误, 请重新输入')
                    continue
                c1 = tmp[0]
                c2 = tmp[1]
                print('您输入的是：')
                print('第一份数据中的【 {}:{} 】'.format(c1, code1[c1]['content']))
                print('第二份数据中的【 {}:{} 】'.format(c2, code2[c2]['content']))
                w = code_similar({c1: code1[c1]}, {c2: code2[c2]})
                similar_code = w.loc[(c1, 'similar_code')]
                if ((similar_code in [1, 2]) and (len(code1[c1]['qlist']) == len(code2[c2]['qlist']))):
                    qqlist1 += code1[c1]['qlist']
                    qqlist2 += code2[c2]['qlist']
                    qlist1.append(c1)
                    qlist2.append(c2)
                    print('将自动合并: {} 和 {}'.format(c1, c2))
                else:
                    print('选项不匹配，请重新输入')
    else:
        qqlist1 = []
        for qq in qlist1:
            qqlist1 = (qqlist1 + code1[qq]['qlist'])
        qqlist2 = []
        for qq in qlist2:
            qqlist2 = (qqlist2 + code2[qq]['qlist'])
    if (mergeqnum in qqlist1):
        mergeqnum = (mergeqnum + 'merge')
    data1 = data1.loc[:, qqlist1]
    data1.loc[:, mergeqnum] = 1
    data2 = data2.loc[:, qqlist2]
    data2.loc[:, mergeqnum] = 2
    if (len(qqlist1) != len(qqlist2)):
        print('两份数据选项不完全匹配，请检查....')
        raise
    data2 = data2.rename(columns=dict(zip(qqlist2, qqlist1)))
    data12 = data1.append(data2, ignore_index=True)
    code12 = {}
    for (i, cc) in enumerate(qlist1):
        code12[cc] = code1[cc]
        if (('code' in code1[cc]) and ('code' in code2[qlist2[i]])):
            code12[cc]['code'].update(code2[qlist2[i]]['code'])
    code12[mergeqnum] = {'content': u'来源', 'code': {1: name1, 2: name2}, 'qtype': u'单选题', 'qlist': [mergeqnum]}
    return (data12, code12)
合并两份数据
ques1: 列表,[data1,code1]
ques2: 列表,[data2,code2]
def data_merge(ques1, ques2, qlist1=None, qlist2=None, name1='ques1', name2='ques2', mergeqnum='Q0', similar_threshold=70):
    '合并两份数据\n    ques1: 列表,[data1,code1]\n    ques2: 列表,[data2,code2]\n    '
    (data1, code1) = ques1
    (data2, code2) = ques2
    if ((qlist1 is None) or (qlist2 is None)):
        qlist1 = []
        qlist2 = []
        qqlist1 = []
        qqlist2 = []
        code_distance_min = code_similar(code1, code2)
        code1_key = sorted(code1, key=(lambda x: int(re.findall('\\d+', x)[0])))
        for c1 in code1_key:
            qtype1 = code1[c1]['qtype']
            rs_qq = code_distance_min.loc[(c1, 'qnum')]
            similar_content = code_distance_min.loc[(c1, 'similar_content')]
            similar_code = code_distance_min.loc[(c1, 'similar_code')]
            if ((similar_content >= similar_threshold) and (similar_code in [1, 2])):
                print('将自动合并: {} 和 {}'.format(c1, rs_qq))
                user_qq = rs_qq
                qqlist1 += code1[c1]['qlist']
                qqlist2 += code2[user_qq]['qlist']
                qlist1.append(c1)
                qlist2.append(rs_qq)
            elif ((similar_content >= similar_threshold) and (similar_code == 3)):
                t1 = (code1[c1]['code_r'] if (qtype1 == '矩阵单选题') else code1[c1]['code'])
                t1_qlist = code1[c1]['qlist']
                t1_value = [t1[k] for k in t1_qlist]
                t2 = (code2[rs_qq]['code_r'] if (qtype1 == '矩阵单选题') else code2[rs_qq]['code'])
                t2_qlist = code2[rs_qq]['qlist']
                t2_value = [t2[k] for k in t2_qlist]
                t1_qlist_new = [q for q in t1_qlist if (t1[q] in list((set(t1_value) & set(t2_value))))]
                t2_r = dict(zip([s[1] for s in t2.items()], [s[0] for s in t2.items()]))
                t2_qlist_new = [t2_r[s] for s in [t1[q] for q in t1_qlist_new]]
                code1[c1]['qlist'] = t1_qlist_new
                code1[c1]['code'] = {k: t1[k] for k in t1_qlist_new}
                qqlist1 += t1_qlist_new
                qqlist2 += t2_qlist_new
                qlist1.append(c1)
                qlist2.append(rs_qq)
                print('将自动合并: {} 和 {} (只保留了相同的选项)'.format(c1, rs_qq))
            elif (similar_code in [1, 2]):
                print(('-' * 40))
                print('为【  {}:{} 】自动匹配到: '.format(c1, code1[c1]['content']))
                print('  【  {}:{} 】,其相似度为{:.0f}%.'.format(rs_qq, code2[rs_qq]['content'], similar_content))
                tmp = input('是否合并该组题目,请输入 yes/no (也可以输入第二份数据中其他您需要匹配的题目): ')
                tmp = re.sub('\\s', , tmp)
                tmp = tmp.lower()
                if (tmp in ['yes', 'y']):
                    user_qq = rs_qq
                elif (tmp in ['no', 'n']):
                    user_qq = None
                else:
                    tmp = re.sub('^q', 'Q', tmp)
                    if (tmp not in code2):
                        user_qq = None
                    elif ((tmp in code2) and (tmp != rs_qq)):
                        print('您输入的是{}:{}'.format(tmp, code2[tmp]['content']))
                        user_qq = tmp
                if (user_qq == rs_qq):
                    qqlist1 += code1[c1]['qlist']
                    qqlist2 += code2[user_qq]['qlist']
                    qlist1.append(c1)
                    qlist2.append(user_qq)
                    print('将自动合并: {} 和 {}'.format(c1, rs_qq))
                elif (user_qq is not None):
                    if (('code' in code1[c1]) and (len(code1[c1]['code']) > 0)):
                        t1 = (code1[c1]['code_r'] if (qtype1 == '矩阵单选题') else code1[c1]['code'])
                        t2 = (code2[user_qq]['code_r'] if (code2[user_qq]['qtype'] == '矩阵单选题') else code2[user_qq]['code'])
                        if (set(t1.values()) == set(t2.values())):
                            qqlist1 += code1[c1]['qlist']
                            qqlist2 += code2[user_qq]['qlist']
                            qlist1.append(c1)
                            qlist2.append(user_qq)
                            print('将自动合并: {} 和 {}'.format(c1, user_qq))
                        else:
                            print('两个题目的选项不匹配,将自动跳过.')
                    else:
                        qqlist1 += [code1[c1]['qlist'][0]]
                        qqlist2 += [code2[user_qq]['qlist'][0]]
                        qlist1.append(c1)
                        qlist2.append(user_qq)
                        print('将自动合并: {} 和 {}'.format(c1, user_qq))
                else:
                    print('将自动跳过: {}'.format(c1))
                print(('-' * 40))
            else:
                print('将自动跳过: {}'.format(c1))
        tmp = input('请问您需要的题目是否都已经合并? 请输入(yes / no)： ')
        tmp = re.sub('\\s', , tmp)
        tmp = tmp.lower()
        if (tmp in ['no', 'n']):
            print('请确保接下来您要合并的题目类型和选项完全一样.')
            while 1:
                tmp = input('请输入您想合并的题目对,直接回车则终止输入(如: Q1,Q1 ): ')
                tmp = re.sub('\\s', , tmp)
                tmp = re.sub('，', ',', tmp)
                tmp = tmp.split(',')
                tmp = [re.sub('^q', 'Q', qq) for qq in tmp]
                if (len(tmp) < 2):
                    break
                if ((tmp[0] in qlist1) or (tmp[1] in qlist2)):
                    print('该题已经被合并，请重新输入')
                    continue
                if ((tmp[0] not in code1) or (tmp[1] not in code2)):
                    print('输入错误, 请重新输入')
                    continue
                c1 = tmp[0]
                c2 = tmp[1]
                print('您输入的是：')
                print('第一份数据中的【 {}:{} 】'.format(c1, code1[c1]['content']))
                print('第二份数据中的【 {}:{} 】'.format(c2, code2[c2]['content']))
                w = code_similar({c1: code1[c1]}, {c2: code2[c2]})
                similar_code = w.loc[(c1, 'similar_code')]
                if ((similar_code in [1, 2]) and (len(code1[c1]['qlist']) == len(code2[c2]['qlist']))):
                    qqlist1 += code1[c1]['qlist']
                    qqlist2 += code2[c2]['qlist']
                    qlist1.append(c1)
                    qlist2.append(c2)
                    print('将自动合并: {} 和 {}'.format(c1, c2))
                else:
                    print('选项不匹配，请重新输入')
    else:
        qqlist1 = []
        for qq in qlist1:
            qqlist1 = (qqlist1 + code1[qq]['qlist'])
        qqlist2 = []
        for qq in qlist2:
            qqlist2 = (qqlist2 + code2[qq]['qlist'])
    if (mergeqnum in qqlist1):
        mergeqnum = (mergeqnum + 'merge')
    data1 = data1.loc[:, qqlist1]
    data1.loc[:, mergeqnum] = 1
    data2 = data2.loc[:, qqlist2]
    data2.loc[:, mergeqnum] = 2
    if (len(qqlist1) != len(qqlist2)):
        print('两份数据选项不完全匹配，请检查....')
        raise
    data2 = data2.rename(columns=dict(zip(qqlist2, qqlist1)))
    data12 = data1.append(data2, ignore_index=True)
    code12 = {}
    for (i, cc) in enumerate(qlist1):
        code12[cc] = code1[cc]
        if (('code' in code1[cc]) and ('code' in code2[qlist2[i]])):
            code12[cc]['code'].update(code2[qlist2[i]]['code'])
    code12[mergeqnum] = {'content': u'来源', 'code': {1: name1, 2: name2}, 'qtype': u'单选题', 'qlist': [mergeqnum]}
    return (data12, code12)