Check if a tuple contains a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ennoil
    New Member
    • May 2010
    • 11

    Check if a tuple contains a string

    I have a tuple of a tuple (ServerList) that contains the following:

    <host1>,DEV,Inf ormatica,Red_Ha t,64
    <host2>,PROD,In formatica,Red_H at,64
    <host3>,PROD,In formatica,Red_H at,64
    <host4>,QA,Info rmatica,Red_Hat ,64
    <host5>,PROD,Ti bco,Solaris,
    <host6>,TEST,Ti bco,Solaris,

    I have a list (EnvList) that could contain the following:

    ['PROD','Informa tica','Red_Hat' ,'64']

    or any combination.

    Using the the example above I want to create a list which contains:

    ['<host2>','<hos t3>']

    I am hoping for something like

    Code:
    for i in ServerList:
      if i contains EnvList:
        ServerName.append(ServerList[0])
    How can this be actually done?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can do it like this:
    Code:
    serverList = (("<host1>","DEV","Informatica","Red_Hat","64"),
                  ("<host2>","PROD","Informatica","Red_Hat","64"),
                  ("<host3>","PROD","Informatica","Red_Hat","64"),
                  ("<host4>","QA","Informatica","Red_Hat","64"),
                  ("<host5>","PROD","Tibco","Solaris"),
                  ("<host6>","TEST","Tibco","Solaris"))
    
    envList = [['PROD','Informatica','Red_Hat','64'], ["PROD","Tibco","Solaris"]]
    
    output = []
    for item in serverList:
        if list(item[1:]) in envList:
            output.append(item[0])
    Note that the objects being compared must evaluate to be equal in order to work.

    Comment

    • ennoil
      New Member
      • May 2010
      • 11

      #3
      Part of the problem is that not all of the 4 items in EnvList need to exist.
      For example, if I want to pick just the Solaris servers out of the ServerList then EnvList will only contain
      ['Solaris']
      EnvList is created through a series of menus from which the user will pick only then EnvList items that they need.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        You will need an additional loop.
        Code:
        serverList = (("<host1>","DEV","Informatica","Red_Hat","64"),
                      ("<host2>","PROD","Informatica","Red_Hat","64"),
                      ("<host3>","PROD","Informatica","Red_Hat","64"),
                      ("<host4>","QA","Informatica","Red_Hat","64"),
                      ("<host5>","PROD","Tibco","Solaris"),
                      ("<host6>","TEST","Tibco","Solaris"))
        envList = [['PROD','Red_Hat','64'], ["PROD","Solaris"]]
        
        output = []
        for item in serverList:
            for elem in envList:
                if set(elem).issubset(set(item[1:])):
                    output.append(item[0])
                    
        print output
        The results:
        Code:
        >>> ['<host2>', '<host3>', '<host5>']
        >>>

        Comment

        Working...