字符串文字串联中的以下两个变体(带或不带加号):
join首选?码:
>>> # variant 1. Plus
>>> 'A'+'B'
'AB'
>>> # variant 2. Just a blank space
>>> 'A' 'B'
'AB'
>>> # They seems to be both equal
>>> 'A'+'B' == 'A' 'B'
True
Run Code Online (Sandbox Code Playgroud) 在我过去的一个问题中,一个回答者告诉我,当你想要创建的类是从头开始时,从对象继承是更好的,这不需要从其他类继承.
例如,像我一直做的那样:
class my_class:
"a class inherits from nothing"
def __init__(self):
pass
Run Code Online (Sandbox Code Playgroud)
对于他或她的建议:
class suggested_class(object):
"a class inherits from object type"
def __init__(self):
pass
Run Code Online (Sandbox Code Playgroud)
我对这两种方法的好处或缺点感到困惑.
问题1:
那么你的想法是什么,从对象类型继承还是什么都没有?
请参阅下面的代码.这段代码效果很好,但我想做两件事.有一点是我做了if语句或比实际更短的例如.我有很多像这样的列,而不是彼此相邻.我希望它更短.此外,有时我可能不知道确切的专栏信.
所以我想知道是否有办法知道列名或标题.就像最顶行的值一样.因此,如果它位于指定的列中,我可以测试以查看它是否始终在该单元格上执行函数.我找不到openpyxl函数来做列名.不确定它是否理解第一行与休息不同.我想也许如果不是我可以尝试在第一行做测试,但不明白如何做到这一点.
那么有没有办法调用列名?或者如果没有办法调用列名来测试,有人可以帮我做第一行检查,看看它是否有价值?然后改变正确的行我在?这有意义吗?
所以而不是代码说:
if cellObj.column == 'H' or ...
Run Code Online (Sandbox Code Playgroud)
它会说:
if cellObj.column_header == 'NameOfField or ...
Run Code Online (Sandbox Code Playgroud)
或者,如果不可能这样做,那么:
if this cell has column where first row value is 'NameOfField' ...
Run Code Online (Sandbox Code Playgroud)
请帮助我们做到这一点的最好方法.我查看了stackoverflow以及书籍和博客网站,但似乎并不是一种调用列名的方法(不是列的字母).
for row in sheet.iter_rows():
for cellObj in row:
if cellObj.column == 'H' or cellObj.column == 'I' or cellObj.column == 'L' or cellObj.column == 'M':
print(cellObj.value),
if cellObj.value.upper() == 'OldValue1':
cellObj.value = 1
print(cellObj.value)
elif cellObj.value.upper() == 'OldValue2':
cellObj.value = 2
print(cellObj.value)
Run Code Online (Sandbox Code Playgroud) 我正在学习Python并使用Python获得expandtabs命令.这是文档中的官方定义:
Run Code Online (Sandbox Code Playgroud)string.expandtabs(s[, tabsize])展开字符串中的选项卡,将其替换为一个或多个空格,具体取决于当前列和给定的选项卡大小.在字符串中出现每个换行符后,列号将重置为零.这不了解其他非打印字符或转义序列.选项卡大小默认为8.
所以我从中理解的是,制表符的默认大小是8,为了增加它,我们可以使用其他值
所以,当我在shell中尝试它时,我尝试了以下输入 -
>>> str = "this is\tstring"
>>> print str.expandtabs(0)
this isstring
>>> print str.expandtabs(1)
this is string
>>> print str.expandtabs(2)
this is string
>>> print str.expandtabs(3)
this is string
>>> print str.expandtabs(4)
this is string
>>> print str.expandtabs(5)
this is string
>>> print str.expandtabs(6)
this is string
>>> print str.expandtabs(7)
this is string
>>> print str.expandtabs(8)
this is string
>>> print str.expandtabs(9)
this is string
>>> print str.expandtabs(10)
this is string
>>> …Run Code Online (Sandbox Code Playgroud) 我是这里的新成员,我会直接进入这个,因为我整个星期天都试图绕过它.
我是Python的新手,以前在C++上学习了基础中级编码(这是一个为期10周的大学模块).
我正在尝试一些迭代技术来计算Pi,但两者都有些不准确,我不知道为什么.
我在大学教的第一种方法 - 我相信你们中的一些人之前已经看过它.
x=0.0
y=0.0
incircle = 0.0
outcircle = 0.0
pi = 0.0
i = 0
while (i<100000):
x = random.uniform(-1,1)
y = random.uniform(-1,1)
if (x*x+y*y<=1):
incircle=incircle+1
else:
outcircle=outcircle+1
i=i+1
pi = (incircle/outcircle)
print pi
Run Code Online (Sandbox Code Playgroud)
它本质上是两个轴上-1到+1平面上随机(x,y)坐标的生成器.然后,如果x ^ 2 + y ^ 2 <= 1,我们知道该点位于由坐标轴形成的框内的半径为1的圆内.
根据点的位置,计数器增加incircle或outcircle.
然后,pi的值是圆圈内外的值的比率.坐标是随机生成的,所以它应该是均匀的.
但是,即使在非常高的迭代值下,我对Pi的结果总是在3.65左右.
第二种方法是另一次迭代,它计算多边形的周长,边数增加,直到多边形几乎是一个圆,然后,Pi =圆周/直径.(我有点被骗,因为编码有一个math.cos(Pi)术语,所以看起来我正在使用Pi来找到Pi,但这只是因为你不能轻易地使用度来表示Python上的角度).但即使是高迭代,最终结果似乎也在3.20左右结束,这也是错误的.代码在这里:
S = 0.0
C = 0.0
L = 1.0
n = 2.0
k = 3.0
while (n<2000):
S = 2.0**k
L = L/(2.0*math.cos((math.pi)/(4.0*n))) …Run Code Online (Sandbox Code Playgroud) 文件夹结构:
<current dir>
main.py
packages <dir>
__init__.py
mod.py
Run Code Online (Sandbox Code Playgroud)
主要的py:
import packages
print packages.mod.hello()
Run Code Online (Sandbox Code Playgroud)
mod.py:
def hello():
return 'hello'
Run Code Online (Sandbox Code Playgroud)
__init__.py:
from packages import mod
Run Code Online (Sandbox Code Playgroud)
如果我跑main.py,我没有错误.但是,如果我编辑__init__.py到'from packages import *',我得到这个错误:AttributeError的:"模块"对象有没有属性"国防部"
我不是在问这个'print'命令如何工作.我可以使用其他'import'语法main.py使其工作.现在的问题是:我很好奇的是'from packages import mod'在__init__.py.如果我可以这样做,import mod当我替换import *,这意味着导入一切,为什么我会得到一个错误?
那么from packages import *内心真正意味着__init__.py什么呢?
有人可以帮忙吗?谢谢
我的系统中有Ananconda4.0.2.我尝试通过python控制台中的命令找出我的numpy配置:
import numpy as np
np.__config__.show()
Run Code Online (Sandbox Code Playgroud)
它返回以下输出
lapack_opt_info:
libraries = ['mkl_lapack95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
library_dirs = ['/home/<username>/anaconda2/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/home/<username>/anaconda2/include']
blas_opt_info:
libraries = ['mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
library_dirs = ['/home/<username>/anaconda2/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/home/<username>/anaconda2/include']
openblas_lapack_info:
NOT AVAILABLE
lapack_mkl_info:
libraries = ['mkl_lapack95_lp64', 'mkl_intel_lp64', 'mkl_intel_thread','mkl_core', 'iomp5', 'pthread']
library_dirs = ['/home/<username>/anaconda2/lib']
define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
include_dirs = ['/home/<username>/anaconda2/include']
blas_mkl_info:
libraries = ['mkl_intel_lp64', 'mkl_intel_thread', 'mkl_core', 'iomp5', 'pthread']
library_dirs …Run Code Online (Sandbox Code Playgroud) 当我继续使用conda install PIL进行安装时,这就是问题它给了我:
UnsatisfiableError: The following specifications were found to be in conflict:
- pil -> python 2.6*
- python 3.6*
Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法找到数据框中最高值的位置(列和行索引).所以,例如,如果我的数据框看起来像这样:
A B C D E
0 100 9 1 12 6
1 80 10 67 15 91
2 20 67 1 56 23
3 12 51 5 10 58
4 73 28 72 25 1
Run Code Online (Sandbox Code Playgroud)
如何获得如下结果:[0, 'A']使用Pandas?
我按照Python Central上的简易指南为我的代码创建一个包:
https://www.pythoncentral.io/how-to-create-a-python-package/
所以我的目录结构是:
main.py
pack1/
__init__.py
Class1.py
Run Code Online (Sandbox Code Playgroud)
在main.py我导入和使用的文件Class1中:
from pack1 import Class1
var1 = Class1()
Run Code Online (Sandbox Code Playgroud)
在__init__.py我写的文件中:
import Class1 from Class1
Run Code Online (Sandbox Code Playgroud)
我完全按照指南,仍然得到错误:
ModuleNotFoundError: No module named 'Class1' (in __init__.py)
Run Code Online (Sandbox Code Playgroud) python ×10
python-2.7 ×3
packages ×2
python-3.x ×2
anaconda ×1
dataframe ×1
excel ×1
import ×1
inheritance ×1
montecarlo ×1
numeric ×1
numpy ×1
object ×1
openpyxl ×1
pandas ×1
pi ×1
pillow ×1
string ×1