The following code Ruby code will iterate the source string and produce a list of the cumulative words delimited by a ‘.’ character, other than those after the last ‘.’.
For example, give a source string of ‘Company.Dept.Group.Team’ the result will be …
[“Company.Dept.Group”, “Company.Dept”, “Company”]
Given that a while loop in Python (I believe) will test only an expression and not a statement as shown below, how would one best write this in idiomatic Python?
#ruby
source = 'Company.Dept.Group.Team'
results = []
temp = source.clone
while (i = temp.rindex('.')) # test statement not supported in Python?
temp = temp[0...i]
results << temp
end
p results # >> ["Company.Dept.Group", "Company.Dept", "Company"]
The Python idiom is something like this: