小编Rom*_*huk的帖子

Python数组切片用逗号?

我想知道在切割Python数组时逗号的用途是什么 - 我有一个似乎可行的例子,但对我来说看起来很奇怪的一行是

p = 20*numpy.log10(numpy.abs(numpy.fft.rfft(data[:2048, 0])))
Run Code Online (Sandbox Code Playgroud)

现在,我知道在切片数组时,第一个数字是开始,下一个是结束,最后一个是步骤,但结束号后面的逗号是什么?谢谢.

python numpy list slice

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

检查函数是否是某个对象的方法

如何检查函数是否是某个对象的方法?

例如:

def check_method(f):
    ...

check_method(lambda x: x + 1)           # >>> False
check_method(SomeClass().some_method)  # >>> True
Run Code Online (Sandbox Code Playgroud)

我的'helloworld'示例中的方法中有一些特殊属性(例如'im_self','__ self__'等).我可以依靠它们还是有更好的方法?

python methods function

12
推荐指数
2
解决办法
8688
查看次数

Python:导入文件夹的符号链接

我有一个文件夹A,其中包含一些Python文件和__init__.py.

如果我将整个文件夹A复制到其他文件夹B并在那里创建一个带有"import A"的文件,它就可以了.但现在我删除文件夹并移动到原始文件夹的符号链接.现在它不起作用,说"没有名为foo的模块".有谁知道如何使用符号链接进行导入?

python import symlink

11
推荐指数
2
解决办法
1万
查看次数

具有大整数的按位运算

我正在实现BER压缩整数的解码,最近我发现了与大整数的按位运算相关的奇怪的JavaScript行为.

例如:

var a = 17516032;          // has 25 bits
alert(a << 7)              // outputs -2052915200
alert(a * 128)             // outputs  2242052096
alert(2242052096 >> 16)    // outputs -31325
alert(2242052096 / 65536)  // outputs  34211
Run Code Online (Sandbox Code Playgroud)

虽然第一种解决方法(乘法而不是左移)是可以接受的,但第二种不是.

为什么会这样?怎么忍受呢?

javascript integer bit-manipulation

10
推荐指数
2
解决办法
2435
查看次数

如何在Python中使用与对象名称相同的字符串来访问对象本身?

例如,在下面的代码中,我想使用x作为参考获得列表[1,2,3].

In[1]: pasta=[1,2,3]
In:[2]: pasta
Out[2]: [1, 2, 3]
In [3]: x='pas'+'ta'
In [4]: x
Out[4]: 'pasta'
Run Code Online (Sandbox Code Playgroud)

python reference object

10
推荐指数
1
解决办法
338
查看次数

Python:使用自定义比较器对字典数组进行排序?

我有以下Python字典数组:

myarr = [ { 'name': 'Richard', 'rank': 1 },
{ 'name': 'Reuben', 'rank': 4 },
{ 'name': 'Reece', 'rank': 0 },
{ 'name': 'Rohan', 'rank': 3 },
{ 'name': 'Ralph', 'rank': 2 },
{ 'name': 'Raphael', 'rank': 0 },
{ 'name': 'Robin', 'rank': 0 } ]
Run Code Online (Sandbox Code Playgroud)

我想按等级值对其进行排序,排序如下:1-2-3-4-0-0-0.

如果我尝试:

sorted_master_list = sorted(myarr, key=itemgetter('rank'))
Run Code Online (Sandbox Code Playgroud)

然后列表按0-0-0-1-2-3-4的顺序排序.

如何定义自定义比较器函数以将零推到列表底部?我想知道我是否可以使用像methodcaller这样的东西.

python

10
推荐指数
1
解决办法
5590
查看次数

如何在字符串中间抓取数字?(蟒蛇)

random string
this is 34 the string 3 that, i need 234
random string
random string
random string
random string

random string
this is 1 the string 34 that, i need 22
random string
random string
random string
random string

random string
this is 35 the string 55 that, i need 12
random string
random string
random string
random string
Run Code Online (Sandbox Code Playgroud)

在一个字符串中有多行.其中一条线重复,但每次都有不同的数字.我想知道如何将数字存储在这些行中.数字将始终位于行中的相同位置,但可以是任意数量的数字.

编辑:随机字符串中也可以包含数字.

python regex

8
推荐指数
1
解决办法
1612
查看次数

'str'对象没有属性'__dict__'

我想在Python中将字典序列化为JSON.我有这个'str'对象没有属性' dict '错误.这是我的代码......

from django.utils import simplejson

class Person(object):
    a = ""

person1 = Person()
person1.a = "111"

person2 = Person()
person2.a = "222"

list = {}
list["first"] = person1
list["second"] = person2

s = simplejson.dumps([p.__dict__ for p in list])
Run Code Online (Sandbox Code Playgroud)

例外是;

Traceback (most recent call last):
  File "/base/data/home/apps/py-ide-online/2.352580383594527534/shell.py", line 380, in post
    exec(compiled_code, globals())
  File "<string>", line 17, in <module>
AttributeError: 'str' object has no attribute '__dict__'
Run Code Online (Sandbox Code Playgroud)

python serialization json dictionary

7
推荐指数
2
解决办法
9935
查看次数

用于u""文字的编码

考虑下一个例子:

>>> s = u"????"
>>> s
u'\xe1\xe0\xe1\xe0'
>>> print s
áàáà
Run Code Online (Sandbox Code Playgroud)

cp1251在空闲时使用编码,但似乎解释器实际上用于latin1创建unicode字符串:

>>> print s.encode('latin1')
????
Run Code Online (Sandbox Code Playgroud)

为什么这样?这种行为有规格吗?

CPython,2.7.


编辑

我实际上寻找的代码是

>>> u'\xe1\xe0\xe1\xe0' == u'\u00e1\u00e0\u00e1\u00e0'
True
Run Code Online (Sandbox Code Playgroud)

似乎在使用latin1编解码器对unicode进行编码时,所有unicode点都少于256,因此只会产生我之前输入的字节.

python unicode encoding

6
推荐指数
1
解决办法
448
查看次数

Python - 交叉字符串

尝试编写一个带有两个字符串的for函数,并返回按它们在第一个字符串中出现的顺序相交的字符.

这是我试过的:

def strIntersection(str1, str2):
    for i in str1:
        str3 = ''
        str3 = str3.join(i for i in str1 if i in str2 not in str3)
    return str3

str1 = 'asdfasdfasfd'
str2 = 'qazwsxedc'

strIntersection(str1,str2)

=> 'asdasdasd'
Run Code Online (Sandbox Code Playgroud)

但是我只希望交叉字符出现一次并按照第一个字符串的顺序即出现.'ASD'

有人可以帮忙吗?

我在其他论坛上发现了一些类似的问题,但解决方案似乎都涉及列表,而我希望我的输出是一个字符串

python string intersection

6
推荐指数
3
解决办法
1万
查看次数