woooee's Profile User Rating: -----

Reputation: 21 Tradesman
Group:
Active Members
Active Posts:
78 (0.12 per day)
Joined:
21-November 12
Profile Views:
464
Last Active:
User is offline May 28 2014 12:17 PM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   woooee has not set their status

Posts I've Made

  1. In Topic: Help keeping track of attendees

    Posted 28 May 2014

    Use a list of lists, which makes it easier to understand. See code below.

    A list of dictionaries containing one attendee only has no advantage, and in fact adds one more layer when looking up items. You must first go through each dictionary in the list, and then look at "name" or "company" for that single entry. It would be better to use one dictionary that contains everyone so you could go right to the person, or a list of lists, with one sub-list for each person, and then you could check item[0] for name, [1] for company, etc.

    In trying to keep it simple the code posted says that the assumption made is that no two people have the same name, so the name can be used for the key. If you want to use a dictionary and allow for multiple names that are the same, then you would use a unique number for the id and a lookup dictionary of name to a list if id's. Looking up the name would give one or more id's that would go directly to the person(s) but I am pretty sure this is beyond what you want to do.
    ## note that I do not have time to test this code so 
    ## see it as an outline example, and correct typos yourself
    def add(attendees_list):
        name = input("Enter name: ")
        company = input("Enter company name: ")
        state = input("Enter state: ")
        email = input("Enter email: ")
    
        ## note the brackets, i.e. appending a sub_list)
        attendees_list.append([name, company, state, email])
        return attendees_list
    
    def display(attendees_list):
        for lit in ["Name", "Company", "State", "e-mail"]:
            print "%20s" % (lit),
            print
    
        for sub_list in attendees_list:
            for ctr in range(len(sub_list)):
                print "%20s" % (sub_list[ctr]),
            print
    
    def display_state(state, attendees_list):
        """ display all attendees that match the parameter "state"
            i.e. [2] in each sub_list
        """
     
        ## redundant code that can and should be put in a common function
        for lit in ["Name", "Company", "State", "e-mail"]:
            print "%20s" % (lit),
            print
    
        for sub_list in attendees_list:
            if sub_list[2] == state:
                for ctr in range(len(sub_list)):
                    print "%20s" % (sub_list[ctr]),
                print
    
    ## first call = empty list
    attendees = []
    attendees= add(attendees)
     
    
  2. In Topic: How to compare a list to a dictionary.

    Posted 28 May 2014

    This is just formatting to print under the proper heading. A list works fine for this, and the program assumes you want to display it on the console.
    def print_line(line_list):
        """ format and print one line
        """
        for value in line_list:
            print "%7s" % (value),
        print
    
    names = [('Tom', '1st'),('James','8th'),('Andrew','3rd'),('Mary','2nd'),
             ('David','8th'),('Jason','5th')]
    days = ('Name','1st','2nd','3rd','4th','5th','6th','7th','8th', '9th')
    
    print_line(days)
    
    for name, date in names:
        line = ["" for ctr in range(len(days))]  ## empty list
        line[0] = name
        ##strip number --> dates have either one or two digits
        if date[1].isdigit():
            number = int(date[:2])
        else:
            number = int(date[0])
    
        line[number] = 1
        print_line(line) 
    
  3. In Topic: Help keeping track of attendees

    Posted 27 May 2014

    Dictionaries can not have more than one key the same. So every time you use info["name"] it would over-write the previous entry, except you redefine the dictionary as empty each time the function is called. You have to have a unique key for each person in the dictionary, so let's assume that no two people have the same name (although you could also add to a number each time and use that as the unique key).
    def add_name(name, company, state, email, info_dict):
        """ assume each name is unique so it can be used as key
        """
        info_dict[name] = [company, state, email]
        return info_dict
    
    def find_name(name, info_dict):
        print name,
        if name in info_dict:
            print "Found"
        else:
            print "Not Found"
    
    def find_company(company_name, info_dict):
        """ avoid possible duplicates in other fields by testing [0]=company only
        """
        print company_name,
        for key in info_dict:
            if info_dict[key][0] == company_name:
                print "Found"
                return
        print "Not Found"
    
    test_data = [["John Doe", "ABC Company", "California", "JDoe@ABC,com"],
                 ["Mary Smith", "XYZ Inc", "Oregon", "smitty@XYZ.com"],
                 ["Bill Green", "Conglomerate, Inc", "Delaware", "Bill@Conglormeate"]]
    info_dict = {}
    for name, company, state, email in test_data:
        info_dict=add_name(name, company, state, email, info_dict)
        print info_dict, "\n"
    
    for name in ["John Doe", "Mary SMith", "Bill Gren"]:
        find_name(name, info_dict)
    print
    for company in ["ABCD Company", "XYZ Inc", "Conglomerate Inc"]:
        find_company(company, info_dict) 
    
  4. In Topic: Tricky Lexicographically

    Posted 27 May 2014

    You can certainly rearrange the data into the required form, but before spending time on code that won't be used, do you you want to modify your existing code to keep it in the correct order (which we don't know how to do since we don't have the input) or is "sorting" afterwards OK?
  5. In Topic: Help finding substrings

    Posted 25 May 2014

    Your +1 is outside of the find() function call, and you don't supply the location to add one to. Take a look at the tutorial here. A few simple print statements will tell what is going on.
    DNAString = "GATATATGCATATACTT"
    motif = "ATAT"
    
    ## see if the sub-string is found anywhere
    count = DNAString.find(motif)
    
    ## if sub-string was found, look for more
    while count > -1:
        print count
        count += 1   ## don't want to keep finding the same string
        count = DNAString.find(motif, count) 
    

My Information

Member Title:
D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

woooee hasn't added any friends yet.

Comments

woooee has no profile comments yet. Why not say hello?