社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  ewz93  »  全部回复
回复总数  3
4 年前
回复了 ewz93 创建的主题 » 如何在Python中加快过滤过程?

检查是否总是只检查一行中是否有单词 df1["PRODUCTDESC"] 是 input , product 或 discription ? 在这种情况下,您可以使用:

def intersects_inp_prod_or_desc(string_):
    return any([spl in ["input", "product", "description"] for spl in string_.split()])

df1["INTERSECT"] = df1["PRODUCTDESC"].apply(intersects_inp_prod_or_desc)
df1 = df1[df1["INTERSECT"]]

它会创建一个布尔列,如果为True,则包含True,如果为Not,则包含False,然后使用该列过滤数据帧。

编辑:关于运行时

我们可以做一些基本的计时,比如:

def run(rows):
    start = time.time()
    productdesc = ["something with input", "something with product", 
                   "something with description", "something else"]
    productdesc *= rows//len(productdesc)
    df1 = pandas.DataFrame(productdesc, columns=["PRODUCTDESC"])
    df1["INTERSECT"] = df1["PRODUCTDESC"].apply(intersects_inp_prod_or_desc)
    df1 = df1[df1["INTERSECT"]]
    finish = time.time()
    print(f"{rows:,} rows: {round(finish - start, 5)} seconds")


run(10)
run(100)
run(1000)
run(10000)
run(100000)
run(1000000)
run(10000000)

首先,采用不变的方法:

def intersects_inp_prod_or_desc(string_):
    return any([spl in ["input", "product", "description"] for spl in string_.split()])

# 10 rows: 0.0 seconds
# 100 rows: 0.0 seconds
# 1,000 rows: 0.01012 seconds
# 10,000 rows: 0.01013 seconds
# 100,000 rows: 0.12163 seconds
# 1,000,000 rows: 1.21986 seconds
# 10,000,000 rows: 12.12009 seconds

我们可以手动提前返回,以节省一些时间:

def intersects_inp_prod_or_desc(string_):
    split_string = string_.split()
    for split_ in split_string:
        if split_ == "input":
            return True
        if split_ == "product":
            return True
        if split_ == "description":
            return True
    return False

# 10 rows: 0.0 seconds
# 100 rows: 0.00812 seconds
# 1,000 rows: 0.00215 seconds
# 10,000 rows: 0.0102 seconds
# 100,000 rows: 0.09158 seconds
# 1,000,000 rows: 0.94362 seconds
# 10,000,000 rows: 9.40814 seconds

然而,一旦我们在数据帧中有了一些非常长的描述,比如这样(250字),它就会开始变得慢得多:

productdesc = ["something with input", "something with product", 
               "something with description", "something else", 
               "something long without key words "*50]

# 10 rows: 0.0 seconds
# 100 rows: 0.00813 seconds
# 1,000 rows: 0.01014 seconds
# 10,000 rows: 0.09424 seconds
# 100,000 rows: 0.93761 seconds
# 1,000,000 rows: 9.35188 seconds
# 10,000,000 rows: 73.0588 seconds

但我们可以通过首先检查任何关键字是否确实是描述的子字符串来修改该方法,并且只有当它是时,我们才对拆分的字符串进行拆分和迭代:

def intersects_inp_prod_or_desc(string_):
    if "input" in string_ or "product" in string_ or "description" in string_:
        split_string = string_.split()
        for split_ in split_string:
            if split_ == "input":
                return True
            if split_ == "product":
                return True
            if split_ == "description":
                return True
    return False

# 10 rows: 0.00814 seconds
# 100 rows: 0.00209 seconds
# 1,000 rows: 0.0 seconds
# 10,000 rows: 0.0102 seconds
# 100,000 rows: 0.12225 seconds
# 1,000,000 rows: 1.15786 seconds
# 10,000,000 rows: 11.52258 seconds

但请注意,在任何情况下,此额外检查都会花费我们一点时间,因此,如果没有长描述,则不会节省时间,因为预检查的额外时间可能会比跳过拆分字符串的好处更长,例如,如果您在列表中调用此修改版本,而没有再次调用长描述:

productdesc = ["something with input", "something with product",
               "something with description", "something else"]

# 10 rows: 0.0 seconds
# 100 rows: 0.0 seconds
# 1,000 rows: 0.0 seconds
# 10,000 rows: 0.01017 seconds
# 100,000 rows: 0.10158 seconds
# 1,000,000 rows: 0.95558 seconds
# 10,000,000 rows: 9.60593 seconds

一般来说,我不知道您的数据帧有多大,因为在我看来,即使是未更改的版本也非常快。它之所以慢,可能是因为可能有很多行,而且很多行都很长。在这种情况下,使用修改后的方法,该方法会预先检查任何关键字是否是字符串的子字符串,如果不是,则立即返回False。

4 年前
回复了 ewz93 创建的主题 » 使用Python需要删除超过3Ù的ÙÙÙهÙÙههه‡

这有点棘手,因为从右到左的方向,但这似乎适合我:

import re

text = "شنو هل الغابة اوي عايشين بيها كلنا نختلف ههههههههههههه اي اي الايلينز تجين"
text = re.sub("ه{3,}", "ههه", text)
print(text)
> "شنو هل الغابة اوي عايشين بيها كلنا نختلف ههه اي اي الايلينز تجين"

但如果我正确理解谷歌翻译的话,重复的部分只是在笑,所以不确定为了NLP的目的而统一它能带来多大的价值。

你的意思是如何代替打印,将所有值放入一个列表中,然后打印出来?

你可以通过列表来理解:

output = [lemmatizer.lemmatize(w.lower(), pos=wordnet.VERB) for w in processed_H2_tag]

或者,如果您更喜欢经典for loop:

output = []
for w in processed_H2_tag:
    output.append(lemmatizer.lemmatize(w.lower(), pos=wordnet.VERB))