问题描述:

I've got this as a homework question and dont know how I should go about it.

Firstly, I've been given a dataset with a list of employees' names, addresses, emails etc. with about 50 employees in total.

You are asked to write an application to provide information about the members of staff. Your program should prompt the user to enter search criteria. Any members of staff who match the search criteria should be printed to the screen in the following format:

Position Designation Room and Extension Name and Email Address

(columns are tab-separated)

Matching information............

You will have to amend the dataset for processing, and you may choose to hold this in a separate file, although this is not necessary. Your program should satisfy certain constraints:

  • You should compare each column in the dataset against the search criteria.
  • The comparisons should not be case-sensitive.
  • All output should be in initial capitals, apart from the email address.
  • If a match is found, the results line should be printed and the columns should all line up.
  • Where there is no match, a message should be printed, without a title line.

You should save (1) your program, and (2) a paragraph to explain HOW you accomplished the processing of the dataset.

You should also run these test cases on your application:

  • Search for ‘brenda’
  • Search for all clerical staff.
  • Search for ‘BredNa’
  • Find Dr Carr’s position
  • Which office is Neil located in?

So, firstly, how should I read this dataset? Should I read it in as a text file or create a tuple, dictionary? etc.


staff = [['prof.liam maguire','head of school','academic','MS127','75605','[email protected]'],

['prof. martin McGinnity','director of intelligent systems research centre','academic','MS112','75616','[email protected]'],

['dr laxmidhar Behera','reader','academic','MS107','75276','[email protected]'],

['dr girijesh Prasad','professor','academic','MS137','75645','[email protected]'],

['dr kevin Curran','senior lecturer','academic','MS130','75565','[email protected]'],

['mr aiden McCaughey','Senior Lecturer','academic','MG126','75131','[email protected]'],

['dr tom Lunney','postgraduate courses co-ordinator (Senior Lecturer)','academic','MG121D','75388','[email protected]'],

['dr heather Sayers','undergraduate courses','co-ordinator (Senior Lecturer)','academic','MG121C','75148','[email protected]'],

['dr liam Mc Daid','senior lecturer','academic','MS016','75452','[email protected]'],

['mr derek Woods','senior lecturer','academic','MS134','75380','[email protected]'],

['dr ammar Belatreche','lecturer','academic','MS104','75185','[email protected]'],

['mr michael Callaghan','lecturer','academic','MS132','75771','[email protected]'],

['dr sonya Coleman','lecturer','academic','MS133','75030','[email protected]'],

['dr joan Condell','lecturer','academic','MS131','75024','[email protected]'],

['dr damien Coyle','lecturer','academic','MS103','75170','[email protected]'],

['mr martin Doherty','lecturer','academic','MG121A','75552','[email protected]'],

['dr jim Harkin','lecturer','academic','MS108','75128','[email protected]'],

['dr yuhua Li','lecturer','academic','MS106','75528','[email protected]'],

['dr sandra Moffett','lecturer','academic','MS015','75381','[email protected]'],

['mrs mairin Nicell','lecturer','academic','MG127','75007','[email protected]'],

['mrs maeve Paris','lecturer','academic','MG040','75212','[email protected]'],

['dr jose Santos','lecturer','academic','MG035','75034','[email protected]'],

['dr nH. Siddique','lecturer','academic','MG037','75340','[email protected]'],

['dr zumao Weng','lecturer','academic','MG050','75358','[email protected]'],

['dr shane Wilson','lecturer','academic','MG038','75527','[email protected]'],

['dr caitriona carr','computing and Technical Support','MG121B','75003','[email protected]'],

['mr neil McDonnell','technical Services Supervisor','computing and Technical Support','MS030 / MF143','75360','[email protected]'],

['mr paddy McDonough','technical Services Engineer','computing and Technical Support','MS034','75322','[email protected]'],

['mr bernard McGarry','network Assistant','computing and Technical Support','MG132','75644','[email protected]'],

['mr stephen Friel','secretary','clerical staff','MG048','75148','[email protected]'],

['ms emma McLaughlin','secretary','clerical staff','MG048','75153','[email protected]'],

['mrs. brenda Plummer','secretary','clerical staff','MS126','75605','[email protected]'],

['miss paula Sheerin','secretary','clerical staff','MS111','75616','[email protected]'],

['mrs michelle Stewart','secretary','clerical staff','MG048','75382','[email protected]']]

