python - 嵌套 for 循环遍历 python 列表

标签 python multithreading pandas iteration nested-loops

我必须遍历超过 4000 个项目的列表,并使用 python 中的推荐算法检查它们的相似性。

该脚本需要很长时间才能运行(10-11 小时),我想结合多线程来提高速度,但不知 Prop 体怎么做。

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt

    data=pd.read_csv('data.csv',index_col=0, encoding="ISO-8859-1")       

    # Get list of unique items
    itemList=list(set(data["product_ref"].tolist()))

    # Get count of customers
    userCount=len(set(data["customer_id"].tolist()))

    # Create an empty data frame to store item affinity scores for items.
    itemAffinity= pd.DataFrame(columns=('item1', 'item2', 'score'))

    def itemUsers(ind):
      return data[data.product_ref==itemList[ind]]["customer_id"].tolist()

    rowCount=0
    for ind1 in range(len(itemList)): 
        item1Users = itemUsers(ind1) 
        pool = Pool()
        pool.map(loop2, data_inputs)
        for ind2 in range(ind1+1, len(itemList)): 
            print(ind1, ":", ind2)       
            item2Users = itemUsers(ind2) 
            commonUsers= len(set(item1Users).intersection(set(item2Users))) 
            score=commonUsers / userCount
            itemAffinity.loc[rowCount] = [itemList[ind1],itemList[ind2],score] 
            rowCount +=1

最佳答案

合并多线程不会改善您的运行时间。

这样想,当您使用多线程时,您会在多个线程之间分散计算时间 - 当您可以将其分散在一个进程上时。

例如,当您在线程上等待用户输入并且您想在等待时进行计算时,它可能会有所帮助,但这不是您的情况。

关于python - 嵌套 for 循环遍历 python 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50930805/

相关文章:

java - JMS多线程处理

python-3.x - Pandas 数据框使用列逻辑删除重复行

python - 将值从元组列表添加到现有数据框

python - 有没有一种方法可以在 python 中创建自定义字典而无需复制方法?

python - 如何访问 python 的 mako 模板系统中的当前 URI?

python - 每个点带有颜色编码值的散点图(如 pcolormesh)

Python 2.7 - Windows 10 - pywin32 安装失败

c++ - 是否曾在大型多线程 C++ 程序中使用过通信顺序进程?

android - Android AsyncTask的doInBackground无法启动

python - 如何使用并发将数据帧附加到空数据帧