有人可以向我解释一种在Python(2.7)中查找数字的所有因子的有效方法吗?
我可以创建算法来完成这项工作,但我认为编码很差,并且执行大量数据的结果需要很长时间.
如果我想让我的格式化字符串动态可调,我将更改以下代码
print '%20s : %20s' % ("Python", "Very Good")
Run Code Online (Sandbox Code Playgroud)
至
width = 20
print ('%' + str(width) + 's : %' + str(width) + 's') % ("Python", "Very Good")
Run Code Online (Sandbox Code Playgroud)
但是,似乎字符串连接在这里很麻烦.还有其他简化方法吗?
如何在批处理文件中执行另一个进程之前等待进程终止?假设我有一个notepad.exe
我需要在执行之前杀死的进程wordpad.exe
.然后,当wordpad.exe
终止时,我需要notepad.exe
再次启动.我怎么做?
我使用的是Python 2.7 + BeautifulSoup 4.3.2.
我正在尝试使用Python和BeautifulSoup来获取网页上的信息.由于网页在公司网站上需要登录和重定向,因此我将目标网页的源代码复制到文件中,并将其保存为C:\中的"example.html",以方便练习.
这是原始代码的一部分:
<tr class="ghj">
<td><span class="city-sh"><sh src="./citys/1.jpg" alt="boy" title="boy" /></span><a href="./membercity.php?mode=view&u=12563">port_new_cape</a></td>
<td class="position"><a href="./search.php?id=12563&sr=positions" title="Search positions">452</a></td>
<td class="details"><div>South</div></td>
<td>May 09, 1997</td>
<td>Jan 23, 2009 12:05 pm </td>
</tr>
Run Code Online (Sandbox Code Playgroud)
到目前为止我编写的代码是:
from bs4 import BeautifulSoup
import re
import urllib2
url = "C:\example.html"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())
cities = soup.find_all('span', {'class' : 'city-sh'})
for city in cities:
print city
Run Code Online (Sandbox Code Playgroud)
这只是测试的第一阶段,有些没有完成.
但是,当我运行它时,它会显示错误消息,使用"urllib2.urlopen"来打开本地文件似乎是不合适的.
Traceback (most recent call last):
File "C:\Python27\Testing.py", line 8, in <module>
page = urllib2.urlopen(url)
File "C:\Python27\lib\urllib2.py", …
Run Code Online (Sandbox Code Playgroud) import glob
list = glob.glob(r'*abc*.txt') + glob.glob(r'*123*.txt') + glob.glob(r'*a1b*.txt')
for i in list:
print i
Run Code Online (Sandbox Code Playgroud)
此代码用于列出当前文件夹中名称中包含"abc","123"或"a1b"的文件.如何使用一个glob来实现这个功能,谢谢
我想匹配字符串中最后一个简单模式的出现,例如
list = re.findall(r"\w+ AAAA \w+", "foo bar AAAA foo2 AAAA bar2")
print "last match: ", list[len(list)-1]
Run Code Online (Sandbox Code Playgroud)
但是,如果字符串很长,则会生成大量匹配项.是否有更直接的方法来匹配"AAAA"的第二次出现或我应该使用此变通方法?
考虑,
path1 = "c:/fold1/fold2"
list_of_paths = ["c:\\fold1\\fold2","c:\\temp\\temp123"]
if path1 in list_of_paths:
print "found"
Run Code Online (Sandbox Code Playgroud)
我希望if语句返回true但它的计算结果为False,因为它是字符串比较
如何比较两条路径,无论它有前向或后向斜线!我不喜欢使用替换功能并将两个字符串转换为comman格式
我试图得到dict键,它的值在所有dict的值中都是max.我找到了两种方法,都不够优雅.
d= {'a':2,'b':5,'c':3}
# 1st way
print [k for k in d.keys() if d[k] == max(d.values())][0]
# 2nd way
print Counter(d).most_common(1)[0][0]
Run Code Online (Sandbox Code Playgroud)
有更好的方法吗?
任何人都可以告诉我如何排序这个:
{'a': [1, 2, 3], 'c': ['one', 'two'], 'b': ['blah', 'bhasdf', 'asdf'], 'd': ['asdf', 'wer', 'asdf', 'zxcv']}
Run Code Online (Sandbox Code Playgroud)
成
{'a': [1, 2, 3], 'b': ['blah', 'bhasdf', 'asdf'], 'c': ['one', 'two'],'d': ['asdf', 'wer', 'asdf', 'zxcv']}
Run Code Online (Sandbox Code Playgroud)
?谢谢!
更新1,代码示例:
所以我在做语言学.一篇文章分解为存储在数据库中的单词,并具有各种属性,包括段ID和句子ID.任务:尝试重建原始文本.
从DB中获取500个连续的单词
words = Words.objects.all()[wordId:wordId+500]
# I first create paragraphs, through which I can loop later in my django template,
# and in each para will be a list of words (also dictionaries).
# So i am trying to get a dictionary with values that …
Run Code Online (Sandbox Code Playgroud) 例如,我在list.txt中保存了一些链接:
d:\phpnow\htdocs\pm\includes\templates\slucky\common\tpl_gallery_display.php
d:\phpnow\htdocs\pm\includes\templates\slucky\common\tpl_main_page.php
d:\phpnow\htdocs\pm\includes\templates\slucky\templates\tpl_main_page.php
d:\phpnow\htdocs\pm\includes\templates\slucky\templates\tpl_product_info_display.php
d:\phpnow\htdocs\pm\includes\templates\slucky\templates\tpl_product_info_display2.php
Run Code Online (Sandbox Code Playgroud)
我想用记事本++打开它们.有没有插件可以完成这项工作?
python ×8
dictionary ×2
python-2.7 ×2
algorithm ×1
batch-file ×1
counter ×1
glob ×1
notepad++ ×1
path ×1
performance ×1
regex ×1
sorting ×1
string ×1
windows ×1