Questions tagged «python»

Python是一种多范式,动态类型的多用途编程语言。它旨在快速学习,理解和使用并强制使用干净统一的语法。请注意,Python 2自2020年1月1日起已不再受支持。不过,对于特定于版本的Python问题,请添加[python-2.7]或[python-3.x]标签。使用Python变体或库(例如Jython,PyPy,Pandas,Numpy)时,请将其包含在标签中。

2
用python 3解开python 2对象
我想知道是否有一种方法可以加载在Python 2.4和Python 3.4中腌制的对象。 我一直在大量公司遗留代码上运行2to3,以使其保持最新状态。 完成此操作后,在运行文件时出现以下错误: File "H:\fixers - 3.4\addressfixer - 3.4\trunk\lib\address\address_generic.py" , line 382, in read_ref_files d = pickle.load(open(mshelffile, 'rb')) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 1: ordinal not in range(128) 在争用中查看腌制的对象,它dict在中dict,包含键和type值str。 所以我的问题是:有没有办法用python 3.4加载最初在python 2.4中腌制的对象?

8
如何装饰一堂课?
在Python 2.5中,有没有办法创建装饰类的装饰器?具体来说,我想使用装饰器将成员添加到类中,并更改构造函数以获取该成员的值。 寻找类似以下的内容(在“ Foo类:”上存在语法错误: def getId(self): return self.__id class addID(original_class): def __init__(self, id, *args, **kws): self.__id = id self.getId = getId original_class.__init__(self, *args, **kws) @addID class Foo: def __init__(self, value1): self.value1 = value1 if __name__ == '__main__': foo1 = Foo(5,1) print foo1.value1, foo1.getId() foo2 = Foo(15,2) print foo2.value1, foo2.getId() 我想我真正想要的是在Python中执行类似C#接口的方法。我想我应该改变我的范式。

12
Python中的多元线性回归
我似乎找不到任何进行多元回归的python库。我发现的唯一的东西只是做简单的回归。我需要针对几个自变量(x1,x2,x3等)对我的因变量(y)进行回归。 例如,使用以下数据: print 'y x1 x2 x3 x4 x5 x6 x7' for t in texts: print "{:>7.1f}{:>10.2f}{:>9.2f}{:>9.2f}{:>10.2f}{:>7.2f}{:>7.2f}{:>9.2f}" / .format(t.y,t.x1,t.x2,t.x3,t.x4,t.x5,t.x6,t.x7) (以上输出:) y x1 x2 x3 x4 x5 x6 x7 -6.0 -4.95 -5.87 -0.76 14.73 4.02 0.20 0.45 -5.0 -4.55 -4.52 -0.71 13.74 4.47 0.16 0.50 -10.0 -10.96 -11.64 -0.98 15.49 4.18 …

11
UnicodeDecodeError:'ascii'编解码器无法解码位置13的字节0xe2:序数不在范围内(128)
我正在使用NLTK在我的文本文件中执行kmeans聚类,其中每一行都被视为文档。例如,我的文本文件是这样的: belong finger death punch <br> hasty <br> mike hasty walls jericho <br> jägermeister rules <br> rules bands follow performing jägermeister stage <br> approach 现在我要运行的演示代码是这样的: import sys import numpy from nltk.cluster import KMeansClusterer, GAAClusterer, euclidean_distance import nltk.corpus from nltk import decorators import nltk.stem stemmer_func = nltk.stem.EnglishStemmer().stem stopwords = set(nltk.corpus.stopwords.words('english')) @decorators.memoize …
129 python  python-2.7 



7
嵌套的defaultdict defaultdict
有没有办法使defaultdict也成为defaultdict的默认值?(即无限级递归defaultdict?) 我希望能够做到: x = defaultdict(...stuff...) x[0][1][0] {} 因此,我可以做到x = defaultdict(defaultdict),但这仅是第二层: x[0] {} x[0][0] KeyError: 0 有一些食谱可以做到这一点。但是,仅使用常规的defaultdict参数就可以做到吗? 请注意,这是在问如何执行无限级递归defaultdict,因此它与Python不同:defaultdict的defaultdict?,这是执行两级defaultdict的方法。 我可能最终会使用束模式,但是当我意识到自己不知道该怎么做时,这引起了我的兴趣。

9
从列中的字符串中删除不需要的部分
我正在寻找一种有效的方法来从DataFrame列的字符串中删除不需要的部分。 数据如下: time result 1 09:00 +52A 2 10:00 +62B 3 11:00 +44a 4 12:00 +30b 5 13:00 -110a 我需要将这些数据修剪为: time result 1 09:00 52 2 10:00 62 3 11:00 44 4 12:00 30 5 13:00 110 我试过了.str.lstrip('+-')。str.rstrip('aAbBcC'),但出现错误: TypeError: wrapper() takes exactly 1 argument (2 given) 任何指针将不胜感激!


4
从列表中随机选择50个项目写入文件
到目前为止,我已经弄清楚了如何导入文件,创建新文件以及使列表随机化。 我在从列表中随机选择50个项目以写入文件时遇到麻烦吗? def randomizer(input,output1='random_1.txt',output2='random_2.txt',output3='random_3.txt',output4='random_total.txt'): #Input file query=open(input,'r').read().split() dir,file=os.path.split(input) temp1 = os.path.join(dir,output1) temp2 = os.path.join(dir,output2) temp3 = os.path.join(dir,output3) temp4 = os.path.join(dir,output4) out_file4=open(temp4,'w') random.shuffle(query) for item in query: out_file4.write(item+'\n') 因此,如果总随机文件为 example: random_total = ['9','2','3','1','5','6','8','7','0','4'] 我想要3个文件(out_file1 | 2 | 3),其中第一个随机集为3,第二个随机集为3,第三个随机集为3(对于此示例,但我要创建的文件应该有50个) random_1 = ['9','2','3'] random_2 = ['1','5','6'] random_3 = ['8','7','0'] 因此,不会包含最后一个“ 4”,这很好。 如何从随机选择的列表中选择50? 更好的是,如何从原始列表中随机选择50个?
129 python  file  list  select  random 

12
初始化一个numpy数组
有没有办法初始化形状的numpy数组并将其添加到其中?我将通过列表示例来说明我需要的内容。如果要创建循环中生成的对象列表,可以执行以下操作: a = [] for i in range(5): a.append(i) 我想对一个numpy数组做类似的事情。我了解vstack,串联等。但是,这些似乎需要两个numpy数组作为输入。我需要的是: big_array # Initially empty. This is where I don't know what to specify for i in range(5): array i of shape = (2,4) created. add to big_array 本big_array应具有的形状(10,4)。这该怎么做? 编辑: 我想添加以下说明。我知道我可以定义big_array = numpy.zeros((10,4))然后填充它。但是,这需要预先指定big_array的大小。我知道这种情况下的大小,但是如果我不知道该怎么办?当我们使用该.append函数在python中扩展列表时,我们不需要事先知道其最终大小。我想知道是否存在从空数组开始的从较小数组创建较大数组的类似方法。
129 python  arrays  numpy 

9
Python提取模式匹配
Python 2.7.1我正在尝试使用python正则表达式来提取模式内的单词 我有一些看起来像这样的字符串 someline abc someother line name my_user_name is valid some more lines 我要提取单词“ my_user_name”。我做类似的事情 import re s = #that big string p = re.compile("name .* is valid", re.flags) p.match(s) #this gives me <_sre.SRE_Match object at 0x026B6838> 如何立即提取my_user_name?
129 python  regex 

5
Anaconda导出环境文件
如何制作可以在其他计算机上使用的anaconda环境文件? 我使用将Anaconda python环境导出到YML conda env export > environment.yml。导出的environment.yml内容包含此行prefix: /home/superdev/miniconda3/envs/juicyenv,它映射到我的anaconda的位置,这在其他计算机上将有所不同。


2
检查变量是否为数据框
当我的函数f用一个变量调用时,我想检查var是否是一个熊猫数据框: def f(var): if var == pd.DataFrame(): print "do stuff" 我想解决方案可能很简单,但即使 def f(var): if var.values != None: print "do stuff" 我无法使其按预期方式工作。
129 python  pandas 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.