def is_list(p):
return isinstance(p, list)
def deep_reverse(list):
o=[]
for i in reversed(list):
if is_list(i)==True:
print i
deep_reverse(i)
o.append(i)
return o
Run Code Online (Sandbox Code Playgroud)
例如:
p = [1, [2, 3, [4, [5, 6]]]]
print deep_reverse(p)
#>>> [[[[6, 5], 4], 3, 2], 1]
Run Code Online (Sandbox Code Playgroud) 可以说我有两个列表:
x = [1,2,3,4]
y = [1,4,7,8]
Run Code Online (Sandbox Code Playgroud)
我想在x中附加x中尚未包含的任何值.我可以通过循环轻松完成此操作:
for value in y:
if value not in x:
x.append(value)
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更多的Pythonic方式.
让我们说我想打印这个:text \" text2.
我能得到的最好的输出是:text " text2.
有没有办法将上述文本完全按原样放入字符串中?
非常简单的问题.对不起,我是新人.我有以下代码:
list = ['edward', 'jason', 'john']
for i in list:
print list
Run Code Online (Sandbox Code Playgroud)
它产生:
john
Run Code Online (Sandbox Code Playgroud)
为什么我只得到姓氏?我想要所有这些.即.
edward
jason
john
Run Code Online (Sandbox Code Playgroud) 如果事情属实,我如何结束我的计划?
这是我的代码
count=0
num=input("What would you like to do [1,2,3,4]? ")
while (num>'0' and num<'5'):
while num=='1':
Do something
while num=='2':
Do something
While num=='3':
Do something
while num=='4' and count!=1:
print("The End")
count= count+1
Run Code Online (Sandbox Code Playgroud)
我希望程序在num为'4'时结束
我想从列表A中删除列表B中的列表项.这是我写的函数:
def remove(A,B):
to_remove=[];
for i in range(len(A)):
for j in range(len(B)):
if (B[j]==A[i]):
to_remove.append(i);
for j in range(len(to_remove)):
A.pop(to_remove[j]);
Run Code Online (Sandbox Code Playgroud)
这是正常的做法吗?虽然,这完全正常(如果错别字,我不知道),我认为可能有更多的pythonic方式来做到这一点.请建议.
Bonjour Stack0verflow
我试图让这段代码将数据写入stored_output没有第1行(标题行)
with open(filenamex, 'rb') as currentx:
current_data = currentx.read()
## because of my filesize I dont want to go through each line the the route shown below to remove the first line (title row)
for counter, line in enumerate(current_data):
if counter != 0:
data.writeline(line)
#stored_output.writelines(current_data)
Run Code Online (Sandbox Code Playgroud)
因为文件大小我不想做for循环(效率)
任何建设性的评论或代码片段将不胜感激.
谢谢AEA
我尝试在python中启动一个基本程序,它是:
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
hello = "world"
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
File "toto.py", line 4
hello = "world"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
但我确定这是好语法!那我在哪里错了?
谢谢.
我对python有点疑问.
我有一段代码
screen.blit(badninja1,(badninja1px ,badninja1py))
screen.blit(badninja2,(badninja2px ,badninja2py))
screen.blit(badninja3,(badninja3px ,badninja3py))
Run Code Online (Sandbox Code Playgroud)
我知道它重复了3次,我想做一些重构.
这是一段python代码,它的作用是在badninja1px和badninja1py等地方绘制badninja1 .bandninja1,badninja1px,badninja1py和其他是变量名.
我想把这段代码放在for循环中
for x in range(1, 4):
#add number 1, 2, 3 to the variable name.
Run Code Online (Sandbox Code Playgroud)
我试着这样做,并意识到我可以在python中轻松组合字符串:
"Dec "+str(25)+"th".
Run Code Online (Sandbox Code Playgroud)
但是,更改变量名称有点棘手.
任何人都可以给你一个提示谢谢!!
我刚刚开始学习python,并且正在使用slice(:)运算符和字符串。由于某些原因,如果我指定了无效的索引,然后是切片运算符,则不会给出“超出范围”错误。
>>> s='Hello'
>>> print s[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> print s[5:]
//Prints just a newline, no error
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下为什么我在第二种情况下没有出错。
python ×10
list ×3
string ×3
python-2.7 ×2
exception ×1
file ×1
find ×1
iteration ×1
python-3.x ×1
stringio ×1
syntax ×1
syntax-error ×1
variables ×1