Questions tagged «python»

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

13
openpyxl-调整列宽大小
我有以下脚本将CSV文件转换为XLSX文件,但是我的列大小非常狭窄。每次我必须用鼠标拖动它们以读取数据时。有人知道如何设置列宽openpyxl吗? 这是我正在使用的代码。 #!/usr/bin/python2.6 import csv from openpyxl import Workbook from openpyxl.cell import get_column_letter f = open('users_info_cvs.txt', "rU") csv.register_dialect('colons', delimiter=':') reader = csv.reader(f, dialect='colons') wb = Workbook() dest_filename = r"account_info.xlsx" ws = wb.worksheets[0] ws.title = "Users Account Information" for row_index, row in enumerate(reader): for column_index, cell in enumerate(row): column_letter = get_column_letter((column_index …
81 python  openpyxl 



5
如何从python程序使用youtube-dl?
我想访问以下shell命令的结果, youtube-dl -g "www.youtube.com/..." direct url从python程序中将其输出打印到文件中。这是我尝试过的: import youtube-dl fromurl="www.youtube.com/..." geturl=youtube-dl.magiclyextracturlfromurl(fromurl) 那可能吗?我试图了解源的机制,但迷路了:youtube_dl/__init__.py,youtube_dl/youtube_DL.py,info_extractors...



2
Python映射对象不可下标
为什么以下脚本会给出错误: payIntList[i] = payIntList[i] + 1000 TypeError: 'map' object is not subscriptable payList = [] numElements = 0 while True: payValue = raw_input("Enter the pay amount: ") numElements = numElements + 1 payList.append(payValue) choice = raw_input("Do you wish to continue(y/n)?") if choice == 'n' or choice == 'N': break payIntList …

2
Python Scipy中的两样本Kolmogorov-Smirnov测试
我不知道如何在Scipy中进行两样本KS测试。 阅读文档scipy kstest之后 我可以看到如何测试分布与标准正态分布相同的地方 from scipy.stats import kstest import numpy as np x = np.random.normal(0,1,1000) test_stat = kstest(x, 'norm') #>>> test_stat #(0.021080234718821145, 0.76584491300591395) 这意味着在0.76的p值下,我们不能拒绝两个分布相同的零假设。 但是,我想比较两个分布,看看是否可以拒绝它们相同的零假设,例如: from scipy.stats import kstest import numpy as np x = np.random.normal(0,1,1000) z = np.random.normal(1.1,0.9, 1000) 并测试x和z是否相同 我尝试过天真: test_stat = kstest(x, z) 并得到以下错误: TypeError: 'numpy.ndarray' object …


3
joblib和pickle有哪些不同的用例?
背景:我刚刚开始使用scikit-learn,并在页面底部阅读了关于joblib和pickle的信息。 使用joblib替换pickle(joblib.dump和joblib.load)可能会更有趣,这在大数据上效率更高,但只能在磁盘而不是字符串中进行酸洗。 我阅读了有关Pickle的问答,Python的Pickle 常见用例,想知道这里的社区是否可以共享joblib和pickle之间的差异?一个人何时应该使用另一个?


7
检查列表中的所有值是否都大于某个数字
my_list1 = [30,34,56] my_list2 = [29,500,43] 如何检查列表中的所有值是否均> = 30?my_list1应该工作,my_list2而不应该。 我唯一想到的是: boolean = 0 def func(ls): for k in ls: if k >= 30: boolean = boolean + 1 else: boolean = 0 if boolean > 0: print 'Continue' elif boolean = 0: pass 2016年更新: 事后看来,在处理速度实际上很重要的更大数据集并利用numpy...之后,我会这样做: >>> my_list1 = [30,34,56] >>> …
81 python  list  function  max 

13
如何以所有可能的方式将列表分成几对
我有一个清单(为简单起见说6个元素) L = [0, 1, 2, 3, 4, 5] 我想以所有可能的方式将其分成几对。我显示一些配置: [(0, 1), (2, 3), (4, 5)] [(0, 1), (2, 4), (3, 5)] [(0, 1), (2, 5), (3, 4)] 等等。在这里(a, b) = (b, a),配对的顺序并不重要,即 [(0, 1), (2, 3), (4, 5)] = [(0, 1), (4, 5), (2, 3)] 这样的配置的总数是1*3*5*...*(N-1)哪里N是我的列表的长度。 如何使用Python编写一个生成器,为我提供任意生成器的所有可能配置N?
81 python 

9
用Flask解决跨源资源共享
对于以下ajax发布请求Flask(如何使用从flask中的ajax发布的数据?): $.ajax({ url: "http://127.0.0.1:5000/foo", type: "POST", contentType: "application/json", data: JSON.stringify({'inputVar': 1}), success: function( data ) { alert( "success" + data ); } }); 我收到一个Cross Origin Resource Sharing (CORS)错误: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code …

3
如何在pytest下测试单个文件
您如何在pytest中测试单个文件?我只能在文档中找到忽略选项,而没有“仅测试此文件”选项。 最好是在命令行而不是上运行setup.cfg,因为我想在ide中运行不同的文件测试。整个套件花费的时间太长。
81 python  pytest 

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.