python有没有办法使用tuple作为切片的slice?以下是无效的:
>>> a = range(20)
>>> b = (5, 12) # my slice indices
>>> a[b] # not valid
>>> a[slice(b)] # not valid
>>> a[b[0]:b[1]] # is an awkward syntax
[5, 6, 7, 8, 9, 10, 11]
>>> b1, b2 = b
>>> a[b1:b2] # looks a bit cleaner
[5, 6, 7, 8, 9, 10, 11]
喜欢这个:
people = []
begin
info = gets.chomp
people += [Person.new(info)] if not info.empty?
end while not info.empty?
该begin <code> end while <condition>由Ruby的作者Matz的拒绝。相反,他建议使用Kernel#loop,例如
loop do
# some code here
break if <condition>
end