我想产生这样的输出:
1
.列表项目2
.另一个清单项目关于清单项目1和2的评论段落.
3
.更多项目4
.最后的项目
我确信我已经看到了以这种方式中断和恢复列表的好方法(没有明确设置一些计数器),但我无法重现它.
鉴于输入
echo abc123def | grep -o '[0-9]*'
Run Code Online (Sandbox Code Playgroud)
在一台计算机上(使用GNU grep 2.5.4),这将返回123,而在另一台计算机上(使用GNU grep 2.5.1),它返回空字符串.有没有解释为什么grep 2.5.1在这里失败,或者它只是一个错误?我grep -o在这个方式中使用bash脚本,我希望能够在不同的计算机上运行(可能有不同版本的grep).是否有"正确的方法"来获得一致的行为?
我有一些构建数据结构的方法(比如说某些文件内容):
def loadfile(FILE):
return # some data structure created from the contents of FILE
Run Code Online (Sandbox Code Playgroud)
所以我可以做的事情
puppies = loadfile("puppies.csv") # wait for loadfile to work
kitties = loadfile("kitties.csv") # wait some more
print len(puppies)
print puppies[32]
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我浪费了大量时间实际阅读kitties.csv和创建一个我从未使用过的数据结构.我想在没有经常检查的情况下避免浪费if not kitties.我希望能够做到
puppies = lazyload("puppies.csv") # instant
kitties = lazyload("kitties.csv") # instant
print len(puppies) # wait for loadfile
print puppies[32]
Run Code Online (Sandbox Code Playgroud)
因此,如果我不尝试做任何事情kitties,loadfile("kitties.csv")永远不会被召唤.
有没有一些标准的方法来做到这一点?
在玩了一下后,我制作了以下解决方案,它似乎工作正常并且非常简短.还有其他选择吗?使用这种方法是否有缺点我应该记住?
class lazyload:
def __init__(self,FILE):
self.FILE = FILE
self.F = None
def __getattr__(self,name):
if …Run Code Online (Sandbox Code Playgroud)