在下面的例子中,我希望所有元素都是元组,为什么元组只包含一个字符串时转换为字符串?
>>> a = [('a'), ('b'), ('c', 'd')]
>>> a
['a', 'b', ('c', 'd')]
>>>
>>> for elem in a:
... print type(elem)
...
<type 'str'>
<type 'str'>
<type 'tuple'>
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Python应用程序,我希望在其上看到实时统计信息.我想用Flask它来使它易于使用和理解.
问题是我的Flask服务器应该从我的Python应用程序的最开始开始,并在最后停止.它应该如下所示:
def main():
""" My main application """
from watcher.flask import app
# watcher.flask define an app as in the Quickstart flask documentation.
# See: http://flask.pocoo.org/docs/0.10/quickstart/#quickstart
app.run() # Starting the flask application
do_my_stuff()
app.stop() # Undefined, for the idea
Run Code Online (Sandbox Code Playgroud)
因为我需要我的应用程序上下文(用于统计),我不能使用multiprocessing.Process.然后我试图使用a threading.Thread,但看起来Werkzeug不喜欢它:
* Running on http://0.0.0.0:10079/
Exception in thread Flask Server:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File ".../develop-eggs/watcher.flask/src/watcher/flask/__init__.py", line 14, …Run Code Online (Sandbox Code Playgroud) Python是一种"以空格分隔"的语言.然而,使用分号被允许的.例如,以下工作但不赞成:
print("Hello!");
print("This is valid");
Run Code Online (Sandbox Code Playgroud)
我已经使用python好几年了,而且我用过分号的唯一一次是用python生成一次性命令行脚本:
python -c "import inspect, mymodule; print(inspect.getfile(mymodule))"
Run Code Online (Sandbox Code Playgroud)
或在SO的评论中添加代码(即"你应该尝试import os; print os.path.join(a,b)")
我在这个答案中也注意到一个类似的问题,分号也可以用来制作一个行if块,如
if x < y < z: print(x); print(y); print(z)
Run Code Online (Sandbox Code Playgroud)
这对我给出的两个用法示例(命令行脚本和注释)很方便.
以上示例用于以段落形式传递代码或制作简短的代码段,但不是我在生产代码库中所期望的.
这是我的问题:在python中,是否有理由在生产代码中使用分号?我想他们仅仅因为我引用的原因而被添加到语言中,但是Guido总是有可能考虑到更为宏伟的计划.没有意见; 我正在寻找分号有用的现有代码中的示例,或者来自python docs或Guido关于分号使用的某种语句.
我有以下代码,这是基于我在这里找到的一个例子,但是当我运行它时,我得到一个错误.请帮忙,我确定它非常简单:
def listener(port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('',port))
sock.settimeout(1) # n second(s) timeout
try:
while True:
data, addr = sock.recvfrom(1024)
print data
except socket.timeout:
print 'Finished'
def startListenerThread(port):
threading.Thread(target=listener, args=(port)).start()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 477, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: listener() argument after * must be a sequence, not int
Run Code Online (Sandbox Code Playgroud) 我正在尝试计算数据帧中第一列和其他列之间的回归的滚动r平方(第一列和第二列,第一列和第三列等)但是当我尝试线程时,它一直告诉我错误
TypeError:*之后的ParallelRegression()参数必须是可迭代的,而不是int".
我想知道如何解决这个问题?非常感谢!
import threading
totalThreads=3 #three different colors
def ParallelRegression(threadnum):
for i in range(threadnum):
res[:,i]=sm.OLS(df.iloc[:,0], df.iloc[:,i+1]).fit().rsquared
threads=[]
for threadnum in range(totalThreads):
t=threading.Thread(target=ParallelRegression,args=(threadnum))
threads.append(t)
t.start()
for threadnum in range(totalThreads):
threads[threadnum].join()
Run Code Online (Sandbox Code Playgroud)
查看下面链接图片中的数据摘要(df):

python multithreading iterable regression python-multithreading