我正在学习Python编程语言,而且我遇到了一些我不太了解的东西.
在如下方法中:
def method(self, blah):
def __init__(?):
....
....
Run Code Online (Sandbox Code Playgroud)
怎么self办?这是什么意思?这是强制性的吗?
该__init__方法有什么作用?为什么有必要?(等等.)
我认为它们可能是OOP结构,但我不太了解.
使用virtualenv,我使用默认版本的Python(2.7)运行我的项目.在一个项目中,我需要使用Python 3.4.
我brew install python3以前在Mac上安装它.现在,我如何创建一个使用新版本的virtualenv?
例如sudo virtualenv envPython3
如果我尝试:
virtualenv -p python3 test
Run Code Online (Sandbox Code Playgroud)
我明白了:
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
import os
File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It …Run Code Online (Sandbox Code Playgroud) 如果您只是想尝试 - 除非不处理异常,您如何在Python中执行此操作?
以下是正确的方法吗?
try:
shutil.rmtree(path)
except:
pass
Run Code Online (Sandbox Code Playgroud) 可能重复:
Python:递增和递减运算符的行为
当我回顾我的VB6时,我总是嘲笑自己,并且想:"现代语言不允许用双加号增加?":
number++
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,我在Python文档中找不到任何相关内容.我真的要服从自己number = number + 1吗?人们不会使用这种++/--符号吗?
我正在寻找最快的方法来了解列表中是否存在值(列表中包含数百万个值)以及它的索引是什么?我知道列表中的所有值都是唯一的,就像我的例子一样.
我尝试的第一种方法是(在我的实际代码中为3.8秒):
a = [4,2,3,1,5,6]
if a.count(7) == 1:
b=a.index(7)
"Do something with variable b"
Run Code Online (Sandbox Code Playgroud)
我尝试的第二种方法是(快2倍:我的实际代码为1.9秒):
a = [4,2,3,1,5,6]
try:
b=a.index(7)
except ValueError:
"Do nothing"
else:
"Do something with variable b"
Run Code Online (Sandbox Code Playgroud)
Stackoverflow用户提出的方法(我的实际代码为2.74秒):
a = [4,2,3,1,5,6]
if 7 in a:
a.index(7)
Run Code Online (Sandbox Code Playgroud)
在我的实际代码中,第一种方法需要3.81秒,第二种方法需要1.88秒.这是一个很好的改进但是:
我是Python /脚本的初学者,我想知道是否有最快的方法可以做同样的事情并节省更多的处理时间?
我的应用程序更具体的解释:
在blender的API中,a可以访问粒子列表:
particles = [1, 2, 3, 4, etc.]
Run Code Online (Sandbox Code Playgroud)
从那里,我可以访问它的位置:
particles[x].location = [x,y,z]
Run Code Online (Sandbox Code Playgroud)
我通过搜索每个粒子的位置测试每个粒子是否存在邻居,如:
if [x+1,y,z] in particles.location
"Find the identity of this neighbour particle in x:the particle's index
in the array"
particles.index([x+1,y,z])
Run Code Online (Sandbox Code Playgroud) 似乎有两种不同的方法将字符串转换为字节,如TypeError的答案所示:'str'不支持缓冲区接口
哪种方法更好或更好Pythonic?或者只是个人喜好?
b = bytes(mystring, 'utf-8')
b = mystring.encode('utf-8')
Run Code Online (Sandbox Code Playgroud) 我需要在Python程序中模拟do-while循环.不幸的是,以下简单的代码不起作用:
list_of_ints = [ 1, 2, 3 ]
iterator = list_of_ints.__iter__()
element = None
while True:
if element:
print element
try:
element = iterator.next()
except StopIteration:
break
print "done"
Run Code Online (Sandbox Code Playgroud)
而不是"1,2,3,完成",它打印以下输出:
[stdout:]1
[stdout:]2
[stdout:]3
None['Traceback (most recent call last):
', ' File "test_python.py", line 8, in <module>
s = i.next()
', 'StopIteration
']
Run Code Online (Sandbox Code Playgroud)
我能做些什么来捕获'stop iteration'异常并正确地打破while循环?
以下将伪代码示为可能需要这样的事物的示例.
状态机:
s = ""
while True :
if state is STATE_CODE :
if "//" in s :
tokens.add( TOKEN_COMMENT, s.split( "//" )[1] )
state = …Run Code Online (Sandbox Code Playgroud) 我在Python中有两个列表,如下所示:
temp1 = ['One', 'Two', 'Three', 'Four']
temp2 = ['One', 'Two']
Run Code Online (Sandbox Code Playgroud)
我需要创建第三个列表,其中包含第一个列表中不存在于第二个列表中的项目.从我必须得到的例子:
temp3 = ['Three', 'Four']
Run Code Online (Sandbox Code Playgroud)
有没有循环和检查的快速方法?
我知道pandas旨在加载完全填充DataFrame但我需要创建一个空的DataFrame,然后逐个添加行.做这个的最好方式是什么 ?
我成功创建了一个空的DataFrame:
res = DataFrame(columns=('lib', 'qty1', 'qty2'))
Run Code Online (Sandbox Code Playgroud)
然后我可以添加一个新行并填充一个字段:
res = res.set_value(len(res), 'qty1', 10.0)
Run Code Online (Sandbox Code Playgroud)
它工作但似乎很奇怪: - /(它添加字符串值失败)
如何向我的DataFrame添加新行(具有不同的列类型)?
如何使Python类可序列化?
一个简单的课程:
class FileItem:
def __init__(self, fname):
self.fname = fname
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能得到输出:
>>> import json
>>> my_file = FileItem('/foo/bar')
>>> json.dumps(my_file)
TypeError: Object of type 'FileItem' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
没有错误(__CODE__)
python ×10
list ×2
performance ×2
python-3.x ×2
append ×1
dataframe ×1
do-while ×1
exception ×1
json ×1
oop ×1
pandas ×1
self ×1
set ×1
string ×1
syntax ×1
try-except ×1
virtualenv ×1
while-loop ×1