1 Replies - 449 Views - Last Post: 28 May 2014 - 11:46 AM Rate Topic: -----

#1 TheFremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-May 14

How to compare a list to a dictionary.

Posted 28 May 2014 - 08:28 AM

We currently have a list of names and days to login, which is written to a file.

The program then reads the file and inputs the content to another list which can then be written to another file.

We have list of people and days they login;
aNames = [('Tom', '31st'),('James','28th'),('Andrew','8th'),('Mary','18th'),('David','8th'),('Jason','8th')]



Then a collection of login days
days = ('Name','1th','2nd','3rd','4th','5th','6th','7th','8th')



We would like to try to compare these together, so that it reads something like the attached excel file.




The full code is;


import csv
from collections import Counter
aNames = [('Tom', '31st'),('James','28th'),('Andrew','8th'),('Mary','18th'),('David','8th'),('Jason','8th')]
days = ('Name','1th','2nd','3rd','4th','5th','6th','7th','8th')



print(aNames)
file = open("workers.csv", "w")
with open('workers.csv', 'w' ,encoding='utf-8') as csvfile:
    output = csv.writer(csvfile, delimiter=',', lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
    output.writerows(aNames)
csvfile.closed



with open('workers.csv', 'r' ,encoding='utf-8') as csvfile:
    reader = csv.reader(csvfile)
    workers = {}
    i = 1
    print("\tName\tDate")
    print("=" *20)
    for rows in reader:
       if len(rows) > 0:
          workers.update({rows[0]: rows[1]})
          print("{2}\t {0} \t {1}".format(rows[0],rows[1],i))
          i+=1
print(str(workers))
print("Test")
csvfile.closed


aNames2={}
aNames2=workers



for x in aNames2.values():
   print(x)

 

print(str(aNames2))
Alist2=dict.items(workers)
print(str(aNames2))




file = open("output.csv", "w")
with open('output.csv', 'w' ,encoding='utf-8') as csvfile2:
    output = csv.writer(csvfile2, delimiter=',', lineterminator='\n', quoting=csv.QUOTE_MINIMAL)
    output.writerow(days)
csvfile2.closed


   



Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: How to compare a list to a dictionary.

#2 woooee  Icon User is offline

  • D.I.C Head

Reputation: 45
  • View blog
  • Posts: 168
  • Joined: 21-November 12

Re: How to compare a list to a dictionary.

Posted 28 May 2014 - 11:46 AM

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) 

This post has been edited by woooee: 28 May 2014 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1