Re: create lowercase strings in lists - was: (No subject)

Discussion in 'Python' started by Mark Devine, Dec 17, 2004.

  1. Mark Devine

    Mark Devine Guest

    Thanks for the help. This is the final script:

    #!/usr/bin/env python
    import os
    import sys
    import time
    import string
    import pexpect
    import commands


    # Test if the words of list2 elements appear in any order in list1 elements
    # disregarding case and parens

    # Reference list
    list1 = ["a b C (D)", "D A B", "A B E"]
    # Test list
    list2 = ["A B C D", "A B D", "A E F", "A (E) B", "A B", "E A B" ]

    def normalize(text, unwanted = "()", table = string.maketrans(string.ascii_uppercase,string.ascii_lowercase)):
    text.translate(table,unwanted)
    return set(text.split())


    reflist = [normalize(element) for element in list1]
    print reflist

    #This is the list of sets to test against


    def testmember(element):
    """is element a member of the reflist, according to the above rules?"""
    testelement = normalize(element)
    #brute force comparison until match - depends on small reflist
    for el in reflist:
    if el.issuperset(testelement):
    return True
    return False

    for element in list2:
    print element, testmember(element)


    the trouble is it throws up the following error for set:

    $ ./test.py
    Traceback (most recent call last):
    File "./test.py", line 23, in ?
    reflist = [normalize(element) for element in list1]
    File "./test.py", line 20, in normalize
    return set(text.split())
    NameError: global name 'set' is not defined

    when I checked http://docs.python.org/lib/genindex.html#letter-s

    it said that
    set() (built-in function)

    The python I use is the one that comes with cygwin. Does anybody know if the following version of python is incomplete or do I need to call built in functions in a different way?

    $ python
    Python 2.3.4 (#1, Jun 13 2004, 11:21:03)
    [GCC 3.3.1 (cygming special)] on cygwin
    Type "help", "copyright", "credits" or "license" for more information.


    Michael Spencer <> wrote:

    >
    > Steve Holden wrote:
    > > Mark Devine wrote:
    > >
    > >> Actually what I want is element 'class-map match-all cmap1' from list
    > >> 1 to match 'class-map cmap1 (match-all)' or 'class-map cmap1 mark
    > >> match-all done' in list 2 but not to match 'class-map cmap1'.
    > >> Each element in both lists have multiple words in them. If all the
    > >> words of any element of the first list appear in any order within any
    > >> element of the second list I want a match but if any of the words are
    > >> missing then there is no match. There are far more elements in list 2
    > >> than in list 1.
    > >>

    > >

    > sounds like a case for sets...
    >
    > >>> # NB Python 2.4

    > ...
    > >>> # Test if the words of list2 elements appear in any order in list1 elements
    > >>> # disregarding case and parens

    > ...
    > >>> # Reference list
    > >>> list1 = ["a b C (D)",

    > ... "D A B",
    > ... "A B E"]
    > >>> # Test list
    > >>> list2 = ["A B C D", #True

    > ... "A B D", #True
    > ... "A E F", #False
    > ... "A (E) B", #True
    > ... "A B", #True
    > ... "E A B" ]
    > ...
    > >>> def normalize(text, unwanted = "()"):

    > ... conv = "".join(char.lower() for char in text if char not in unwanted)
    > ... return set(conv.split())
    > ...
    > >>> reflist = [normalize(element) for element in list1]
    > >>> print reflist

    > ...
    > [set(['a', 'c', 'b', 'd']), set(['a', 'b', 'd']), set(['a', 'b', 'e'])]
    >
    > This is the list of sets to test against
    >
    >
    > >>> def testmember(element):

    > ... """is element a member of the reflist, according to the above rules?"""
    > ... testelement = normalize(element)
    > ... #brute force comparison until match - depends on small reflist
    > ... for el in reflist:
    > ... if el.issuperset(testelement):
    > ... return True
    > ... return False
    > ...
    > >>> for element in list2:

    > ... print element, testmember(element)
    > ...
    > A B C D True
    > A B D True
    > A E F False
    > A (E) B True
    > A B True
    > E A B True
    > >>>

    >
    > Michael
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    _________________________________________________________________
    Sign up for eircom broadband now and get a free two month trial.*
    Phone 1850 73 00 73 or visit http://home.eircom.net/broadbandoffer
     
    Mark Devine, Dec 17, 2004
    #1
    1. Advertising

  2. Mark Devine wrote:
    > the trouble is it throws up the following error for set:
    >
    > $ ./test.py
    > Traceback (most recent call last):
    > File "./test.py", line 23, in ?
    > reflist = [normalize(element) for element in list1]
    > File "./test.py", line 20, in normalize
    > return set(text.split())
    > NameError: global name 'set' is not defined
    >


    The set type became a builtin in Python 2.4. I would suggest upgrading,
    but if this is not an option, you can put this at the top of the file,
    and it should get rid of the NameError:

    from sets import Set as set

    Steve
     
    Steven Bethard, Dec 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Devine
    Replies:
    2
    Views:
    292
    Mike Meyer
    Dec 16, 2004
  2. Mark Devine
    Replies:
    6
    Views:
    310
    Michael Spencer
    Dec 17, 2004
  3. Mark Devine
    Replies:
    3
    Views:
    775
    Steve Holden
    Dec 17, 2004
  4. robin
    Replies:
    10
    Views:
    551
    Dave Hansen
    Apr 12, 2006
  5. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    424
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
Loading...

Share This Page