Questions tagged «python»

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

6
熊猫数据框/ numpy数组“轴”定义中的歧义
对于如何定义python轴以及它们是否引用DataFrame的行或列,我一直感到困惑。考虑下面的代码: >>> df = pd.DataFrame([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]], columns=["col1", "col2", "col3", "col4"]) >>> df col1 col2 col3 col4 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 因此,如果调用df.mean(axis=1),我们将在各行中获得均值: >>> df.mean(axis=1) 0 1 1 2 2 3 …

7
使用参数从bash调用Python脚本
我知道我可以使用以下命令从bash脚本运行python脚本: python python_script.py 但是,如果我想将变量/自变量从bash脚本传递给python脚本,该怎么办?我怎样才能做到这一点? 基本上,bash会计算出文件名,然后python将其上传,但是当我调用它时,我需要将文件名从bash发送到python。
91 python  linux  bash  shell  debian 


5
python中的函数重载了吗?
Python中是否可能有重载函数?在C#中,我会做类似的事情 void myfunction (int first, string second) { //some code } void myfunction (int first, string second , float third) { //some different code } 然后当我调用该函数时,它将根据参数的数量在两者之间进行区分。是否可以在Python中做类似的事情?


6
从多索引熊猫中选择
我有一个带有列“ A”和“ B”的多索引数据框。 有没有一种方法可以通过在多索引的一列上进行过滤而不选择将索引重置为单列索引来选择行? 例如。 # has multi-index (A,B) df #can I do this? I know this doesn't work because the index is multi-index so I need to specify a tuple df.ix[df.A ==1]

8
在给定均值和标准差的情况下,如何计算正态分布中的概率?
如何在Python中给定平均值std的正态分布中计算概率?我总是可以像这个问题中的OP一样根据定义明确地编写自己的函数:计算Python分布中随机变量的概率 只是想知道是否有一个库函数调用将允许您执行此操作。在我的想象中,它将是这样的: nd = NormalDistribution(mu=100, std=12) p = nd.prob(98) Perl中有一个类似的问题:如何在给定的Perl正态分布下计算点的概率?。但是我没有在Python中看到它。 Numpy有一个random.normal功能,但这就像采样,不完全是我想要的。

1
来自Pylint的Cell-var-from-loop警告
对于以下代码: for sort_key, order in query_data['sort']: results.sort(key=lambda k: get_from_dot_path(k, sort_key), reverse=(order == -1)) Pylint报告了一个错误: 循环中定义的单元变量sort_key(cell-var-from-loop) 有人可以暗示这里发生了什么吗?根据pylint源代码,描述为: 闭包中使用的变量在循环中定义。这将导致所有闭包对封闭变量使用相同的值。 但是我不知道这意味着什么。谁能举例说明这个问题?

3
如何在没有pip / easy_install的情况下手动安装pypi模块?
我想使用gntp模块显示C / C ++软件的类似烤面包机的通知。我想打包所有依赖项,以使软件可以在另一台计算机上自行执行。 gntp模块仅可通过pip安装程序使用,无法使用(运行软件的计算机没有任何互联网连接):如何从源代码安装它? 我不想强迫用户安装easy_install / pip并手动将pip路径添加到%PATH。 PS:我在Windows计算机上使用Python 2.7。

11
如何使用Python判断字符串是否以数字开头?
我有一个以数字(从0到9)开头的字符串,我知道我可以使用startswith()“或” 10个测试用例,但是可能有一个更整洁的解决方案 所以不要写 if (string.startswith('0') || string.startswith('2') || string.startswith('3') || string.startswith('4') || string.startswith('5') || string.startswith('6') || string.startswith('7') || string.startswith('8') || string.startswith('9')): #do something 有没有更聪明/更有效的方法?
91 python  string  digits 

2
列表显示Django中的多对多
class PurchaseOrder(models.Model): product = models.ManyToManyField('Product') vendor = models.ForeignKey('VendorProfile') dollar_amount = models.FloatField(verbose_name='Price') class Product(models.Model): products = models.CharField(max_length=256) def __unicode__(self): return self.products 我有那个代码。不幸的是,该错误来自admin.py,其中包含ManyToManyField class PurchaseOrderAdmin(admin.ModelAdmin): fields = ['product', 'dollar_amount'] list_display = ('product', 'vendor') 错误提示: “ PurchaseOrderAdmin.list_display [0]”,“产品”是不支持的ManyToManyField。 然而,当我把它编译'product'出来list_display。所以,我怎么能显示'product'在list_display没有给它的错误? 编辑:也许一个更好的问题是你如何显示ManyToManyField的list_display?

6
Python:追加项目以列出N次
这似乎是Python的捷径。我想将项目附加到列表N次,有效地做到这一点: l = [] x = 0 for i in range(100): l.append(x) 在我看来,应该为此采用一种“优化”的方法,例如: l.append_multiple(x, 100) 在那儿?
91 python  list 

5
如何检查是否已导入python模块?
如何检查我是否在代码中的某个位置导入了模块? if not has_imported("somemodule"): print('you have not imported somemodule') 我想检查是否已经导入模块的原因是因为我有一个我不想导入的模块,因为有时会弄乱我的程序。

2
使用Pandas读取制表符分隔的文件-在Windows上适用,但在Mac上不适用
我一直在Windows中使用Pandas / Python读取制表符分隔的数据文件,没有任何问题。数据文件的前三行包含注释,然后带有标题。 df = pd.read_csv(myfile,sep='\t',skiprows=(0,1,2),header=(0)) 我现在正在尝试使用Mac读取此文件。(我第一次在Mac上使用Python。)出现以下错误。 pandas.parser.CParserError: Error tokenizing data. C error: Expected 1 fields in line 8, saw 39 如果将read_csv的error_bad_lines参数设置为False,则会得到以下信息,该信息一直持续到最后一行的末尾。 Skipping line 8: expected 1 fields, saw 39 Skipping line 9: expected 1 fields, saw 125 Skipping line 10: expected 1 fields, saw 125 Skipping line 11: expected …

3
使用pip安装Matplotlib时出现内存错误
我正在使用Python 2.7,如果我尝试安装Matplotlib,如果我使用“ pip install matplotlib”,则会收到此错误 Exception: Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 232, in main status = self.run(options, args) File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 339, in run requirement_set.prepare_files(finder) File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 355, in prepare_files do_download, session=self.session, File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 782, in unpack_url session, File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 667, in unpack_http_url from_path, …

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.