有没有办法打印python中的所有字符,即使是那些通常不打印的字符?
例如
>>>print_all("skip
line")
skip\nline
Run Code Online (Sandbox Code Playgroud) 我有两个清单:
list_1 = [0, 1, 0, 0, 0, 0, 1]
list_2 = [34, 54, 23, 54, 656 34, 100]
Run Code Online (Sandbox Code Playgroud)
我想只将list_2索引所在list_2的值与实际相同索引的值1 相加list_1.
这里的例子是 54 + 100 = 154
我怎么能用Pythonic方式在Python中做到这一点?
我想做这样的事情:
def myFunc(y):
aVariable = "a"
bVariable = "b"
y(aVariable,bVariable)
def main():
myFunc(lambda a,b: a+=b)
Run Code Online (Sandbox Code Playgroud)
并期望输出"ab".
相反,我收到以下错误:
File "<stdin>", line 7
myFunc(lambda x, y: x += y)
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud) 在我的节目,我有25个不同的功能命名"task1","task2","task3",等...此刻我可以从一个单独的文件中对这些函数调用,并做他们的外壳:
import examplefilename
tasknum = str(input("Which task would you like to see? "))
task = "task" + tasknum
methodToCall = getattr(examplefilename, task)
result = methodToCall()
Run Code Online (Sandbox Code Playgroud)
尽管我所做的只能从另一个文件中调用,所以如何从同一文件中以这种方式执行功能呢?
我正在将用Python 2编写的源代码转换为Python 3,我偶然发现:
from Queue import Queue, Empty
Run Code Online (Sandbox Code Playgroud)
我改成了:
from multiprocessing import Queue, Empty
Run Code Online (Sandbox Code Playgroud)
但这给了我一个例外:
ImportError: cannot import name 'Empty'
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
我有一个函数有两个递归调用,我试图将其转换为迭代函数.我已经弄明白我可以通过一个电话很容易地做到这一点,但我无法弄清楚如何合并另一个电话.
功能:
def specialMultiplication(n):
if n < 2:
return 1
return n * specialMultiplication(n-1) * specialMultiplication(n-2)
Run Code Online (Sandbox Code Playgroud)
如果我只有其中一个,那将非常容易:
def specialMult(n, mult = 1):
while n > 1:
(n, mult) = (n-1, n * mult) # Or n-2 for the second one
return mult
Run Code Online (Sandbox Code Playgroud)
我只是无法弄清楚如何添加第二个调用以获得正确的答案.谢谢!
如果我有一个需要n从列表中返回随机元素的函数,我可能想检查列表中元素的数量是否大于我要求的样本,即:
mysample = []
def rnd(n):
if len(mysample) < n:
return False
return random.sample(mysample, n)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,False如果要求的元素数量大于我们的数量,我会返回.
这是好习惯吗?我们正在调用的地方rnd()是期望返回一个列表,而不是布尔值,所以对我来说感觉不对.
我在一个竞争激烈的编程网站上,发现一个人写了这个奇怪的(对我来说)Python 3代码:
[r,"Nothing"][r==""]
Run Code Online (Sandbox Code Playgroud)
它输出'Nothing',如果r是空字符串.
这怎么称呼它是什么意思?它看起来像一个三元运算符.
我最近刚刚发现有一种叫做函数注释的东西,但我不太确定如何使用它。这是我到目前为止:
def check_type(f):
def decorated(*args, **kwargs):
counter=0
for arg, type in zip(args, f.__annotations__.items()):
if not isinstance(arg, type[1]):
msg = 'Not the valid type'
raise ValueError(msg)
counter+=1
return f(*args, **kwargs)
return decorated
@check_type
def foo(a: int, b: list, c: str): #a must be int, b must be list, c must be str
print(a,b,c)
foo(12, [1,2], '12') #This works
foo(12, 12, 12) #This raises a value error just as I wanted to
foo(a=12, b=12, c=12) #But this works too:(
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在尝试检查 的类型a …
为什么第二次next不回来"bye"?
def salute():
yield "hello"
yield "bye"
def greet_me():
print(next(salute()))
print(next(salute()))
greet_me()
Run Code Online (Sandbox Code Playgroud)
输出:
你好
你好
python ×10
python-3.x ×8
lambda ×2
function ×1
generator ×1
list ×1
python-2.7 ×1
queue ×1
recursion ×1
string ×1
type-hinting ×1