def set_which(tl, *args, **kwargs):
    """
        tl1 = [('k1','v1'),('k1','v1'),('k31','v3'),('k32','v3'),('k4','v41'),('k4','v42'),('k1','v1')]
        set_which(tl1,'k1','v11',mode='key',which=1)
        tl1 = [('k1','v1'),('k1','v1'),('k31','v3'),('k32','v3'),('k4','v41'),('k4','v42'),('k1','v1')]
        set_which(tl1,'k1','v11',mode='key',which=2)
        tl1 = [('k1','v1'),('k1','v1'),('k31','v3'),('k32','v3'),('k4','v41'),('k4','v42'),('k1','v1')]
        set_which(tl1,'k3','v3',mode='value')
        tl1 = [('k1','v1'),('k1','v1'),('k31','v3'),('k32','v3'),('k4','v41'),('k4','v42'),('k1','v1')]
        set_which(tl1,'k3','v3',mode='kv',which=5)
    """
    if 'mode' in kwargs:
        mode = kwargs['mode']
    else:
        mode = 'key'
    if 'which' in kwargs:
        which = kwargs['which']
    else:
        which = 'all'
    args = list(args)
    if args.__len__() == 1:
        key, value = args[0]
    elif args.__len__() == 2:
        key = args[0]
        value = args[1]
    else:
        raise Exception('key or value or kv needed')
    length = tl.__len__()
    cursor = 0
    for i in range(0, length):
        t = tl[i]
        k = t[0]
        v = t[1]
        if mode == 'key':
            match = k == key
        elif mode == 'value':
            match = v == value
        else:
            match = True
        if match:
            if which == 'all':
                cond = True
            else:
                cond = cursor == which
            if cond:
                nt = key, value
                tl[i] = nt
                if which == 'all':
                    pass
                else:
                    break
            else:
                pass
            cursor = cursor + 1
        else:
            pass
    return tl
