天道酬勤,学无止境

python

Python function default argument random value

In the following code, a random value is generated as expected: import random for i in range(10): print(random.randint(0,10)) However, this does not work if I use a function: import random def f(val: int = random.randint(0,10)): print(val) for i in range(10): f() Why is the result of the second code snippet always the same number? The most similar question I could find is this one, but it refers to a different language (I don't master) .

2022-01-25 05:41:27    分类:问答    python   function   random

How do I interpolate a variable into a string?

I have a username, "jerry" that I want to check if it's in my airtable's Username column: username = 'jerry' rows = at.get_all(formula="FIND('jerry', {Username})=1") # I want to replace the `jerry` there with the `username` var How can I do this?

2022-01-25 05:41:22    分类:问答    python

如何从网页中检索这些元素?(How to retrieve these elements from a webpage?)

问题 我有一个包含以下元素的 HTML 网页: <div class="content_page"> <a href="/earth" class="nametessera" >earth</a> </div> <div class="content_page"> <a href="/world" class="nametessera" >world</a> </div> <div class="content_page"> <a href="/planet" class="nametessera">planet</a> </div> ... 我需要检索 /earth、/world、/planet 等,因此我需要检索标签 A 的所有链接,其类为“nametessera”。 我怎样才能用 python 做到这一点? 回答1 简短的回答: 使用 beautifulSoup 解析页面,获取 url,然后使用 urlib2 或 pycurl 下载提到的 url。 [编辑:] 添加到下面的示例中,但仅使用 div 中包含的 href >>> alldiv = soup.findAll('div', { "class" : "content_page" }) >>> for div in alldiv: print div.a ... <a href="/earth" class=

2022-01-25 05:40:41    分类:技术分享    python

Decrypting in pgpy fails with "ValueError: Expected: ASCII-armored PGP data"

I have an OpenPGP encrypted file and its private key in a text file and know its passphrase. I tried this below code: import pgpy emsg = pgpy.PGPMessage.from_file('PGPEcrypted.txt') key,_ = pgpy.PGPKey.from_file('PrivateKey.txt') with key.unlock('passcode!'): print (key.decrypt(emsg).message) But while trying to execute I am getting following error: Traceback (most recent call last): File "D:\Project\PGP\pgp_test.py", line 4, in <module> key,_ = pgpy.PGPKey.from_file('SyngentaPrivateKey.txt') File "D:\Anaconda\lib\site-packages\pgpy\types.py", line 191, in from_file po = obj.parse(data) File

2022-01-25 05:39:53    分类:问答    python   encryption   gpgpu   pgp   openpgp

Python Scrapy 只会一遍又一遍地抓取相同的元素(Python Scrapy only scraping the same elements over and over again)

问题 我正在尝试学习 Scrapy,我正在 yelp 网站上学习这个 LINK 但是当 scrapy 运行时,它会一遍又一遍地抓取相同的电话、地址,而不是抓取不同的部分。 我使用的选择器是所有的“li”标签,属于页面的每个餐馆的特定类,每个LI标记包含我使用适当的选择器的每个餐厅信息,但Scape给了我的结果,重复形成2或3家餐馆。 出于某种原因,Scrapy 一遍又一遍地使用相同的部分,而当它们在 for 循环中完成时应该跳过它们。 这是代码 try: import scrapy from urlparse import urljoin except ImportError: print "\nERROR IMPORTING THE NESSASARY LIBRARIES\n" #scrapy.optional_features.remove('boto') url = raw_input('ENTER THE SITE URL : ') class YelpSpider(scrapy.Spider): name = 'yelp spider' start_urls = [url] def parse(self, response): SET_SELECTOR = '.regular-search-result' #Going over each li tags containg

2022-01-25 05:39:52    分类:技术分享    python   html   css   web-scraping   scrapy

Networkx: how get attribute color from the graph

I have graph and I need to get color of nodes. I try to use color = nx.get_node_attributes(G, 'color') But it returns empty dictionary. What I do wrong? Graph was generated with G = nx.erdos_renyi_graph(100, 0.05) pos = nx.spring_layout(G) nx.draw_networkx_nodes(G, pos, G.nodes(), node_size=20, node_color='b') nx.draw_networkx_edges(G,pos, alpha=0.3) plt.show()

2022-01-25 05:39:45    分类:问答    python   networkx

两个fft函数的卷积(Convolution of two fft function)

问题 对于卷积定理 F(xy) = F(x)*F(y) 但是在python上实现它之后 x = np.array([0,0,0,0,1, 2, 3, 4, 0 ,0,0,0]) y = np.array([0,0,0,0,-3, 5, -4, 0, 0, 0,0,0]) xy = x*y inverse_fft_xy = np.fft.ifft(np.convolve(np.fft.fft(x),np.fft.fft(y))) 将产生 xy array([ 0, 0, 0, 0, -3, 10, -12, 0, 0, 0, 0, 0]) inverse_fft_xy array([ 0.00000000e+00, -8.70383905e-01, 1.65925305e-02, -8.90888514e-01, 7.07822398e-02, -8.80447879e-01, 1.19687210e-01, 3.09247006e+00, -9.54481834e+00, -5.81203213e+00, 2.15726342e+01, -1.47366137e+01, -1.03012447e+01, 2.76823117e+00, -1.42560168e+00, 4.98000293e-01, -1.18537317e+00, 2.02675981e-01, -9

2022-01-25 05:39:37    分类:技术分享    python   numpy   fft

How to get new input to generator in Python without create a new generator

I try to write code that gets a list and generates all of this transformations by using yield statement. The problem is when I want to get new input to generator by using send function, I continue to get the old input. def permute(items): permutations = [x for x in itertools.permutations(items)] permutations.sort() for n in permutations: yield (n) g = permute(['b','a','c']) print(next(g)) #('a', 'b', 'c') print(next(g)) #('a', 'c', 'b') g.send(['e','q','c']) print(next(g)) #('b', 'c', 'a') need to be ('c', 'e', 'q') How can I empty the permutation list and repeat to sorting permutations list

2022-01-25 05:39:30    分类:问答    python   yield

Turning a list of rows into columns

foo = ['123','123','123] I am attempting to turn foo into: revisedfoo = ['111', '222', '333'] This in effect is turning the "rows" into "columns": 111 222 333 I have attempted something along these lines: for j in range(len(foo)): for i in range(len(foo)): revisedfoo.append(foo[i][j]) But it leaves me with: revisedfoo = ['1','1','1','2','2','2','3','3','3'] Any help would be appreciated!

2022-01-25 05:39:08    分类:问答    python   python-3.x

使用 PIL 保存时修改路径名(Modify path name when saving with PIL)

问题 我正在从图像中提取 RGB 通道并将它们保存为灰度 png 文件,但我无法保存它们。 这是我的代码: listing = os.listdir(path1) for f in listing: im = Image.open(path1 + f) red, green, blue = im.split() red = red.convert('LA') green = green.convert('LA') blue = blue.convert('LA') red.save(path2 + f + 'r', 'png') green.save(path2 + f + 'g', 'png') blue.save(path2 + f + 'b','png') 其中path1和path2分别是图像文件夹和保存目的地。 我想要做的是将img.png颜色通道的黑白版本保存到imgr.png 、 imgg.png 、 imgb.png ,但我用这段代码得到的是img.pngr 、 img.pngg 、 img.pngb . 任何帮助,将不胜感激。 回答1 你可以这样做: import os listing = os.listdir(path1) for f in listing: im = Image.open(os.path.join(path1, f)) red, green

2022-01-25 05:37:41    分类:技术分享    python   python-2.7   python-imaging-library