我想知道是否有一条快捷方式可以在Python列表中列出一个简单的列表.
我可以在for循环中做到这一点,但也许有一些很酷的"单行"?我用reduce尝试了,但是我收到了一个错误.
码
l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
reduce(lambda x, y: x.extend(y), l)
Run Code Online (Sandbox Code Playgroud)
错误信息
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
AttributeError: 'NoneType' object has no attribute 'extend'
Run Code Online (Sandbox Code Playgroud) 我试图在Linux上删除python 2.7中的所有空格/制表符/换行符.
我写了这个,应该做的工作:
myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = myString.strip(' \n\t')
print myString
Run Code Online (Sandbox Code Playgroud)
输出:
I want to Remove all white spaces, new lines
and tabs
Run Code Online (Sandbox Code Playgroud)
这似乎是一件简单的事情,但我在这里缺少一些东西.我应该进口什么吗?
哪个是将元组列表转换为字符串的最pythonic方式?
我有:
[(1,2), (3,4)]
Run Code Online (Sandbox Code Playgroud)
而且我要:
"(1,2), (3,4)"
Run Code Online (Sandbox Code Playgroud)
我的解决方案是:
l=[(1,2),(3,4)]
s=""
for t in l:
s += "(%s,%s)," % t
s = s[:-1]
Run Code Online (Sandbox Code Playgroud)
是否有更多的pythonic方式来做到这一点?