我刚刚在Mac上将Python更新为2.6.4.我从dmg包安装.
二进制文件似乎没有正确设置我的Python路径,所以我添加'/usr/local/lib/python2.6/site-packages'
了.bash_profile
>>> pprint.pprint(sys.path)
['',
'/Users/Bryan/work/django-trunk',
'/usr/local/lib/python2.6/site-packages',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages']
Run Code Online (Sandbox Code Playgroud)
显然,这不是所有必需的路径,因为我无法运行iPython.
$ ipython
Traceback (most recent call last):
File "/usr/local/bin/ipython", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named `pkg_resources`
Run Code Online (Sandbox Code Playgroud)
我已完成谷歌搜索,我无法弄清楚如何安装pkg_resources
或确保它在路上.
我需要做些什么来解决这个问题?
在那里,做关于Python的内置函数python.array
什么argsort()
上呢numpy.array
?
我可以.map(func)
在df中的任何列上使用,例如:
df=DataFrame({'a':[1,2,3,4,5,6],'b':[2,3,4,5,6,7]})
df['a']=df['a'].map(lambda x: x > 1)
Run Code Online (Sandbox Code Playgroud)
我还可以:
df['a'],df['b']=df['a'].map(lambda x: x > 1),df['b'].map(lambda x: x > 1)
Run Code Online (Sandbox Code Playgroud)
是否有更多的pythonic方法将函数应用于所有列或整个帧(没有循环)?
我可以将一个import语句放在一个字符串中,执行它,它可以工作(打印一个随机数字):
code = """
import random
def f():
print random.randint(0,9)
"""
def f():
pass
exec code
f()
Run Code Online (Sandbox Code Playgroud)
现在,如果我把exec code
和f()
自己的功能和调用它,这是行不通的.
def test():
exec code
f()
test()
Run Code Online (Sandbox Code Playgroud)
它说NameError: global name 'random' is not defined
.知道发生了什么事吗?谢谢
(不要与itertools.chain混淆)
我正在阅读以下内容:http: //en.wikipedia.org/wiki/Method_chaining
我的问题是:在python中实现方法链的最佳方法是什么?
这是我的尝试:
class chain():
def __init__(self, my_object):
self.o = my_object
def __getattr__(self, attr):
x = getattr(self.o, attr)
if hasattr(x, '__call__'):
method = x
return lambda *args: self if method(*args) is None else method(*args)
else:
prop = x
return prop
list_ = chain([1, 2, 3, 0])
print list_.extend([9, 5]).sort().reverse()
"""
C:\Python27\python.exe C:/Users/Robert/PycharmProjects/contests/sof.py
[9, 5, 3, 2, 1, 0]
"""
Run Code Online (Sandbox Code Playgroud)
一个问题是如果调用method(*args)
修改self.o
但不返回None
.(然后我应该返回self
或返回什么method(*args)
返回).
有没有人有更好的方法来实现链接?可能有很多方法可以做到这一点.
我应该假设一个方法总是返回None …
我安装了Python 2.7和NumPy.我已经为SciPy下载了预构建的二进制文件,但安装脚本失败并显示以下错误:
(http://www.netlib.org/blas/)
找不到Blas 库.可以在numpy/distutils/site.cfg
文件(部分[blas]
)中指定搜索库的目录,也可以通过设置BLAS环境变量来指定
.
我真的不知道这个愚弄它.我认为这是一个简单的安装过程,但似乎不是.我用Google搜索了BLAS环境变量,但找不到任何合适的东西.任何帮助表示赞赏.
麦克风
编辑:没关系,我找到了一个非官方的安装程序exe.
我有这样一个问题:
有一个类的元素列表CAnswer
(不需要描述类),我需要对它进行洗牌,但是有一个约束 - 列表中的一些元素已经CAnswer.freeze
设置为True
,并且这些元素不能被洗牌,而是保留在它们的上面原始职位.所以,让我们说,对于给定的列表:
[a, b, c, d, e, f]
Run Code Online (Sandbox Code Playgroud)
如果所有元素都是CAnswer
,但是c.freeze == True
,对于其他元素freeze == False
,可能的结果可能是:
[e, a, c, f, b, d]
Run Code Online (Sandbox Code Playgroud)
所以索引为2的元素仍处于其位置.
实现它的最佳算法是什么?
先感谢您 :)
让一个数组:
a =np.array([[1,2],[3,-5],[6,-15],[10,7]])
Run Code Online (Sandbox Code Playgroud)
为了获得第二列元素高于-6的行,可以这样做
>>> a[a[:,1]>-6]
array([[ 1, 2],
[ 3, -5],
[10, 7]])
Run Code Online (Sandbox Code Playgroud)
但如何在-6; 3之间获得第二个元素的线条?我试过了:
>>> a[3>a[:,1]>-6]
Run Code Online (Sandbox Code Playgroud)
而且(引起错误):
>>> np.ma.masked_inside(a,-6,3)
Run Code Online (Sandbox Code Playgroud)
这使:
masked_array(data =
[[-- --]
[-- --]
[6 -15]
[10 7]],
mask =
[[ True True]
[ True True]
[False False]
[False False]],
fill_value = 999999)
Run Code Online (Sandbox Code Playgroud)
但结果不太清楚
谢谢jp
使用布尔值对列表进行索引工作正常.虽然索引应该是整数.
以下是我在控制台中尝试的内容:
>>> l = [1,2,3,4,5,6]
>>>
>>> l[False]
1
>>> l[True]
2
>>> l[False + True]
2
>>> l[False + 2*True]
3
>>>
>>> l['0']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> type(True)
<type 'bool'>
Run Code Online (Sandbox Code Playgroud)
当我尝试l['0']
打印错误时,指数中预期的int类型,这是显而易见的.然后,即使是类型'True'
和'False'
存在Bool
,名单上的索引工作正常,并自动将其转换成int类型和执行操作.
请解释内部发生的事情.我是第一次发帖,所以请原谅我有任何错误.
我在使用pip安装python模块时遇到问题.以下是命令窗口的输出:
请注意,我在尝试安装GDAL模块之前立即安装了pip.
我在运行python 2.7的w7 64位机器上
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\jnunn\Desktop>python get-pip.py
Downloading/unpacking pip
Downloading pip-1.2.1.tar.gz (102Kb): 102Kb downloaded
Running setup.py egg_info for package pip
warning: no files found matching '*.html' under directory 'docs'
warning: no previously-included files matching '*.txt' found under directory
'docs\_build'
no previously-included directories found matching 'docs\_build\_sources'
Installing collected packages: pip
Running setup.py install for pip
warning: no files found matching '*.html' under directory 'docs'
warning: no previously-included files matching …
Run Code Online (Sandbox Code Playgroud)