matches = []

criterion = input ("please enter search criterion: ")

criterion = criterion.lower()

for person in staff:

for characteristic in person:

if characteristic in person:

if criterion in characteristic:

matches.append(person)

break

if len(matches) == 0:

print("No Match")

else:

print("POSITION |||DESIGNATION ||| EXT & ROOM NO||| NAME & EMAIL")

for i in matches:

print (i[1].title(),': ',i[2].title(),':',i[3].upper()+ i[4],':',i[0].title(), i[5].title())`

This is what ive came up with so far and it seems to work, is there any improvements you would make?

网友答案:

Thank you for being honest and telling us that this is a homework question. StackOverflow discourages straight-up giving out answers to homework questions, but we can guide you towards the correct answer.

With regards to "amend the dataset for processing": This implies that the data is not currently in a consistent format. The first thing you need to do is look at the data you've been given, and decide the best representation for the data.

I'd recommend a columnar tab-delimited data file- this is easily created in Microsoft Excel by placing the data in a spreadsheet, and saving it as text. (Excel will complain that it will lose all the various things that make it a spreadsheet instead of a text file, but that's okay- you want a text file.) Save the updated file.

Excel produced what's called a tab-delimited text file: a 2-dimensional grid of data (like the shape of a spreadsheet), represented with one row of data per line (rephrased, the linebreak symbol is used to separate rows of data, which text editors interpret as a command to start writing on a new line), and the tab character (written in Python within an escaped string as \t, but really a single character of its own) separating cells within each row. This is also known as tab-separated values, or TSV. Closely related is comma-separated values, or CSV, which is another option in Excel. CSV can also stand for character-separated values, which is the general term for any text file representing a grid of data by using some character (',' for comma-separated, '\t' for tab-separated) to separate records.

CSVs are a very common file format, so Python is ready to help you out here. Python has a library, csv, designed to read these files for you. If you used Excel text format, you'll need to tell it your dialect is excel-tab, since that symbolizes tab-delimited files as Excel outputs them.

You'll need to construct a csv.reader to read your formatted data file. Use the order you put the columns in to understand the lists that you get when you read the CSV one line at a time- the order of the columns and the order of the items in each row is the same, so use that information to index correctly into the list to find each field.

Once you've read a row, what do you want to do with it?

You have a choice of storage formats in your program:

  • Save every record to a list (acting like a list of lists, since each record acts like a list). Now it's loaded, and when you want to search it, you iterate over your entire list-of-lists and use equality testing to find matches. This can be done with a list comprehension, which is almost certainly what your teacher is looking for.
  • Additionally, create a dict for each column of the file, and store every record in every dictionary: each dict maps that column value to your key. There's a catch here! A dict can only store one record for each key, but you'll definitely have different personnel in the same "designation" (multiple professors, multiple clerical staff, etc.) and there's no way to be sure that no two people will have the same name, either. Your indexing dicts will have to store lists of records themselves, not just single records.

The second approach is much faster for repeated queries, since you're organizing all the records at the start for fast lookup. The first is much easier to implement, however, and is more likely to be what your teacher expects. I'd recommend implementing the first, understanding it, and then if you have time, implement the second.

The user interface for all this is up to you, of course, but this should put you well on your way to implementing the core of the program. Good luck.

网友答案:

I assume you have your dataset as a plain text file (or copyable text in email, etc.) Then you have two several choices:

  1. Create a text file where each line stores info about one employee in the format specified: "Name", "Position", etc. In this case, to do the search you'll need to scan through the file and print the matching line, then repeat the matching part.

  2. Use Python data types to store the info in memory, e.g. a list of dicts with keys "Name", "Position", etc. Then the search will become a little more complex to do (just a little, really), but you'll be able to format the output in any way you like. But first you'll need to fill the list with data by reading the text file (or hard-code it manually, if you're desperate).

  3. You could somewhat combine the approaches by forming a dict only from matching lines of your file.

  4. You could use a real database engine such as MySQL, but that would be a real overkill for this homework.

网友答案:

This is how I would go about it:

staff_details = [["Prof. Liam Maguire","Head of School","Academic","MS127","75605","[email protected]"],
                 ["Prof. Martin McGinnity","Director of Intelligent Systems Research Centre","Academic","MS112","75616","[email protected]"],
                 ["Dr Laxmidhar Behera","Reader","Academic","MS107","75276", "[email protected]"],
                 ["Dr  Girijesh Prasad","Professor","Academic","MS137","75645","[email protected]"],
                 ["Dr  Kevin Curran","Senior Lecturer","Academic","MS130","75565","[email protected]"],
                 ["Mr Aiden McCaughey","Senior Lecturer","Academic","MG126","75131","[email protected]"],
                 ["Dr Tom Lunney","Postgraduate Courses’ Co-ordinator (Senior Lecturer) ","Academic","MG121D","75388","[email protected]"],
                 ["Dr Heather Sayers","Undergraduate Courses’ Co-ordinator (Senior Lecturer) ","Academic","MG121C","75148","[email protected]"],
                 ["Dr  Liam Mc Daid","Senior Lecturer","Academic","MS016","75452","[email protected]"],
                 ["Mr Derek Woods","Senior Lecturer","Academic","MS134","75380","[email protected]"],
                 ["Dr Ammar Belatreche","Lecturer","Academic","MS104","75185","[email protected]"],
                 ["Mr Michael Callaghan","Lecturer","Academic","MS132","75771","[email protected]"],
                 ["Dr  Sonya Coleman","Lecturer","Academic","MS133","75030","[email protected]"],
                 ["Dr  Joan Condell","Lecturer","Academic","MS131","75024","[email protected]"],
                 ["Dr Damien Coyle","Lecturer","Academic","MS103","75170","[email protected]"],
                 ["Mr Martin Doherty","Lecturer","Academic","MG121A","75552","[email protected]"],
                 ["Dr  Jim Harkin","Lecturer","Academic","MS108","75128","[email protected]"],
                 ["Dr Yuhua Li","Lecturer","Academic","MS106","75528","[email protected]"],
                 ["Dr  Sandra Moffett","Lecturer","Academic","MS015","75381","[email protected]"],
                 ["Mrs Mairin Nicell","Lecturer","Academic","MG127","75007","[email protected]"],
                 ["Mrs Maeve Paris","Lecturer","Academic","MG040","75212","[email protected]"],
                 ["Dr Jose Santos","Lecturer","Academic","MG035","75034","[email protected]"],
                 ["Dr  NH. Siddique","Lecturer","Academic","MG037","75340","[email protected]"],
                 ["Dr  Zumao Weng","Lecturer","Academic","MG050 ","75358","[email protected]"],
                 ["Dr  Shane Wilson","Lecturer","Academic","MG038","75527","[email protected]"],
                 ["Dr Caitriona Carr","Technical Services Engineer","Computing and Technical Support","MG121B","75003","[email protected]"],
                 ["Mr Neil McDonnell","Technical Services Supervisor","Computing and Technical Support","MS030 / MF143","75360", "[email protected]"],
                 ["Mr Paddy McDonough","Technical Services Engineer","Computing and Technical Support","MS034","75322","[email protected]"],
                 ["Mr Bernard McGarry","Network Assistant","Computing and Technical Support","MG132","75644","[email protected]"],
                 ["Mr Stephen Friel","Secretary","Clerical Staff","MG048","75148","[email protected]"],
                 ["Ms Emma McLaughlin","Secretary","Clerical Staff","MG048","75153","[email protected]"],
                 ["Mrs. Brenda Plummer","Secretary","Clerical Staff","MS126","75605","[email protected]"],
                 ["Miss Paula Sheerin","Secretary","Clerical Staff","MS111","75616","[email protected]"],
                 ["Mrs Michelle Stewart","Secretary","Clerical Staff","MG048","75382","[email protected]"]]

search_result = []

search_input = input ("Please enter a search criterion: ")
search_input = search_input.title()

for person in staff_details:
    for characteristic in person:
 if characteristic in person:
     if search_input in characteristic:
             search_result.append(person)
              break

if len(search_result) == 0:
    print ("No staff members match your search criterion of ->", search_input)


else:
    print("We have a match!")
    print ("{0:<30} {1:<40} {2:<40} {3:<50}".format("Position:", "Designation:", "Room and Extension:", "Name and Email:"))
    print ("-" * 160)

for align in search_result:
    print("{0:<30} {1:<40} {2:<40} {3:<50}".format((align[1]), (align[2]), (align[3] + ", Ext:" + align[4]), align[0] + "(" + align[5] + ")"))

I hope this helps you out!

相关阅读:
Top