Questions tagged «python»

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


14
如何检查列表中是否包含以下项目之一?
我正在尝试寻找一种简短的方法来查看列表中是否包含以下任何项,但是我的第一次尝试不起作用。除了编写函数来完成此任务外,它还是检查多个项目之一是否在列表中的任何捷径。 >>> a = [2,3,4] >>> print (1 or 2) in a False >>> print (2 or 1) in a True
220 python 

10
使用Flask for Python获取访问者的IP地址
我正在建立一个网站,用户可以使用使用Python(在我的情况下为2.6)的Flask微框架(基于Werkzeug)登录和下载文件。 我需要获得用户登录时的IP地址(出于记录目的)。有谁知道如何做到这一点?当然有办法用Python做到吗?

7
Python和IPython有什么区别?
Python和IPython之间到底有什么区别? 如果我用Python编写代码,它是否可以按原样在IPython中运行还是需要进行修改? 我知道IPython应该是Python的交互式外壳,但仅此而已?还是有一种叫做IPython的语言?如果我在IPython下编写某些内容,它将在Python中运行,反之亦然吗?如果存在差异,我怎么知道它们是什么?Python使用的所有软件包都可以像在IPython中那样工作吗?
220 python  ipython 



11
从熊猫日期时间列中分别提取月份和年份
我有一个数据框df,其中包含以下列: df['ArrivalDate'] = ... 936 2012-12-31 938 2012-12-29 965 2012-12-31 966 2012-12-31 967 2012-12-31 968 2012-12-31 969 2012-12-31 970 2012-12-29 971 2012-12-31 972 2012-12-29 973 2012-12-29 ... 该列的元素是pandas.tslib.Timestamp。 我只想包括年份和月份。我以为会有一种简单的方法,但是我无法弄清楚。 这是我尝试过的: df['ArrivalDate'].resample('M', how = 'mean') 我收到以下错误: Only valid with DatetimeIndex or PeriodIndex 然后我尝试了: df['ArrivalDate'].apply(lambda(x):x[:-2]) 我收到以下错误: 'Timestamp' object has no attribute '__getitem__' …
220 python  pandas 


10
如何确定文本的编码?
我收到了一些经过编码的文本,但是我不知道使用了什么字符集。有没有一种方法可以使用Python确定文本文件的编码?如何检测 C#处理的文本文件的编码/代码页。

13
Django模板变量和Javascript
当我使用Django模板渲染器渲染页面时,可以传入包含各种值的字典变量,以使用来在页面中对其进行操作{{ myVar }}。 有没有办法在Javascript中访问相同的变量(也许使用DOM,我不知道Django如何使变量可访问)?我希望能够基于传入的变量中包含的值使用AJAX查找来查找详细信息。


12
嵌套列表上的列表理解?
我有这个嵌套列表: l = [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], ['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', '100'], ['100', '100', '100', '100']] 现在,我要做的是将列表中的每个元素转换为float。我的解决方案是这样的: newList = [] for x in l: for y in x: newList.append(float(y)) 但这可以使用嵌套列表理解来完成吗? 我所做的是: [float(y) …


2
builtins.TypeError:必须为str,而不是字节
我已经将脚本从Python 2.7转换为3.2,并且有一个错误。 # -*- coding: utf-8 -*- import time from datetime import date from lxml import etree from collections import OrderedDict # Create the root element page = etree.Element('results') # Make a new document tree doc = etree.ElementTree(page) # Add the subelements pageElement = etree.SubElement(page, 'Country',Tim = 'Now', name='Germany', AnotherParameter …
219 python  python-3.x  lxml 

13
input()错误-NameError:名称“ ...”未定义
尝试运行此简单脚本时出现错误: input_variable = input ("Enter your name: ") print ("your name is" + input_variable) 假设我输入“花花公子”,我得到的错误是: line 1, in <module> input_variable = input ("Enter your name: ") File "<string>", line 1, in <module> NameError: name 'dude' is not defined 我正在使用python 2.7运行这些脚本。

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.