所以以下让我感到困惑.
#!/usr/bin/python
test = [0, 0, 0, 1, 2, 3, 4, 5, 6]
test1 = [0, 0, 0, 1, 2, 3, 4, 5, 6]
for _dummy in test:
if(_dummy == 0):
test.pop()
for _dummy in test1:
if(_dummy == 0):
test1.pop(0)
print test
print test1
Run Code Online (Sandbox Code Playgroud)
结果
ubuntu-vm:~/sandbox$ ./test.py
[0, 0, 0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
也许,我从根本上误解了pop的实现方式.但我的理解是,它删除列表中给定索引处的项目,并返回它.如果未指定索引,则默认为最后一项.所以看起来在第一个循环中它应该从列表的左边删除3个项目,而在第二个循环中它应该从列表的末尾删除3个项目.
所以我是Python的新手并尝试使用Google Python在线课程[ https://developers.google.com/edu/python/regular-expressions]
我正在运行Python 2.7.5,当我尝试这个例子时,它有=>的问题.
我已经尝试搜索谷歌的解释,但已经空了.任何帮助,将不胜感激.
错误信息如下.
sandbox$./re-test.py
File "./re-test.py", line 8
match = re.search(r'iii', 'piiig') => found, match.group() == "iii"
^
Run Code Online (Sandbox Code Playgroud)
该计划的代码是:
#!/usr/bin/python
import re
## Search for pattern 'iii' in string 'piiig'.
## All of the pattern must match, but it may appear anywhere.
## On success, match.group() is matched text.
match = re.search(r'iii', 'piiig') => found, match.group() == "iii"
match = re.search(r'igs', 'piiig') => not found, match == None
## . = any char but \n
match …
Run Code Online (Sandbox Code Playgroud) 问题描述
尝试启动Flask时收到错误消息.
Traceback (most recent call last):
File "./run.py", line 3, in <module>
from app import app
File "/home/xxxxxx/xxxx.xxxxxxx.com/ClientTracker/app/__init__.py", line 13, in <module>
app.register_blueprint(admin)
File "/home/xxxxx/xxxxx.xxxxxxx.com/ClientTracker/env/local/lib/python2.7/site-packages/flask/app.py", line 65, in wrapper_func
return f(self, *args, **kwargs)
File "/home/xxxxx/xxxxx.xxxxxxx.com/ClientTracker/env/local/lib/python2.7/site-packages/flask/app.py", line 958, in register_blueprint
if blueprint.name in self.blueprints:
AttributeError: 'function' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)
这是从实现蓝图的更简单的层次结构迁移.我正在拆分前端和管理面板的功能.
我一步一步地建造了这个,并且双方都很好.开始迁移(功能和路线).移动一些代码后,我开始收到一条错误消息(基本上与上面相同,但不同的行).
故障排除
码
#ClientTracker/run.py
#!env/bin/python
from app import app
app.run(host='0.0.0.0', port=8080, debug=False)
Run Code Online (Sandbox Code Playgroud)
#ClientTracker/app/__init__.py
# Import flask and template operators
from …
Run Code Online (Sandbox Code Playgroud) 所以我有一个对象,该方法应该迭代在类中定义的列表.
当我使用简单的if语句时,我得到了预期的结果,但是,当我添加一个else语句时,我得到了奇怪的结果.
Class SomeClass(object):
def __init__(self):
self.config = ['something', 'this exists', 'some more stuff']
def check_this(self):
for line in self.config:
if "this exists" in line:
return True
Run Code Online (Sandbox Code Playgroud)
一旦到达列表中的第二个元素,上面的代码就返回True.
如果我将代码更改为以下.该方法返回False.
Class SomeClass(object):
def __init__(self):
self.config = ['something', 'this exists', 'some more stuff']
def check_this(self):
for line in self.config:
if "this exists" in line:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
我必须在这里遗漏一些东西.MAC OS X上的Python 2.7.6
我还在学习,所以要温柔.
给出以下代码.
str = "sometext"
it = iter(str)
print it
print len(list(it))
print len(list(it))
Run Code Online (Sandbox Code Playgroud)
我得到以下输出.
<iterator object at 0x1070b9990>
8
0
Run Code Online (Sandbox Code Playgroud)
为什么对象的内容只能使用一次?