1

How can I match two lists of letters without considering the order of letters appearance in that lists in Python

Eg: Think my first list is ['a','b','c','d'] and I want to match this list with another list ['b','c','a','d'] then to get a out put as true. How to do this? I'm new to python and want your help!

Thanks in advance

Grant
  • 4,413
  • 18
  • 56
  • 82
  • 3
    [What have you tried?](http://whathaveyoutried.com) But I don't know why I bother, someone will your work for you to get the rep points. – Mark Reed Jun 21 '12 at 14:24
  • 3
    @MarkReed You are free to downvote the answers, the question, and to vote to close the question. Even if you would not get rep points for answering questions like this, people would still answer. Yes, I know that questions like this are 'easy earned rep', and it's a pitty that some great answers don't get the attention that they deserve, but just that how stackoverflow works. E.g. look at my second highest voted answer. 15 upvotes for spotting a typo; while some of my answers took nearly half of an hour to write and only got 1 or 2 upvotes. Sad, but true... – sloth Jun 21 '12 at 14:51
  • 1
    @BigYellowCactus: Very true. Somehow, SO hates RTFM questions, but is passionate about RTFM answers, although the latter is just as bad. – georg Jun 21 '12 at 15:06

3 Answers3

11

How about:

# if you don't want to consider duplicates either
output = set(your_first_list) == set(your_second_list)

# if duplicates matter
output = sorted(your_first_list) == sorted(your_second_list)
Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
5

You could sort them:

In [1]: a = list('abcd')
In [2]: b = list('bcad')
In [3]: sorted(a) == sorted(b)
Out[3]: True

In [4]: a == b
Out[4]: False
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • +1, This method is more flexible, in a sense that `a = list('aabcd')` and `b = list('abcd')` will not match. – Akavall Jun 21 '12 at 14:57
5

I had something different in mind, that is, like this:

all(x in a for x in b) and all(x in b for x in a)

This checks if all letters in a occur in b, and all letters of b occur in a. This means that they 'match' if a and b are sets.

But since there was already a good answer, I decided to do a speed comparison, and it turns out my solution is considerably faster than the solution Daren and Lev suggested based on sorted(). For strings with a length under 100 characters, it also outperformed Daren's set(a) == set(b).

import timeit, random, string

def randstring(length):
    return ''.join(random.choice(string.ascii_lowercase) \
                   for i in xrange(length))

def sortmatch(a,b):
    return sorted(a) == sorted(b)

def bothways(a,b):
    return all(x in a for x in b) and all(x in b for x in a)

def setmatch(a,b):
    return set(a) == set(b)

c1 = "sortmatch(a,b)"
c2 = "setmatch(a,b)"
c3 = "bothways(a,b)"

init = """
from __main__ import randstring, sortmatch, bothways, setmatch
a = randstring(%i)
b = randstring(%i)
"""

lengths = [5,20,100,1000,5000]
times = 10000

for n in lengths:

    t1 = timeit.Timer(stmt=c1, setup=init % (n,n))
    t2 = timeit.Timer(stmt=c2, setup=init % (n,n))
    t3 = timeit.Timer(stmt=c3, setup=init % (n,n))

    print("String length: %i" % n)
    print("Sort and match:  %.2f" % (t1.timeit(times)))
    print("Set and match:  %.2f" % (t2.timeit(times)))    
    print("Check both ways: %.2f\n" % (t3.timeit(times)))

Results:

String length: 5
Sort and match: 0.04
Set and match: 0.03
Check both ways: 0.02

String length: 20
Sort and match: 0.11
Set and match: 0.06
Check both ways: 0.02

String length: 100
Sort and match: 0.53
Set and match: 0.16
Check both ways: 0.25

String length: 1000
Sort and match: 6.86
Set and match: 0.89
Check both ways: 3.82

String length: 5000
Sort and match: 36.67
Set and match: 4.28
Check both ways: 19.49

Junuxx
  • 14,011
  • 5
  • 41
  • 71
  • @Lev: No I suppose not. It does work for OP's example though, and it works in general if a and b are sets, and then it's still faster than Daren's set solution for short strings. – Junuxx Jun 21 '12 at 15:08
  • So this mostly shows that sorting and creating sets comes with some overhead, and it can be useful to think about whether you really need that. – Junuxx Jun 21 '12 at 15:20
  • +1, very interesting. thank you for going the extra mile on this one! – Daren Thomas Jun 22 '12 at 07:59