我查看了标准库和StackOverflow,但没有找到类似的问题.那么,有没有办法在不滚动自己的功能的情况下执行以下操作?如果有人在没有内置方式的情况下写出漂亮的功能,则可以获得奖励.
def stringPercentToFloat(stringPercent)
# ???
return floatPercent
p1 = "99%"
p2 = "99.5%"
print stringPercentToFloat(p1)
print stringPercentToFloat(p2)
>>>> 0.99
>>>> 0.995
Run Code Online (Sandbox Code Playgroud) 有人可以用正则表达式帮我一点吗?我目前有这个:re.split(" +", line.rstrip()),用空格分隔.
我怎么能扩展它以涵盖标点符号呢?
我正在生成一些数字(比方说,num)并将数字写入输出文件outf.write(num).
但是编译器抛出一个错误:
"outf.write(num)
TypeError: argument 1 must be string or read-only character buffer, not int".
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我不知道为什么这不起作用请帮忙
import random
x = 0
z = input('?')
int(z)
def main():
while x < z:
n1 = random.randrange(1,3)
n2 = random.randrange(1,3)
t1 = n1+n2
print('{0}+{1}={2}'.format(n1,n2,t1)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它输出此错误
File "/Users/macbook/Documents/workspace/gamlir_filar/samlagning.py", line 12
^
SyntaxError: unexpected EOF while parsing
Run Code Online (Sandbox Code Playgroud)
我正在使用eclipse和python 3.3,我不知道为什么会这样.它有时输出这样的错误.
我正在写一系列文本菜单.下面的类和子类运行没有问题.但我正在审查我的编码,我想知道......我没有def __init__(self)在课堂上使用它是否可以?我应该将数据成员放在def __init__(Self):self.images =(),self.options =()中吗?如果我这样做那么我就不能使用abc模块进行约束,对吗?
class BaseMenu(object):
__metaclass__ = abc.ABCMeta
@abc.abstractproperty
def options(self):
pass
@abc.abstractproperty
def menu_name(self):
pass
def display(self):
header = "FooBar YO"
term = getTerminalSize()
#sys.stdout.write("\x1b[2J\x1b[H")
print header.center(term, '*')
print self.menu_name.center(term, '+')
print "Please choose which option:"
for i in self.options:
print(
str(self.options.index(i)+1) + ") "
+ i.__name__
)
value = int(raw_input("Please Choose: ")) - 1
self.options[value](self)
class Servers(BaseMenu):
menu_name = "Servers"
images = ()
foo = ()
def get_images(self):
if not self.images:
self.images …Run Code Online (Sandbox Code Playgroud) 请考虑以下示例:
i=7
j=8
k=10
def test():
i=1
j=2
k=3
return dict((name,eval(name)) for name in ['i','j','k'])
Run Code Online (Sandbox Code Playgroud)
它返回:
>>> test()
{'i': 7, 'k': 10, 'j': 8}
Run Code Online (Sandbox Code Playgroud)
为什么eval不考虑函数内定义的变量?从文档中,您可以选择传递全局变量和本地字典.这意味着什么?最后,我如何修改这个小案例才能使它工作?
python中的扩展切片语法已经向我解释为" a[n:m:k] returns every kth element from n to m".
这让我很清楚当k为正时会发生什么.但是我对如何解释a[n:m:k]负面k 感到迷茫.我知道这会a[::-1]颠倒一个,并且这a[::-k]需要反转a的第k个元素.
但这是对k正定义的概括呢?我想知道a[n:m:k]实际上是如何定义的,所以(例如)我可以理解为什么:
"abcd"[-1:0:-1] = "dcb"
Run Code Online (Sandbox Code Playgroud)
是否正在a[n:m:-k]逆转序列a,然后从m开始,在m之前从一个原始索引开始,然后结束一个?我不这么认为,因为这种模式不适合我尝试过的其他n和m值.但我无法弄清楚这是如何实际定义的,搜索让我无处可去.
Truel=""
count = 0
finle_touch=False #true after it find the first 3 upperletter
# check if there is 1 lower letter after three upper letter
def one_lower(i):
count=0
if i == i.lower:
finle_touch=True
Truel=i
# check for 3 upper letter
def three_upper(s):
for i in s:
if count == 3:
if finle_touch==True:
break
else:
one_lower(i)
elif i == i.upper:
count +=1
print(count) #for debug
else:
count ==0
finle_touch=False
stuff="dsfsfFSfsssfSFSFFSsfssSSsSSSS......."
three_upper(stuff)
print(Truel)
Run Code Online (Sandbox Code Playgroud)
所以我在'stuff'上有很多字符串,我喜欢找到1个用大写字母表示的小写字母.
但当我运行此代码时,我得到:
Traceback (most recent call last):
File "C:\Python33\mypy\code.py", …Run Code Online (Sandbox Code Playgroud) 拆包/图示运营商*和**在横跨Python版本(2.7,3.X <3.5和3.x> = 3.5)其适用性广泛不同.
例如:
| 2.7 | 3.1-3.4 | 3.5
----------------------------------------------------------------------
function(*args) ? ? ?
x, *y, z = [1, 2, 3, 4, 5] x ? ?
{**x, **y} x x ?
Run Code Online (Sandbox Code Playgroud)
我错过了各种版本之间是否还有其他差异?我正在浏览PEP和Readmes,但文档并未详细说明.
python function parameter-passing argument-unpacking iterable-unpacking
我们有一大堆例如字符串:c1309,IF1306,v1309,p1209,a1309,mo1309.
在Python中,删除数字的最佳方法是什么?所有我需要的是:c,IF,v,p,a,mo从上面的例子.
python ×10
string ×3
function ×2
python-3.x ×2
regex ×2
class ×1
eclipse ×1
eval ×1
list ×1
punctuation ×1
pydev ×1
python-2.7 ×1
python-3.3 ×1
scope ×1
slice ×1
split ×1
variables ×1