Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多处理搜索列表_Python_Python 3.x_For Loop_Multiprocessing - Fatal编程技术网

Python 使用多处理搜索列表

Python 使用多处理搜索列表,python,python-3.x,for-loop,multiprocessing,Python,Python 3.x,For Loop,Multiprocessing,下面是一个示例代码,用于检查列表中是否存在项,并将其写入另一个a列表: list_1 = [(0,1),(3,2),(1,5),(0,3),(3,7)] a = [] for i in list_1: if i[0]==3: a.append(i) break 输出: [(3, 2)] 当找到满足条件的第一个元素时,循环将终止 假设listlist_1非常大,在这种情况下如何应用多处理?@Marichyasana是一个非常好的解决方案。我在回答你的问题时

下面是一个示例代码,用于检查列表中是否存在项,并将其写入另一个
a
列表:

list_1 = [(0,1),(3,2),(1,5),(0,3),(3,7)]

a = []
for i in list_1:
    if i[0]==3:
       a.append(i)
       break
输出:

[(3, 2)]
当找到满足条件的第一个元素时,循环将终止


假设list
list_1
非常大,在这种情况下如何应用多处理?

@Marichyasana是一个非常好的解决方案。我在回答你的问题时有点过火,测试了多个游泳池的大小。代码大约有114行,您可以使用它作为偏离的基础

Brake your big list into smaller lists
>>> big= [(0,1),(3,2),(1,5),(0,3),(3,7)]
>>> a=big[0:2]
>>> b=big[2:]
define a function to look for your tuple and return it or return None
>>> def doit(L):
        for i in L:
            if i[0]==3:
                return i
        return None
figure out how to create a new process and call it with each of the smaller lists
>>> doit(a)
(3, 2)
>>> doit(b)
(3, 7)
look at the tuples returned that are not None in order and choose the first one
我正在测试100000个样本列表的多个
Pool()
大小。我认为我的代码不是很“Pythonic”,任何反馈都会很好。我用过这个

结果:

进程数:3个;处理时间为:00:00:01
长度 结果列表1000

进程数:10个;处理时间为:00:00:02
长度 结果列表1000

进程数:20个;处理时间为:00:00:09
长度 结果列表1000

进程数:33;处理时间为:00:00:04
长度 结果列表1000

代码:

# from multiprocessing import Queue
from multiprocessing import Pool
from random import randint
from Timer import Timer # custom timer wraper I wrote

def create_list_indexes(m_int_list_len):
    """
    this method creates as many evenly spaces segments for the list of data

    m_int_list_len
    type: int
    desc: length of the list of data; number of samples of data

    returns
    type: list
    desc: list of lists; indexes for data list
    list_return[x][0] -> type: int; low index
    list_return[x][1] -> type: int; high index
    """
    # segment length    
    int_seg_len = 100
    list_return = list()

    # get number of segments fo the list
    if m_int_list_len % int_seg_len == 0:
        int_num_seg = int(m_int_list_len / int_seg_len)
        bool_zero_mod = True
    else:
        int_num_seg = int(m_int_list_len / int_seg_len) + 1
        bool_zero_mod = False

    # create indexes of list
    for int_i in range(0, int_num_seg):        
        # check for zero mod
        if ~bool_zero_mod and int_i == int_num_seg - 1:
            int_low = int_i * int_seg_len       
            int_high = m_int_list_len
        else:
            int_low = int_i * int_seg_len
            int_high = int_low + int_seg_len - 1

        list_return.append([int_low, int_high])

    return list_return

def test_pools(m_tuple_args):
    """
    this method tests the different number of pools on a large list of data

    m_list_tasks
    type: list
    desc: list of tuples
    m_tuple_args[0] -> type: int; target to search for
    m_tuple_args[1] -> type: list; list of indexes
        m_tuple_args[1][0] -> type: int; low index
        m_tuple_args[1][1] -> type: int; high index    
    m_tuple_args[2] -> type: list; the data

    returns
    type: list
    desc: list of lists; samples which are lists of length 2
    """

    # unpack tuple for simplicity
    int_target = m_tuple_args[0]
    int_low, int_high = m_tuple_args[1]
    list_data = m_tuple_args[2]

    list_results = list()
    for list_sample in list_data[int_low:int_high]:
        if list_sample == int_target:
            list_results.append(list_sample)

    # return results
    return list_results

if __name__ == '__main__':
    # data structures for example
    list_data = list()
    list_proc = [3, 10, 20, 33]
    int_num_data = 100000
    int_max_int = 100
    int_target = 42

    # create random data list
    for int_i in range(0, int_num_data):
        list_data.append([randint(0, int_max_int),
                          randint(0, int_max_int)])

    # pools of different sizes to compare
    list_pools = [Pool(processes = x) for x in list_proc]

    # create indexes for list
    list_indexes = create_list_indexes(int_num_data)    

    # compare pools
    int_counter = 0
    for pool in list_pools:
        # create task list
        list_tasks = list()
        for int_i in range(0, len(list_indexes)):
            list_tasks.append((int_target, list_indexes[int_i], list_data))

        # test pool
        timer_pool = Timer()
        list_pool = pool.map(test_pools, list_tasks)
        string_pool = 'number of processes: ' + str(list_proc[int_counter])
        timer_pool.stop_timer(string_pool)
        print('length of results list', len(list_pool))
        print()

        # increment counter
        int_counter += 1

我们说的是多大?多处理在这方面可以有所帮助,但不清楚复杂性的大幅增加是否有必要。事实上,我越想它,就越意识到这是一个数据结构问题,而不是多处理问题。首先不要使用列表。通常情况下,这将搜索
列表(itertools.product(tuple(range(64)),repeat=4))
中的值以及更大的值。如果第一个匹配停止搜索,这必须是顺序的,在这种情况下,多处理不是答案。如果是第一场比赛,那就不同了。无论如何,我倾向于同意@wim,这更多的是一个数据结构问题,而不是多处理。在我的实际情况中,这不是关于数据结构的问题,而是在我的算法中存在一个瓶颈,即更广泛的
if
条件,再加上大量的搜索列表,结果很差。这就是为什么我只给出了一个简化的问题,这样就没有人会不必要地去理解代码的全部含义。你现在能并行
doit(a)
doit(b)
吗?如果其中一个返回非空值,则会中断计算。因为这样我就没有时间了。我将不胜感激。是的,这位同事接下来给出了一个冷静而明确的解决方案。但我也重视对问题的不同看法和此人的努力:)@TomaszPrzemski在我的解决方案中,您可以使用列表作为
list\u final.extend(list\u pool[0])
。我忘了将结果格式化为元组列表,可以很容易地修改。结果,
list\u pool
是一个列表列表,这是由于
list\u数据
是基于段长度切片的。