Questions tagged «python»

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


9
在Python中创建一个空对象
是否有在Python中定义空对象的快捷方式,还是总是需要创建自定义空类的实例? 编辑:我的意思是一个空的对象可用于鸭打字。
77 python  oop 


12
获取经纬度给定的当前点,距离和方位
给定一个现有的经度/纬度点,距离(以KM为单位)和方位角(以度为单位转换成弧度),我想计算新的经度/纬度。这个站点一遍又一遍地出现,但我只是无法找到适合我的公式。 上述链接采用的公式为: lat2 = asin(sin(lat1)*cos(d/R) + cos(lat1)*sin(d/R)*cos(θ)) lon2 = lon1 + atan2(sin(θ)*sin(d/R)*cos(lat1), cos(d/R)−sin(lat1)*sin(lat2)) 上面的公式适用于MSExcel,其中- asin = arc sin() d = distance (in any unit) R = Radius of the earth (in the same unit as above) and hence d/r = is the angular distance (in radians) atan2(a,b) = arc tan(b/a) …

5
了解scikit CountVectorizer中的min_df和max_df
我有五个输入到CountVectorizer的文本文件。为CountVectorizer实例指定min_df和max_df时,最小/最大文档频率到底是什么意思?是某个单词在其特定文本文件中的出现频率,还是整个整体语料库(5个txt文件)中该单词的出现频率? 当min_df和max_df作为整数或浮点数提供时有何不同? 该文档似乎没有提供详尽的解释,也没有提供示例来演示min_df和/或max_df的用法。有人可以提供说明min_df或max_df的说明或示例。



4
Python:从给定日期开始和结束一周的数据
day = "13/Oct/2013" print("Parsing :",day) day, mon, yr= day.split("/") sday = yr+" "+day+" "+mon myday = time.strptime(sday, '%Y %d %b') Sstart = yr+" "+time.strftime("%U",myday )+" 0" Send = yr+" "+time.strftime("%U",myday )+" 6" startweek = time.strptime(Sstart, '%Y %U %w') endweek = time.strptime(Send, '%Y %U %w') print("Start of week:",time.strftime("%a, %d %b %Y",startweek)) …
77 python  date  time 


9
使用pysftp验证主机密钥
我正在使用pysftp编写程序,它想针对验证SSH主机密钥C:\Users\JohnCalvin\.ssh\known_hosts。 终端程序使用PuTTY将其保存到Registry中[HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys]。 如何调和pysftp和PuTTY之间的区别? 我的代码是: import pysftp as sftp def push_file_to_server(): s = sftp.Connection(host='138.99.99.129', username='root', password='*********') local_path = "testme.txt" remote_path = "/home/testme.txt" s.put(local_path, remote_path) s.close() push_file_to_server() 我收到的错误响应是: E:\ Program Files(x86)\ Anaconda3 \ lib \ site-packages \ pysftp__init __。py:61:UserWarning: 无法从C:\ Users \ JohnCalvin.ssh \ known_hosts加载HostKeys。 您将需要显式加载HostKeys(cnopts.hostkeys.load(filename))或disableHostKey检查(cnopts.hostkeys = None)。warnings.warn(wmsg,UserWarning)追溯(最近一次调用最近):push_file_to_server()中第14行的文件“ E:\ OneDrive \ Python …

7
Writelines写的行没有换行符,只填充文件
我有一个将列表写入文件的程序。该列表是由管道分隔的行的列表,这些行应按如下所示写入文件: 123|GSV|Weather_Mean|hello|joe|43.45 122|GEV|temp_Mean|hello|joe|23.45 124|GSI|Weather_Mean|hello|Mike|47.45 但是它写了他们这行啊: 123|GSV|Weather_Mean|hello|joe|43.45122|GEV|temp_Mean|hello|joe|23.45124|GSI|Weather_Mean|hello|Mike|47.45 该程序将所有行写成一行,而没有任何换行符。我认为写行应该在文件下写行,而不是将所有内容都写到一行。 fr = open(sys.argv[1], 'r') # source file fw = open(sys.argv[2]+"/masked_"+sys.argv[1], 'w') # Target Directory Location for line in fr: line = line.strip() if line == "": continue columns = line.strip().split('|') if columns[0].find("@") > 1: looking_for = columns[0] # this is what we need …
77 python 

7
OpenCV python:ValueError:太多值无法解包
我正在编写一个opencv程序,并且在另一个stackoverflow问题上找到了一个脚本:Computer Vision:遮盖人的手 运行脚本化答案时,出现以下错误: Traceback (most recent call last): File "skinimagecontour.py", line 13, in <module> contours, _ = cv2.findContours(skin_ycrcb, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ValueError: too many values to unpack 编码: import sys import numpy import cv2 im = cv2.imread('Photos/test.jpg') im_ycrcb = cv2.cvtColor(im, cv2.COLOR_BGR2YCR_CB) skin_ycrcb_mint = numpy.array((0, 133, 77)) skin_ycrcb_maxt = numpy.array((255, 173, 127)) …

13
Python:使用4个空格进行缩进。为什么?[关闭]
已关闭。这个问题是基于观点的。它当前不接受答案。 想改善这个问题吗?更新问题,以便通过编辑此帖子以事实和引用的形式回答。 6年前关闭。 改善这个问题 在对python进行编码时,我仅使用2个空格进行缩进,请确保PEP-8确实建议使用4个空格,但是从历史上看,这是不寻常的。 那么,谁能说服我使用4个空格而不是2个空格?有什么优缺点? PS最后,将所有现有代码库从2个空间转换为4个空间的简单方法是什么? PPS PEP-8也强烈建议不要使用制表符进行缩进。在这里阅读 因此,总结一下: 优点: 包裹长度超过80行的字符串时,有更多的空间可以布置。 可以从摘要中复制代码,并且可以正常工作。 缺点: 使用更深层的嵌套语句,您可以减少实际代码的空间。 谢谢。

6
Python unittest传递参数
在python中,我如何将命令行中的参数传递给unittest函数。这是到目前为止的代码……我知道这是错误的。 class TestingClass(unittest.TestCase): def testEmails(self): assertEqual(email_from_argument, "my_email@example.com") if __name__ == "__main__": unittest.main(argv=[sys.argv[1]]) email_from_argument = sys.argv[1]

3
Jinja2:在循环内更改变量的值
我想在循环内更改在循环外声明的变量的值。但是总是在变化,它将初始值保持在循环之外。 {% set foo = False %} {% for item in items %} {% set foo = True %} {% if foo %} Ok(1)! {% endif %} {% endfor %} {% if foo %} Ok(2)! {% endif %} 这将呈现: Ok(1)! 因此,到目前为止发现的唯一(坏)解决方案是: {% set foo = [] %} {% for item …
77 python  jinja2 

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.