Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Python strip() on a split() string do anything?

Based on some experiments, it appears to me that the following Python v2.7 code:

def lookup_pattern(pattern, file_containing_patterns):
  for line in file_containing_patterns:
    splits = line.split()
    if splits:
      if (pattern == splits[0]):
        return map(lambda x: x.strip(), splits[1:])
  return None

Could be simplified as follows to drop the map of strip():

def lookup_pattern(pattern, file_containing_patterns):
  for line in file_containing_patterns:
    splits = line.split()
    if splits:
      if (pattern == splits[0]):
        return splits[1:]
  return None

I believe this is true because the split() should remove all white space and thus the strip() would be a no-op.

Are there any cases where in the above two are not identical in effect, if so, what are they?

like image 991
WilliamKF Avatar asked Mar 27 '13 17:03

WilliamKF


1 Answers

The documentation indicates that split with the implicit whitespace delimiter (that is, split() without further arguments) will clear out any "empty tokens" and you don't need to strip anything. As any consecutive series of whitespace could be interpreted as a list of empty tokens delimited by space, that'll mean the strings get trimmed automatically.

If, instead you split with a different delimiter or implicitly defined the whitespace, this may happen:

' 1  2   3  '.split()
=> ['1', '2', '3']

'  1  2   3  '.split(None, 1)
=> ['1', '2   3  ']
like image 188
mike3996 Avatar answered Oct 29 '22 02:10

mike3996