小编Muh*_*aha的帖子

如何删除Python中字符串中的任何URL

我想删除字符串中的所有URL(用""替换它们)我搜索过但却找不到我想要的东西.

例:

text1
text2
http://url.com/bla1/blah1/
text3
text4
http://url.com/bla2/blah2/
text5
text6
http://url.com/bla3/blah3/
Run Code Online (Sandbox Code Playgroud)

我希望结果如下:

text1
text2
text3
text4
text5
text6
Run Code Online (Sandbox Code Playgroud)

python regex url replace python-2.7

31
推荐指数
11
解决办法
7万
查看次数

Python - 检查字符串是否包含列表中任何项目中的特定字符的最快方法

检查字符串是否包含列表中任何项目的某些字符的最快方法是什么?

目前,我正在使用这种方法:

lestring = "Text123"

lelist = ["Text", "foo", "bar"]

for x in lelist:
    if lestring.count(x):
        print 'Yep. "%s" contains characters from "%s" item.' % (lestring, x)
Run Code Online (Sandbox Code Playgroud)

有没有办法在没有迭代的情况下做到这一点(我认为这会让它更快.)?

python iteration performance list

9
推荐指数
1
解决办法
3万
查看次数

循环中"time.sleep"与"threading.Timer"的资源使用情况

第一种方法:

import threading
import time

def keepalive():
    while True:
        print 'Alive.'
        time.sleep(200)
threading.Thread(target=keepalive).start()
Run Code Online (Sandbox Code Playgroud)

第二种方法:

import threading

def keepalive():
    print 'Alive.'
    threading.Timer(200, keepalive).start()

threading.Timer(200, keepalive).start()
Run Code Online (Sandbox Code Playgroud)

哪种方法占用更多内存?在第二种方法中,线程在被激活后结束了吗?或者它是否保留在内存中并启动新线程?(多线程)

python memory multithreading timer python-2.7

4
推荐指数
2
解决办法
5151
查看次数