小编Ric*_*son的帖子

如何编写内联if语句用于打印?

我需要在布尔变量设置为时才打印一些东西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.

我在这里错过了什么?

python if-statement inline

342
推荐指数
12
解决办法
57万
查看次数

从整数列表中获取最接近给定值的数字

给定一个整数列表,我想找到哪个数字最接近我输入的数字:

>>> myList = [4, 1, 88, 44, 3]
>>> myNumber = 5
>>> takeClosest(myList, myNumber)
...
4
Run Code Online (Sandbox Code Playgroud)

有没有快速的方法来做到这一点?

python sorting integer list

142
推荐指数
5
解决办法
15万
查看次数

matplotlib在导入时需要时间

我刚刚升级到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 matplotlib

103
推荐指数
4
解决办法
5万
查看次数

在Python中,我可以调用导入模块的main()吗?

在Python中我有一个模块 myModule.py,我在其中定义了一些函数和一个main(),它接受一些命令行参数.

我通常从bash脚本中调用main().现在,我想将所有内容放入一个小包中,所以我想也许我可以将我的简单bash脚本转换为Python脚本并将其放入包中.

那么,我如何 MyFormerBashScript.py 的main()函数实际调用 myModule.py 的main()函数?我甚至可以这样做吗?我如何传递任何参数

python program-entry-point arguments module

70
推荐指数
4
解决办法
8万
查看次数

在bash脚本中存储Python脚本的返回值

我想从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)

python bash return return-value

53
推荐指数
5
解决办法
10万
查看次数

matplotlib:使加号更厚

在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 matplotlib markers

53
推荐指数
2
解决办法
4万
查看次数

Python:按索引过滤列表

在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 indexing list filter

41
推荐指数
5
解决办法
4万
查看次数

Python:SyntaxError:keyword不能是表达式

在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)

究竟出了什么问题?

python syntax syntax-error keyword

37
推荐指数
4
解决办法
13万
查看次数

HTTP保持活动超时

我可以指定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秒,对吧?我怎样才能设定这样的价值?

timeout get http telnet keep-alive

35
推荐指数
2
解决办法
8万
查看次数

matplotlib:在绘图时忽略异常值

我正在绘制各种测试的一些数据.有时在测试中我碰巧有一个异常值(比如说0.1),而所有其他值都小三个数量级.

使用matplotlib,我会对范围进行绘图 [0, max_data_value]

我怎样才能放大我的数据而不显示异常值,这会弄乱我的情节中的x轴?

我应该简单地采用95%并且[0, 95_percentile] 在x轴上具有范围吗?

python plot matplotlib outliers percentile

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