小编cpp*_*nja的帖子

Python:线程中的运行函数不会修改current_thread()

我目前正在试图弄清楚线程如何在python中工作.

我有以下代码:

def func1(arg1, arg2):

    print current_thread()
    ....

class class1:

    def __init__():
        ....

    def func_call():
        print current_thread()
        t1 = threading.Thread(func1(arg1, arg2))
        t1.start()
        t1.join()
Run Code Online (Sandbox Code Playgroud)

我注意到的是两个打印输出相同的东西.为什么线程不会改变?

python multithreading

8
推荐指数
2
解决办法
2万
查看次数

“中位数”算法的Python实现

我已经在 python 中编写了这个中位数算法的实现,但它似乎没有输出正确的结果,而且对我来说它似乎也不是线性复杂性,知道我在哪里偏离轨道吗?

def select(L):
    if len(L) < 10:
        L.sort()
        return L[int(len(L)/2)]
    S = []
    lIndex = 0
    while lIndex+5 < len(L)-1:
        S.append(L[lIndex:lIndex+5])
        lIndex += 5
    S.append(L[lIndex:])
    Meds = []
    for subList in S:
        print(subList)
    Meds.append(select(subList))
    L2 = select(Meds)
    L1 = L3 = []
    for i in L:
        if i < L2:
            L1.append(i)
        if i > L2:
            L3.append(i)
    if len(L) < len(L1):
        return select(L1)
    elif len(L) > len(L1) + 1:
        return select(L3)
    else:
        return L2
Run Code Online (Sandbox Code Playgroud)

该函数的调用方式如下:

L = list(range(100))
shuffle(L) …
Run Code Online (Sandbox Code Playgroud)

python algorithm

5
推荐指数
1
解决办法
9003
查看次数

标签 统计

python ×2

algorithm ×1

multithreading ×1