小编Avo*_*yan的帖子

Python:迭代器返回None

这是我的代码:

class Prizes(object):
    def __init__(self, purchases, n, d):
        self.p = purchases
        self.n = n
        self.d = d
        self.x = 1

    def __iter__(self):
        return self

    def __next__(self):
        print(self.x)

        if self.x % self.n == 0 and self.p[self.x - 1] % self.d == 0:
            self.x = self.x + 1
            return self.x - 1
        elif self.x > len(self.p):
            raise StopIteration

        self.x = self.x + 1

def superPrize(purchases, n, d):
  return list(Prizes(purchases, n, d))
Run Code Online (Sandbox Code Playgroud)

用法示例:

superPrize([12, 43, 13, 465, 1, 13], 2, 3)
Run Code Online (Sandbox Code Playgroud)

输出应该是:

[4] …
Run Code Online (Sandbox Code Playgroud)

python iterator class generator nonetype

5
推荐指数
1
解决办法
1429
查看次数

使用 regex sub (Python) 复制字符串中的数字

我有一个s="gh3wef2geh4ht". 我如何s="gh333wef22geh4444ht"通过使用子接收。我试过这个正则表达式。我做错了什么?

s=re.sub(r"(\d)",r"\1{\1}",s)
Run Code Online (Sandbox Code Playgroud)

python regex

4
推荐指数
1
解决办法
220
查看次数

Python:如何使用 Selenium 打印所有源代码

driver.page_source不要返回所有的源代码。它只是详细地打印了部分代码,但它遗漏了大部分代码。我怎样才能解决这个问题?

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
  def htmlToLuna():
  url ='https://codefights.com/tournaments/Xph7eTJQssbXjDLzP/A'
  driver = webdriver.Chrome('C:\\Python27\\chromedriver\\chromedriver.exe')
  driver.get(url)
  web=open('web.txt','w')
  web.write(driver.page_source)
  print driver.page_source
  web.close()

print htmlToLuna()
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-webdriver

1
推荐指数
1
解决办法
4862
查看次数

Python:如何从第i个到第j个匹配替换字符?

例如,如果我有:

"+----+----+---+---+--+"
Run Code Online (Sandbox Code Playgroud)

是否有可能从第二到第四替换+-

如果我有

"+----+----+---+---+--+"
Run Code Online (Sandbox Code Playgroud)

而我想拥有

"+-----------------+--+"
Run Code Online (Sandbox Code Playgroud)

我必须从第2到第4更换+-.是否有可能通过正则表达式实现这一目标?如何?

python regex

1
推荐指数
1
解决办法
108
查看次数

使用正则表达式匹配字符串中单词的最后 3 个字母

我有一个字符串“cough\tbough”或“quod erat Demonstrandum\ tand that,女士们先生们,是我的备忘录的结尾”。我需要检查这句话的最后一个单词的最后 3 个字母是否相等。但是反斜杠让我很困惑。

pairOfLines="cough\tbough"
pattern = ?????
match = re.match(pattern, pairOfLines)
return match.group(1) == match.group(2)
Run Code Online (Sandbox Code Playgroud)

python regex string

0
推荐指数
1
解决办法
1932
查看次数