Questions tagged «python»

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

8
有没有办法在Python中更改有效的进程名称?
我可以更改Python脚本的有效进程名称吗?当我获得系统进程列表时,我想显示一个不同的名称而不是进程的真实名称。在CI中可以设置 strcpy(argv[0],"othername"); 但是在Python中 argv[0] = "othername" 似乎不起作用。当我获得进程列表(ps ax在Linux框中带有)时,真实姓名不变。如果存在的话,我更喜欢一种便携式解决方案(或者针对posix的一种解决方案,以及针对Windows环境的另一种解决方案)。 提前致谢
78 python  process  arguments  hide  ps 

6
Python 3通过其值对字典进行排序
我发现的唯一方法适用于python2或仅返回元组列表。 是否可以按字典{"aa": 3, "bb": 4, "cc": 2, "dd": 1}的值对字典进行排序? 我要实现的字典排序顺序是从最大到最小。我希望结果看起来像这样: bb 4 aa 3 cc 2 dd 1 排序后,我想将其存储到文本文件中。



11
优雅的方法来检查字典中是否存在嵌套键?
是否有更可读的方法来检查是否存在嵌入字典的键而无需独立检查每个级别? 可以说我需要在埋藏的对象中获取此值(示例取自Wikidata): x = s['mainsnak']['datavalue']['value']['numeric-id'] 为了确保不会以运行时错误结束,有必要检查每个级别,如下所示: if 'mainsnak' in s and 'datavalue' in s['mainsnak'] and 'value' in s['mainsnak']['datavalue'] and 'nurmeric-id' in s['mainsnak']['datavalue']['value']: x = s['mainsnak']['datavalue']['value']['numeric-id'] 我可以想到的另一种解决方法是将其包装到一个try catch结构中,对于这样一个简单的任务,我也觉得很尴尬。 我正在寻找类似的东西: x = exists(s['mainsnak']['datavalue']['value']['numeric-id']) True如果所有级别都存在,则返回。


4
执行时如何打印Python文件的文档字符串?
我有一个带文档字符串的Python脚本。当命令行参数的解析不成功时,我想打印文档字符串以获取用户信息。 有什么办法吗? 最小的例子 #!/usr/bin/env python """ Usage: script.py This describes the script. """ import sys if len(sys.argv) < 2: print("<here comes the docstring>")
78 python  docstring 

7
如何表示未使用的函数参数?
当“解构”一个元组时,我可以_用来表示我不感兴趣的元组元素,例如 >>> a,_,_ = (1,2,3) >>> a 1 使用Python 2.x,如何使用函数参数表达相同的含义?我尝试使用下划线: >>> def f(a,_,_): return a ... File "<stdin>", line 1 SyntaxError: duplicate argument '_' in function definition 我还试图完全忽略该论点: >>> def f(a,,): return a File "<stdin>", line 1 def f(a,,): return a ^ SyntaxError: invalid syntax 还有另一种方法可以达到相同目的吗?

5
如何迭代Jinja模板中的词典列表?
我试过了: list1 = [{"username": "abhi", "pass": 2087}] return render_template("file_output.html", list1=list1) 在模板中: <table border=2> <tr> <td> Key </td> <td> Value </td> </tr> {% for dictionary in list1 %} {% for key in dictionary %} <tr> <td> <h3>{{ key }}</h3> </td> <td> <h3>{{ dictionary[key] }}</h3> </td> </tr> {% endfor %} {% endfor …

4
如何使用正则表达式查找重叠的匹配项?
>>> match = re.findall(r'\w\w', 'hello') >>> print match ['he', 'll'] 由于\ w \ w表示两个字符,因此应使用'he'和'll'。但是,为什么“ el”和“ lo”与正则表达式不匹配? >>> match1 = re.findall(r'el', 'hello') >>> print match1 ['el'] >>>

7
如何在鼻子中设置self.maxDiff以获取完整的差异输出?
在Python 3.3.0中使用鼻子1.2.1时,有时会收到类似于以下内容的错误消息 ====================================================================== FAIL: maxdiff2.test_equal ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/local/lib/python3.3/site-packages/nose/case.py", line 198, in runTest self.test(*self.arg) File "/Users/loic/cmrsj/Calculus_II/scrap/maxdiff2.py", line 32, in test_equal assert_equal(str1, str2) AssertionError: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus.\n [truncated]... != 'Suspendisse lectus leo, consectetur in tempor sit amet, placerat …
78 python  nose 




11
仅在满足条件的情况下添加到dict
我正在使用urllib.urlencode构建Web POST参数,但是,如果None它们不存在其他值,我只想添加一些值。 apple = 'green' orange = 'orange' params = urllib.urlencode({ 'apple': apple, 'orange': orange }) 效果很好,但是如果我将orange变量设置为可选,如何防止将其添加到参数中?这样的东西(伪代码): apple = 'green' orange = None params = urllib.urlencode({ 'apple': apple, if orange: 'orange': orange }) 我希望这已经足够清楚了,有人知道如何解决吗?

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.