#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Specific Item(s) in a List


    Does anyone know how to select a specific item(s) in a list. Here's what I need it for.
    Code:
            if msg == tr+"ban" and user == mt:
                print "Processing command..."
                if msg[5:6] == "*" and msg[-1] == "*":
                    if msg[6:-1] == mt:
                        pass
                    if ho == "yes":
                        for msg[6:-1] in userlist:
                            q.append(ban+userlist+wildmsg)
                    elif ho == "no":
                        q.append(noops)
                else:
                    if msg[5:224] == mt:
                        pass
                    if ho == "yes":
                        q.append(ban+msg[5:224])
                    elif ho == "no":
                        q.append(noops)
                    else:
                        continue
    The part that I need help with is the for msg[6:-1] in userlist: part. I want it so that when a user types something like, !ban *h*, the bot (my program) will attempt to ban all users in the channel (userlist) who's accounts contained "h". Remember, not just one account that contains that, but ALL of them. Can anyone help me with this?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    17
    Rep Power
    0
    Look up pythons' "re" module. You'll be interested in re.search and using compiled objects for speed. Search the net for examples of use.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you just want to look for usernames which contain a substring then you can use the in keyword; or the find() or index() methods if you were so inclined. Here's an example anyway:

    Code:
    >>> usernames = ['netytan', 'Yegg', 'someone', 'tan']
    >>>
    >>> for username in usernames:
    ...     if 'e' in username:
    ...         print username
    ...
    netytan
    Yegg
    someone
    >>> for username in usernames:
    ...     if 'tan' in username:
    ...         print username
    ...
    netytan
    tan
    >>> for username in usernames:
    ...     if 'one' in username:
    ...         print username
    ...
    someone
    >>> for username in usernames:
    ...     if 'Y' in username:
    ...         print username
    ...
    Yegg
    >>>
    As you can see matches are case-sensitive, but you can get around this by using the lower() method to create a copy of each username/search-string that contains only lowercase letters. Sure you get the idea .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    That code won't work how I want it to. If I did, if 'Y' in username:, the bot would try finding 'Y' in the username who sent the message. I want the program to check through a list and see which items are like the string. The one's that are alike, it will ban them. For example,

    Code:
    if message == "!ban *G*":
         for message[6:-1] in userlist:
              print item
    item will get Replaced with whateve the proper code is. I only need to know how to find the items that contain that string in them. And then after the program finds all of the items, it does something.
    Update:
    I think your code is what I need, I just read it wrong. Let me try and see how it works out.
    Another Update:
    Yep, the code works just fine.

IMN logo majestic logo threadwatch logo seochat tools logo