By user3651247

2018-02-13 13:24:33 8 Comments

Given a pattern and a string str, find if str follows the same pattern.

Here follows means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.

Examples: pattern = "abba", str = "dog cat cat dog" should return true; dog is a, cat is b and the words form the abba pattern. pattern = "abba", str = "dog cat cat fish" should return false; the string follows a abbc pattern instead.

My solution works in Python 2:

def wordPattern(self, pattern, str):
    s = pattern
    t = str.split()
    return map(s.find, s) == map(t.index, t)

But I am just wondering why this solution does not working in Python 3. There, the function will always return False when trying to test the above examples. Could anyone please give some advice?


@Martijn Pieters 2018-02-13 13:29:30

In Python 3, map() returns an iterator object, not a list. Equality testing between these objects won't work (equality is testing for identity, the exact same object in memory, instead).

Convert to lists explicitly:

def wordPattern(self, pattern, str):
    s = pattern
    t = str.split()
    return list(map(s.find, s)) == list(map(t.index, t))

or use list comprehensions:

def wordPattern(self, pattern, str):
    s = pattern
    t = str.split()
    return [s.find(c) for c in  s] == [t.index(w) for w in t]

or avoid creating lists altogether by comparing the zipped results with the all() function:

from operator import eq
from itertools import starmap, zip_longest

def wordPattern(self, pattern, str):
    s = pattern
    t = str.split()
    return all(starmap(eq, zip_longest(map(s.find, s), map(t.index, t))))

The latter short-circuits without having to make all comparisons if there is no match. In the spirit of keeping the functional style, I used itertools.starmap() to test for equality with the operator.eq() function. By using itertools.zip_longest() we make sure that we can detect the case where the pattern length and word count don't match.

@Patrick Artner 2018-02-13 13:36:03

thanks for the corrections :) yours already tells the story to tell so mine is superflous.

@Chris_Rands 2018-02-13 13:37:21

I heard that list1 == list2 comparisons short circuit too, is that right? see comment here

@Martijn Pieters 2018-02-13 13:39:22

@Chris_Rands: it does, but you first have to do two full iterations to create the lists.

@Chris_Rands 2018-02-13 13:40:07

@MartijnPieters of course, thanks!

@luc 2018-02-13 13:58:14

@MartijnPieters I think that the last solution only works if s and t have the same number of elements. Did I miss something?

@luc 2018-02-13 13:59:23

wordPattern(None, "abba", "dog cat cat dog dog") should return false and the last example returns true. You should check the number of elements first, i think

@Martijn Pieters 2018-02-13 14:00:24

@luc: good point. itertools.zip_longest() is needed.

@luc 2018-02-13 14:00:57

The reason is that the zip function stops when iteration on one argument is done. additional elements on the other arguments are ignored

@Martijn Pieters 2018-02-13 14:02:31

@luc: yes, I know why :-P

@luc 2018-02-13 14:04:36

@MartijnPieters : with 622k of reputation, i imagine that you know it :-) Just explaining for any other wondering why. Thanks for edit and solution.

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod in Python?

24 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 2430411 View
  • 2131 Score
  • 24 Answer
  • Tags:   python list

16 Answered Questions

[SOLVED] What's the difference between lists and tuples?

  • 2009-03-09 15:41:25
  • Lucas Gabriel Sánchez
  • 326808 View
  • 821 Score
  • 16 Answer
  • Tags:   python list tuples

6 Answered Questions

[SOLVED] What are the differences between type() and isinstance()?

21 Answered Questions

[SOLVED] Difference between __str__ and __repr__?

22 Answered Questions

[SOLVED] Difference between append vs. extend list methods in Python

5 Answered Questions

[SOLVED] What's the difference between raw_input() and input() in python3.x?

  • 2011-02-06 18:52:34
  • pkumar
  • 242909 View
  • 296 Score
  • 5 Answer
  • Tags:   python python-3.x

8 Answered Questions

[SOLVED] Is there a difference between foreach and map?

2 Answered Questions

[SOLVED] boolean loopd keys strings comparator

3 Answered Questions

[SOLVED] Python: Clever ways at string manipulation

  • 2011-01-19 16:24:02
  • Parseltongue
  • 878 View
  • 2 Score
  • 3 Answer
  • Tags:   python string

Sponsored Content