【问题标题】:Python, I need the following code to finish quickerPython,我需要以下代码才能更快完成
【发布时间】:2012-01-12 08:35:11
【问题描述】:

我需要以下代码在没有线程或多处理的情况下更快地完成。如果有人知道任何技巧,将不胜感激。也许for i in enumerate() 或在计算之前将列表更改为字符串,我不确定。
对于下面的示例,我尝试使用随机序列重新创建变量,但是这使得循环内的一些条件变得无用......这对于这个示例来说是可以的,它只是意味着代码的“真实”应用程序将需要更长的时间。 目前在我的 i7 上,下面的示例(大部分会绕过它的一些条件)在 1 秒内完成,我想尽可能地把它弄下来。

import random
import time
import collections
import cProfile


def random_string(length=7):
    """Return a random string of given length"""
    return "".join([chr(random.randint(65, 90)) for i in range(length)])

LIST_LEN = 18400
original = [[random_string() for i in range(LIST_LEN)] for j in range(6)]
LIST_LEN = 5
SufxList = [random_string() for i in range(LIST_LEN)]
LIST_LEN = 28
TerminateHook = [random_string() for i in range(LIST_LEN)]
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Exclude above from benchmark


ListVar = original[:]
for b in range(len(ListVar)):
   for c in range(len(ListVar[b])):

       #If its an int ... remove
       try:
           int(ListVar[b][c].replace(' ', ''))
           ListVar[b][c] = ''
       except: pass

       #if any second sufxList delete
       for d in range(len(SufxList)):
           if ListVar[b][c].find(SufxList[d]) != -1: ListVar[b][c] = ''

       for d in range(len(TerminateHook)):
           if ListVar[b][c].find(TerminateHook[d]) != -1: ListVar[b][c] = ''
   #remove all '' from list
   while '' in ListVar[b]: ListVar[b].remove('')

print(ListVar[b])

【问题讨论】:

  • 您可以尝试用 C 重写代码的某些部分(即在列表中查找 mstrings)。我为此使用swig。此外,也许哈希而不是列表会更快
  • 您能解释一下您要解决的问题吗?我们更容易回答“什么是执行 X 的有效方法”这个问题,而不是理解您的代码并提出更好的方法。
  • 我想通过过滤过程运行一个列表

标签: python multithreading subprocess benchmarking


【解决方案1】:
ListVar = original[:]

这会生成 ListVar 的浅表副本,因此您对二级列表的更改也会影响原始列表。你确定那是你想要的吗?从头开始构建新的修改列表会更好。

for b in range(len(ListVar)):
   for c in range(len(ListVar[b])):

糟糕:尽可能直接迭代列表。

       #If its an int ... remove
       try:
           int(ListVar[b][c].replace(' ', ''))
           ListVar[b][c] = ''
       except: pass

您想忽略数字中间的空格吗?这听起来不对。如果数字可能是负数,您可能需要使用try..except,但如果它们只是正数,请使用.isdigit()

       #if any second sufxList delete
       for d in range(len(SufxList)):
           if ListVar[b][c].find(SufxList[d]) != -1: ListVar[b][c] = ''

这只是不好的命名吗? SufxList 意味着您正在寻找后缀,如果是这样,只需使用 .endswith() (请注意,您可以传入一个元组以避免循环)。如果您确实想找到后缀在字符串中的任何位置,请使用 in 运算符。

       for d in range(len(TerminateHook)):
           if ListVar[b][c].find(TerminateHook[d]) != -1: ListVar[b][c] = ''

再次使用in 运算符。 any() 在这里也很有用。

   #remove all '' from list
   while '' in ListVar[b]: ListVar[b].remove('')

并且while 是 O(n^2) 即它会很慢。您可以改用列表推导式来去除空白,但最好先构建干净的列表。

print(ListVar[b])

我认为你的缩进可能是错误的。

将这些建议放在一起会得到如下结果:

suffixes = tuple(SufxList)
newListVar = []
for row in original:
   newRow = []
   newListVar.append(newRow)
   for value in row:
       if (not value.isdigit() and 
           not value.endswith(suffixes) and
           not any(th in value for th in TerminateHook)):
           newRow.append(value)

    print(newRow)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-23
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多