Questions tagged «python»

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


2
带有Return语句的奇怪的Try-Except-Else-Finally行为
这是一些行为特殊的代码。这是我编写的行为的简化版本。这仍然会显示出奇怪的行为,我对为什么会发生这种情况有一些具体的疑问。 我在Windows 7上使用Python 2.6.6。 def demo1(): try: raise RuntimeError,"To Force Issue" except: return 1 else: return 2 finally: return 3 def demo2(): try: try: raise RuntimeError,"To Force Issue" except: return 1 else: return 2 finally: return 3 except: print 4 else: print 5 finally: print 6 结果: >>> print demo1() …

8
开始索引以迭代Python列表
在Python中迭代列表时,设置起始索引的最佳方法是什么。例如,我有一个星期几的列表-星期日,星期一,星期二,...星期六-但我想从星期一开始遍历该清单。最佳做法是什么?
88 python  iteration 

6
一次以相同顺序随机播放两个列表
我正在使用包含大量文档的nltk图书馆movie_reviews语料库。我的任务是在没有数据预处理的情况下获得这些评论的预测性能。但是有一个问题,在列表中documents,documents2我有相同的文档,因此我需要对它们进行混洗,以便在两个列表中保持相同的顺序。我无法分别对它们进行洗牌,因为每次我对列表进行洗牌时,都会得到其他结果。这就是为什么我需要用相同的顺序立即将其洗牌,因为最后需要比较它们(取决于顺序)。我正在使用python 2.7 示例(实际上是字符串标记化的字符串,但不是相对的): documents = [(['plot : two teen couples go to a church party , '], 'neg'), (['drink and then drive . '], 'pos'), (['they get into an accident . '], 'neg'), (['one of the guys dies'], 'neg')] documents2 = [(['plot two teen couples church party'], 'neg'), (['drink then …
88 python  list  sorting  shuffle 

10
如何“精巧”地在Python中打印列表
在PHP中,我可以这样做: echo '<pre>' print_r($array); echo '</pre>' 在Python中,我目前只是这样做: print the_list 但是,这将导致大量数据。有什么方法可以将其很好地打印到可读的树中吗?(带有缩进)?
88 python 

7
在Python中读取Excel文件
我有一个Excel文件 Arm_id DSPName DSPCode HubCode PinCode PPTL 1 JaVAS 01 AGR 282001 1,2 2 JaVAS 01 AGR 282002 3,4 3 JaVAS 01 AGR 282003 5,6 我想在表格中保存一个字符串Arm_id,DSPCode,Pincode。此格式是可配置的,即可能更改为DSPCode,Arm_id,Pincode。我将其保存在以下列表中: FORMAT = ['Arm_id', 'DSPName', 'Pincode'] 鉴于FORMAT可以配置,我如何读取具有提供名称的特定列的内容? 这就是我尝试过的。目前,我能够读取文件中的所有内容 from xlrd import open_workbook wb = open_workbook('sample.xls') for s in wb.sheets(): #print 'Sheet:',s.name values = [] for …
88 python  excel  xlrd 

6
如何获取Tensorflow张量尺寸(形状)作为int值?
假设我有一个Tensorflow张量。如何获取张量的尺寸(形状)作为整数值?我知道有两种方法,tensor.get_shape()和tf.shape(tensor),但是我不能将形状值作为整int32数值。 例如,下面我创建了一个二维张量,我需要获取行数和列数,int32以便可以调用reshape()以创建shape张量(num_rows * num_cols, 1)。但是,该方法tensor.get_shape()返回值作为Dimension类型,而不是类型int32。 import tensorflow as tf import numpy as np sess = tf.Session() tensor = tf.convert_to_tensor(np.array([[1001,1002,1003],[3,4,5]]), dtype=tf.float32) sess.run(tensor) # array([[ 1001., 1002., 1003.], # [ 3., 4., 5.]], dtype=float32) tensor_shape = tensor.get_shape() tensor_shape # TensorShape([Dimension(2), Dimension(3)]) print tensor_shape # (2, 3) num_rows = tensor_shape[0] # ??? num_cols …


3
带有Nginx的WSGI vs uWSGi [关闭]
从目前的情况来看,这个问题不适合我们的问答形式。我们希望答案能得到事实,参考或专业知识的支持,但是这个问题可能会引起辩论,争论,民意调查或扩展讨论。如果您认为此问题可以解决并且可以重新提出,请访问帮助中心以获取指导。 8年前关闭。 在Nginx上使用WSGI VS uWSGI时,谁能解释优点/缺点。 目前,我正在为Django网站构建生产服务器,该服务器已经准备好了,但无法决定应该使用WSGI还是uWSGI。您能否详细说明每种配置的不同之处?哪种配置最合适? 提前致谢
88 python  django  nginx  wsgi  uwsgi 

13
如何创建可以使用或不使用参数的Python装饰器?
我想创建一个可以与参数一起使用的Python装饰器: @redirect_output("somewhere.log") def foo(): .... 或不使用它们(例如,默认情况下将输出重定向到stderr): @redirect_output def foo(): .... 那有可能吗? 请注意,我并不是在寻找重定向输出问题的其他解决方案,它只是我想要实现的语法的一个示例。
88 python  decorator 

1
pythonic是什么意思?[关闭]
已关闭。这个问题是基于观点的。它当前不接受答案。 想改善这个问题吗?更新问题,以便通过编辑此帖子以事实和引用的形式回答。 6年前关闭。 改善这个问题 在许多网站上,我经常看到这样的评论:代码不是pythonic,或者有更多的pythonic方法可以实现相同的目标。 在这种情况下,pythonic是什么意思?例如,为什么 while i < someValue: do_something(list[i]) i += 1 不是pythonic for x in list: doSomething(x) 是pythonic吗?
88 python 


3
什么时候应该使用@classmethod?什么时候应该使用def method(self)?
在集成我以前从未使用过的Django应用程序时,我发现了用于定义类中函数的两种不同方式。作者似乎非常有意地使用了它们。第一个是我自己经常使用的: class Dummy(object): def some_function(self,*args,**kwargs): do something here self is the class instance 另一个是我不使用的,主要是因为我不知道何时使用它,以及什么用途: class Dummy(object): @classmethod def some_function(cls,*args,**kwargs): do something here cls refers to what? 在Python文档中,classmethod装饰器的解释如下: 类方法将类作为隐式第一个参数接收,就像实例方法接收实例一样。 所以我想cls指的是Dummy自己(而class不是实例)。我不完全理解为什么会这样,因为我总是可以这样做: type(self).do_something_with_the_class 这仅仅是为了清楚起见,还是我错过了最重要的部分:没有它就无法完成的怪异而令人着迷的事情?

7
为什么random.shuffle返回None?
为什么要用Pythonrandom.shuffle返回None? >>> x = ['foo','bar','black','sheep'] >>> from random import shuffle >>> print shuffle(x) None 我如何获得改组后的值而不是None?
88 python  list  random  shuffle 


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.