小编Sil*_*ost的帖子

基数为10的int的文字无效:''

>>> n = ''.join(i for i in x if i.isdigit())
>>> n.isdigit()
True
>>> x.isdigit()
False

>>> previous = 0
>>> next = 100
>>> answer = 0


>>> for i in range(0,100):
...     answer += int(n[previous:next])
...     previous = next
...     next += 100
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ValueError: invalid literal for int() with base 10: ''
Run Code Online (Sandbox Code Playgroud)

为什么我收到此错误?如你所见,n是数字..

python casting slice

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

关于在Python中比较整数符号的函数实现的反馈

我做了一个小函数,给定一个元组,比较这个元组中的所有元素是否具有相同的符号.

例如,元组= [-1, -4, -6, -8]好,而[-1, -4, 12, -8]坏.我不确定我是否做了最聪明的实现,所以我知道这是要问的地方.

def check_consistent_categories(queryset):
    try:
        first_item = queryset[0].amount

        if first_item < 0:
            for item in queryset:
                if item > 0:
                    return False
            return True
        else:
            for item in queryset:
                if item < 0:
                    return False
            return True
    except:
        return False
Run Code Online (Sandbox Code Playgroud)

python sign

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

re.findall问题(重复)

我试图获取4chan网站的源代码,并获得线程的链接.

我有regexp的问题(不工作).资源:

import urllib2, re

req = urllib2.Request('http://boards.4chan.org/wg/')
resp = urllib2.urlopen(req)
html = resp.read()

print re.findall("res/[0-9]+", html)
#print re.findall("^res/[0-9]+$", html)
Run Code Online (Sandbox Code Playgroud)

问题是:

print re.findall("res/[0-9]+", html)
Run Code Online (Sandbox Code Playgroud)

给予重复.

我不能用:

print re.findall("^res/[0-9]+$", html)
Run Code Online (Sandbox Code Playgroud)

我已经阅读过python docs,但是他们没有帮助.

html python regex

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

切片插入问题,L [1:1]

练习一些python,这是一个很容易抓住的语言.

我有

>>> L = [1,2,3,4]
>>> L[1:1] = [1,2,3]
>>> L
[1, 1, 2, 3, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

所以第二行实际上L[1:1]是空列表,但是python如何理解插入[1,2,3]列表从中开始1.我猜有一些内部对我们来说是不透明的,显然,我猜它会L[1:1]返回索引的引用,1即使它返回一个空列表...

祝福,Umut

python slice

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

Python 3.1 - 大型列表采样期间的内存错误

输入列表可以超过100万个数字.当我使用较小的'重复'运行以下代码时,它很好;

def sample(x):
    length = 1000000 
    new_array = random.sample((list(x)),length)
    return (new_array)

def repeat_sample(x):    
    i = 0
    repeats = 100
    list_of_samples = []
    for i in range(repeats):
       list_of_samples.append(sample(x))
    return(list_of_samples)

repeat_sample(large_array)
Run Code Online (Sandbox Code Playgroud)

然而,使用诸如上述100的高重复,导致MemoryError.回溯如下;

Traceback (most recent call last):
  File "C:\Python31\rnd.py", line 221, in <module>
    STORED_REPEAT_SAMPLE = repeat_sample(STORED_ARRAY)
  File "C:\Python31\rnd.py", line 129, in repeat_sample
    list_of_samples.append(sample(x))
  File "C:\Python31\rnd.py", line 121, in sample
    new_array = random.sample((list(x)),length)
  File "C:\Python31\lib\random.py", line 309, in sample
    result = [None] * k
MemoryError
Run Code Online (Sandbox Code Playgroud)

我假设我的内存不足.我不知道如何解决这个问题.

感谢您的时间!

python

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

不太清楚%s在Python中有什么意义,帮忙吗?

我正在从一本书中学习Python,我无法弄清楚使用%s来列出列表,字符串,字典等中的特定项目是什么意思.

例如:

names = ["jones", "cohen", "smith", "griffin"]

print(names[1])
print("%s" % names[1])
Run Code Online (Sandbox Code Playgroud)

这两个命令都打印出"cohen",使用%s的重点是什么?

python syntax string-formatting

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

Python 中非英语语言的正则表达式匹配

我正在尝试在 python 脚本中捕获并匹配俄语字符。由于俄语字符不属于 [aZ] 类型,我应该使用什么正则表达式来匹配它们。我不能使用 (.*) 因为它会匹配所有内容。

linkpat = re.compile('name=[a-Z]+;size=[0-9]+')
Run Code Online (Sandbox Code Playgroud)

python regex unicode python-2.x

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

如何处理"getaddrinfo失败"?

你好,我有问题.我使用mechanize,python 2.7连接一些网站(代码现在不重要)我有网站列表,我现在连接到它们.当它发生时我的列表中的网站不存在我得到错误:

urllib2.URLError:[Errno 11004] getaddrinfo失败

我尝试通过这样做来处理它:

             except mechanize.URLError, e:
                    result = str(e.reason)
Run Code Online (Sandbox Code Playgroud)

要么

             except urllib2.URLError, e:
                    result = str(e.reason)
Run Code Online (Sandbox Code Playgroud)

甚至

             except Exception, e:
                    result = str(e)
Run Code Online (Sandbox Code Playgroud)

但它只是不想工作.

怎么解决这个?当发生此错误时,我只想打印"连接失败"之类的内容并移动到列表中的下一个地址.如何捕获此错误except

python urllib urllib2 urlopen

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

从方法返回元组

我正在编写一个方法,它会在成功但None失败时返回元组.我还没有最终确定None(作为一个失败的案例返回)但它是其中一个选择.我们可以-1,-1为失败案件返回吗?我正在寻找最好的pythonic方法来实现这一点,以便拆包很容易.

请告诉我们如何改进它.伪代码如下

 def myFunc(self):
     if self.validate() != 0:
         return
     x,y = self.getXY()

     return x,y
Run Code Online (Sandbox Code Playgroud)

python

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

如何使用PowerShell将部分添加到web.config?

如何system.serviceModel在web.config中添加一个部分(如果它尚不存在)?

之前:

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

后:

<system.serviceModel>
 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
 <bindings>
      <basicHttpBinding>
        <binding name="ValidationServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="Transport">
            <transport clientCredentialType="Ntlm" />
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
 <bindings>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

xml powershell sharepoint web-config

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