我需要在布尔变量设置为时才打印一些东西True
.所以,看后这个,我试图用一个简单的例子:
>>> a = 100
>>> b = True
>>> print a if b
File "<stdin>", line 1
print a if b
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
如果我写的话也一样print a if b==True
.
我在这里错过了什么?
给定一个整数列表,我想找到哪个数字最接近我输入的数字:
>>> myList = [4, 1, 88, 44, 3]
>>> myNumber = 5
>>> takeClosest(myList, myNumber)
...
4
Run Code Online (Sandbox Code Playgroud)
有没有快速的方法来做到这一点?
我刚刚升级到matplotlib
(1.5.1)的最新稳定版本,每次我导入matplotlib我收到此消息:
/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Run Code Online (Sandbox Code Playgroud)
...总是停顿几秒钟.
这是预期的行为吗?以前是否也一样,但是没有印刷的信息?
在Python中我有一个模块 myModule.py,我在其中定义了一些函数和一个main(),它接受一些命令行参数.
我通常从bash脚本中调用main().现在,我想将所有内容放入一个小包中,所以我想也许我可以将我的简单bash脚本转换为Python脚本并将其放入包中.
那么,我如何从 MyFormerBashScript.py 的main()函数实际调用 myModule.py 的main()函数?我甚至可以这样做吗?我如何传递任何参数?
我想从bash脚本执行一个python脚本,我想将python脚本的输出存储在一个变量中.
在我的python脚本中,我将一些东西打印到屏幕上,最后我返回一个字符串:
sys.exit(myString)
Run Code Online (Sandbox Code Playgroud)
在我的bash脚本中,我执行了以下操作:
outputString=`python myPythonScript arg1 arg2 arg3 `
Run Code Online (Sandbox Code Playgroud)
但是当我检查我的值时outputString
,echo $outputString
我将获得Python脚本打印到屏幕的所有内容,而不是返回值myString
!
我该怎么做?
编辑:我需要字符串,因为它告诉我Python脚本创建的文件位于何处.我想做的事情如下:
fileLocation=`python myPythonScript1 arg1 arg2 arg1`
python myPythonScript2 $fileLocation
Run Code Online (Sandbox Code Playgroud) 在Matplotlib,我想绘制厚加号(或横),但在所提供的所述一个标记组过薄.
即使我增加它的大小,它也不会变得更厚.
# Draw median marker.
if plot_opts.get('bean_show_median', True):
ax.plot(pos, np.median(pos_data),
marker=plot_opts.get('bean_median_marker', '+'),
color=plot_opts.get('bean_median_color', 'r'))
Run Code Online (Sandbox Code Playgroud)
如果我添加一个额外的参数markersize=20
,标记只会拉伸.它会像以前一样薄.我可以把它做厚吗?
在Python中,我有一个元素aList
列表和一个索引列表myIndices
.有什么方法可以一次性检索所有项目中aList
的值作为索引myIndices
吗?
例:
>>> aList = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> myIndices = [0, 3, 4]
>>> aList.A_FUNCTION(myIndices)
['a', 'd', 'e']
Run Code Online (Sandbox Code Playgroud) 在Python脚本中我调用了一个函数rpy2
,但是我收到了这个错误:
#using an R module
res = DirichletReg.ddirichlet(np.asarray(my_values),alphas,
log=False, sum.up=False)
SyntaxError: keyword can't be an expression
Run Code Online (Sandbox Code Playgroud)
究竟出了什么问题?
我可以指定HTTP超时还是服务器强加一个值?例如,如果我这样做:
telnet my.server.net 80
Trying X.X.X.X...
Connected to my.server.net.
Escape character is '^]'.
GET /homepage.html HTTP/1.0
Connection: keep-alive
Host: my.server.net
HTTP/1.1 200 OK
Date: Thu, 03 Oct 2013 09:05:28 GMT
Server: Apache
Last-Modified: Wed, 15 Sep 2010 14:45:31 GMT
ETag: "1af210b-7b-4904d6196d8c0"
Accept-Ranges: bytes
Content-Length: 123
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html
[...]
Run Code Online (Sandbox Code Playgroud)
这条线:
Keep-Alive: timeout=15, max=100
Run Code Online (Sandbox Code Playgroud)
...指定最大超时为100秒,对吧?我怎样才能设定这样的价值?
我正在绘制各种测试的一些数据.有时在测试中我碰巧有一个异常值(比如说0.1),而所有其他值都小三个数量级.
使用matplotlib,我会对范围进行绘图 [0, max_data_value]
我怎样才能放大我的数据而不显示异常值,这会弄乱我的情节中的x轴?
我应该简单地采用95%并且[0, 95_percentile]
在x轴上具有范围吗?
python ×9
matplotlib ×3
list ×2
arguments ×1
bash ×1
filter ×1
get ×1
http ×1
if-statement ×1
indexing ×1
inline ×1
integer ×1
keep-alive ×1
keyword ×1
markers ×1
module ×1
outliers ×1
percentile ×1
plot ×1
return ×1
return-value ×1
sorting ×1
syntax ×1
syntax-error ×1
telnet ×1
timeout ×1