Questions tagged «python»

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

5
operator.itemgetter()和sort()如何工作?
我有以下代码: # initialize a = [] # create the table (name, age, job) a.append(["Nick", 30, "Doctor"]) a.append(["John", 8, "Student"]) a.append(["Paul", 22, "Car Dealer"]) a.append(["Mark", 66, "Retired"]) # sort the table by age import operator a.sort(key=operator.itemgetter(1)) # print the table print(a) 它创建一个4x3的表格,然后按年龄对其进行排序。我的问题是,究竟是什么key=operator.itemgetter(1)?operator.itemgetter函数是否返回项目的值?为什么我不能只输入类似的内容key=a[x][1]?可以吗 用运算符如何打印像3x2is这样的表格的某个值22? Python到底如何对表格进行排序?我可以对它进行反向排序吗? 我如何基于两列(例如第一个年龄,然后如果年龄是相同的b名称)对其进行排序? 没有我怎么办operator?

10
显示其他时区的时间
是否有一种优雅的方式来显示其他时区的当前时间? 我希望本着以下一般精神: cur = <Get the current time, perhaps datetime.datetime.now()> print("Local time {}".format(cur)) print("Pacific time {}".format(<something like cur.tz('PST')>)) print("Israeli time {}".format(<something like cur.tz('IST')>))
75 python  time  timezone 

2
数据类型“ datetime64 [ns]”和“ <M8 [ns]”之间的区别?
我在熊猫中创建了一个TimeSeries: In [346]: from datetime import datetime In [347]: dates = [datetime(2011, 1, 2), datetime(2011, 1, 5), datetime(2011, 1, 7), .....: datetime(2011, 1, 8), datetime(2011, 1, 10), datetime(2011, 1, 12)] In [348]: ts = Series(np.random.randn(6), index=dates) In [349]: ts Out[349]: 2011-01-02 0.690002 2011-01-05 1.001543 2011-01-07 -0.503087 2011-01-08 -0.622274 2011-01-10 -0.921169 …

4
将浮点数舍入为x个小数?
有没有办法将python浮点数舍入为x小数?例如: &gt;&gt;&gt; x = roundfloat(66.66666666666, 4) 66.6667 &gt;&gt;&gt;x = roundfloat(1.29578293, 6) 1.295783 我已经找到了修剪/截断它们的方法(66.666666666-&gt; 66.6666),但是不是圆形的(66.666666666-&gt; 66.6667)。
75 python  math  rounding 

5
如何在没有终端提示的情况下从IPython会话进行复制
通常,我的工作流程涉及在IPython Shell中清理/整理数据。自IPython 5.0版以来,对终端接口进行了所有重大升级,这一点变得特别美妙。因此,假设我尝试整理一些非结构化数据: In [11]: for i, (num, header, txt) in enumerate(data): ...: header = [e.strip() for e in header.strip().split('\n')] ...: header[4] = header[4].strip(',').split(',') ...: data[i] = (num, header, txt) ...: 太棒了,它有效!但是现在,我真的很想将其添加到编辑器中的脚本中。如果从终端复制并粘贴,则会捕获左侧的所有垃圾。我可以在编辑器中轻松地清理它,但是如果我可以将代码直接从终端复制到剪贴板而不用触摸鼠标,也不需要抓住多余的东西,那就太好了。IPython中有这样的功能吗?

27
高品质,简单的随机密码生成器
我对创建一个非常简单的,高(加密)质量的随机密码生成器感兴趣。有一个更好的方法吗? import os, random, string length = 13 chars = string.ascii_letters + string.digits + '!@#$%^&amp;*()' random.seed = (os.urandom(1024)) print ''.join(random.choice(chars) for i in range(length))




6
Python strip()多个字符?
我想从字符串中删除任何括号。为什么这不能正常工作? &gt;&gt;&gt; name = "Barack (of Washington)" &gt;&gt;&gt; name = name.strip("(){}&lt;&gt;") &gt;&gt;&gt; print name Barack (of Washington
75 python 

7
ImportError:没有名为pandas的模块
我正在尝试在python中编写代码以获取Twitter数据,但twython并没有收到错误。但是我对熊猫错误。 我已经使用pip install pandas安装了pandas。但是我仍然会收到这个错误。请帮忙 F:\&gt;pip install pandas Collecting pandas c:\python27\lib\site-packages\pip\_vendor\requests\packages\urllib3\util\ssl_.py :90: InsecurePlatformWarning: A true SSLContext object is not available. This pr events urllib3 from configuring SSL appropriately and may cause certain SSL conn ections to fail. For more information, see https://urllib3.readthedocs.org/en/la test/security.html#insecureplatformwarning. InsecurePlatformWarning Using cached pandas-0.17.0-cp27-none-win32.whl Requirement already satisfied (use …
75 python  pandas 

7
如何使用TensorFlow获得稳定的结果,设置随机种子
我正在尝试使用不同的参数多次运行神经网络,以便校准网络参数(辍学概率,学习率ed)。但是我遇到的问题是,当我按如下所示循环运行网络时,在保持参数不变的情况下运行网络仍然为我提供了不同的解决方案: filename = create_results_file() for i in range(3): g = tf.Graph() with g.as_default(): accuracy_result, average_error = network.train_network( parameters, inputHeight, inputWidth, inputChannels, outputClasses) f, w = get_csv_writer(filename) w.writerow([accuracy_result, "did run %d" % i, average_error]) f.close() 在设置网络的层和错误功能之前,我在train_network函数的开头使用了以下代码: np.random.seed(1) tf.set_random_seed(1) 我还尝试在TensorFlow图形创建之前添加此代码,但是在结果输出中我会不断获得不同的解决方案。 我正在使用AdamOptimizer并使用初始化网络权重tf.truncated_normal。另外,我正在使用np.random.permutation随机播放每个时期的传入图像。

3
将函数应用于列表的每个元素
如何将函数应用于变量输入列表?例如,filter函数返回真值,但不返回函数的实际输出。 from string import upper mylis=['this is test', 'another test'] filter(upper, mylis) ['this is test', 'another test'] 预期的输出是: ['THIS IS TEST', 'ANOTHER TEST'] 我知道upper是内置的。这只是一个例子。
75 python  list  function 

2
Flake8:忽略整个文件的特定警告
目前,“忽略错误”文档列出了一种忽略特定行的特定错误的方法: example = lambda: 'example' # noqa: E731 ...以及忽略整个文件的所有错误的方法: # flake8: noqa from foo import unused function_that_doesnt_exist() x = 1+ 2 ...以及通过配置或命令行选项通过两种方式在整个项目中全局禁用特定错误的方法。 但是,如果我想忽略整个单个文件中的特定错误,例如,要禁用有关桶文件中未使用的导入的警告,该文件只会导入一堆类,以便其他包中的代码可以从中导入它们转?文档似乎并未暗示任何语法。可能吗?__init__.py
75 python  flake8 

3
您如何在Numpy中找到IQR?
是否有内置的Numpy / Scipy函数来查找四分位数范围?我自己可以很容易地做到这一点,但是mean()基本上存在sum/len... def IQR(dist): return np.percentile(dist, 75) - np.percentile(dist, 25)
75 python  numpy  scipy 

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.