Python:展平函数在控制台中有效,但在文件中无效?
我正在做一个练习来展平嵌套列表。该代码在控制台中工作,但在文件中时不起作用。我不知道发生了什么事。 :(
def flatten(nested):
"""
>>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
[2, 9, 2, 1, 13, 2, 8, 2, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
[9, 7, 1, 13, 2, 8, 7, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
[9, 7, 1, 13, 2, 8, 2, 6]
>>> flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
[5, 5, 1, 5, 5, 5, 5, 6]
"""
simple = []
for x in nested:
if type(x) == type([]):
for y in x:
simple.append(y)
else:
simple.append(x)
return simple
if __name__ == '__main__':
import doctest
doctest.testmod()
我首先尝试递归地解决这个练习,但决定首先尝试迭代。
编辑:在文件中执行时,它只打印出原始函数参数 TIA
I'm doing an exercise to flatten nested lists. The code works in console but it doesn't work when its in a file. I have no idea what's going on. :(
def flatten(nested):
"""
>>> flatten([2, 9, [2, 1, 13, 2], 8, [2, 6]])
[2, 9, 2, 1, 13, 2, 8, 2, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [7, 6]])
[9, 7, 1, 13, 2, 8, 7, 6]
>>> flatten([[9, [7, 1, 13, 2], 8], [2, 6]])
[9, 7, 1, 13, 2, 8, 2, 6]
>>> flatten([[5, [5, [1, 5], 5], 5], [5, 6]])
[5, 5, 1, 5, 5, 5, 5, 6]
"""
simple = []
for x in nested:
if type(x) == type([]):
for y in x:
simple.append(y)
else:
simple.append(x)
return simple
if __name__ == '__main__':
import doctest
doctest.testmod()
I first tried to solve this exercise recursively but decided to try it iterative first.
edit: When executed in the file it just prints out the original function argument
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是你的展平函数只能展平一层。 doctest 之所以打印出错误,是因为它确实出错了。它们不是您传入的内容。
您应该研究一种递归方法,而不是附加
y
——您也可以对y
调用 flatten。if type(x) != type([])
可以是您的基本情况。The problem is that your flatten function only flattens one level. The reason why doctest is printing out errors is because it is indeed erroring. They are not what you passed in.
You should investigate a recursive approach that instead of appending
y
--- you call flatten ony
as well.if type(x) != type([])
can be your base case.如果你想“作弊”,你可以这样做:
我有点好奇与“正常”展平操作相比,它的速度有多快......
编辑:
粗略的测试表明,作弊方法几乎需要恒定的时间,无论什么数据复杂性,而递归解决方案会随着时间的推移而增加。
较大
较小
这是我的代码(我对更大的数据集真的很好奇!):
If you want to "cheat" you could do this:
I'm kind of curious how fast that would be compared to the "normal" flattening operation...
Edit:
A cursory test shows that the cheating method takes nearly constant time regardless of data complexity, while a recursive solution increases in time.
Larger
Smaller
Here's my code (and I'm really curious about larger data sets!):
您的应用程序并不是什么都不做。
您需要保持扁平化直到完成,例如使用递归。简单,但对数据进行了一次额外的传递:如果
type(x) == type([])
,则返回flatten(simple)
而不是simple 位于函数末尾。
Your application is not doing nothing.
You need to keep flattening until it completes, e.g. using recursion. Simple but with one extra pass over your data: If
type(x) == type([])
, returnflatten(simple)
instead ofsimple
at the end of the function.我认为 @orangeoctopus 的答案是正确的,但没有抓住“为什么它在控制台中工作”的问题。我将做出一个猜测:
它在控制台中不起作用。我认为您使用恰好有效的输入子集进行了测试。例如,
作品!
但
没有那么多。
I think that @orangeoctopus's answer is correct, but doesn't capture the "why does it work in the console" question. I'm going to make a guess:
It doesn't work in the console. I think you tested with a subset of the input that happens to work. For example,
works!
But
does not so much.
我的答案与@DonaldMiner的非常相似(事实上我也是从这个开始的),但后来注意到如果列表中的字符串包含
"["
或"]",它会失败。
例如,在这里不起作用:
我编写了一个类似的函数(稍微复杂一点,并且仅删除
[
和]
如果它不在列表中的字符串中。你当然也可以使用
eval()
,但前提是你不从某个地方读取列表(它是由程序创建的),否则,有人只需输入命令,Python就会执行。它。My answer is pretty similiar to @DonaldMiner's (In fact I started out with that as well), but then noticed that if a string in the list contains
"["
or"]"
, it would fail.E.g. won't work here:
I wrote a similiar function (a little more complex, and only removes the
[
and]
if it is not in a string in the list.You could of course also use
eval()
, but only if you dont read in the list from somwhere (It is made by the program). Otherwise, someone can just enter a command, and python will execute it.