Re: [Tutor] enumerate over Dictionaries
Thanks Alan and Mats for the explanation. On 05/07/19 19:57, Mats Wichmann wrote: On 7/4/19 3:53 PM, Alan Gauld via Tutor wrote: Does this means that the Dict is ordered? or it is implementation dependent? Neither, it means the items in a list always have indexes starting at zero. By pure coincidence dictionaries in recent Python versions (since 3.6 or 3.7???) retain their insertion order. But that was not always the case, but the result would have been the same so far as the 0,1,2 bit goes. To be a little more precise, in 3.6 CPython insertion order was preserved as an artefact of the new implementation of dicts, but not promised to be that way. Since 3.7 it is guaranteed (it is actually in the language specification, so other Pythons have to do this too now). It's still not the same as a collections.OrderedDict, which has some useful additional features in case you care a lot about ordering. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] enumerate over Dictionaries
On 7/4/19 3:53 PM, Alan Gauld via Tutor wrote: >> Does this means that the Dict is ordered? or it is implementation dependent? > > Neither, it means the items in a list always have indexes > starting at zero. > > By pure coincidence dictionaries in recent Python versions (since 3.6 > or 3.7???) retain their insertion order. But that was not always the > case, but the result would have been the same so far as the 0,1,2 bit goes. > To be a little more precise, in 3.6 CPython insertion order was preserved as an artefact of the new implementation of dicts, but not promised to be that way. Since 3.7 it is guaranteed (it is actually in the language specification, so other Pythons have to do this too now). It's still not the same as a collections.OrderedDict, which has some useful additional features in case you care a lot about ordering. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regarding help in python algorithm
On 05/07/2019 07:25, Suhit Kumar wrote: > I am a student at an university. Currently I was working on an algorithm > using python. It is based on scheduling the teachers to their nearest > venues. And at the venues there can be atmost 400 teachers and these are to > be divided into the Batches of 40 i.e. ten batches. All the batches will > have teachers having same group number assigned to them. Who assigns the group numbers? Is that the program or part of the data? > should get only the two days from the working days in the month. I have no idea what that means. How does time enter into the calculation? You allocate teachers to their *nearest* venue. Where does time enter into that? There must be some other criteria? You schedule to them to the nearest venue that they have not already visited within the last week/month or something? > This is only the last part of the complete algorithm. I am sending the > files associated to it and the code that I have made till now. The code got through but the data didn't. The server only permits text based attachments. However your code is over 300 lines long and poorly structured (hint create some well named functions!) Meanwhile you have not given us any clues about what kind of help you need. Does it work? Does it fail with an error - what error? Does it fail to load the data correctly - in what way? You cannot seriously expect us to wade through 300+ lines of code that, by your own admission, only partially describes the problem with nothing more than a loosely defined problem description. > Please help me in making it as I need it urgently. We won't do your homework for you. Tell us what you are having difficulties with and we will try to help. But first, for all our sakes, go back and restructure your code into functions with reasonable names. Each one performing one clearly defined part of your solution. Then perhaps it will be easier to see where the issue(s) lie and certainly easier to describe them. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Regarding help in python algorithm
Hi, I am a student at an university. Currently I was working on an algorithm using python. It is based on scheduling the teachers to their nearest venues. And at the venues there can be atmost 400 teachers and these are to be divided into the Batches of 40 i.e. ten batches. All the batches will have teachers having same group number assigned to them. and al the batches should get only the two days from the working days in the month. This is only the last part of the complete algorithm. I am sending the files associated to it and the code that I have made till now. Please help me in making it as I need it urgently. Thanks in advance. Regards Suhit import math import csv import pdb import pandas as pd import numpy as np from math import radians, sin, cos, acos def distanceCalculator(latitude1,longitude1,latitude2,longitude2): slat = radians(latitude1) slon = radians(longitude1) elat = radians(latitude2) elon = radians(longitude2) dist = 6371.01 * acos(sin(slat)*sin(elat) + cos(slat)*cos(elat)*cos(slon - elon)) return dist #df = pd.read_csv("venueData.csv",header=None) df2 = pd.read_csv("mtdata.csv",header=None) df2.columns = ['name','location','latitude','longitude','subject'] df = pd.read_csv("venueData.csv",header=None) df.columns=['name','latitude','longitude'] #df2 = pd.read_csv("mtdata.csv",header=None) #df2.columns = ['name','location','latitude','longitude','subject'] teacher = pd.read_csv("teachers.csv") teacher['Latitude'] = teacher['Latitude'].apply(lambda x: x.rstrip(",") if type(x) == str else x ) teacher['Longitude'] = teacher['Longitude'].apply(lambda x: x.rstrip(",") if type(x) == str else x ) listEmpty = [] dictionaryTeacher = {} for i,ex in teacher.iterrows(): lat1 = ex['Latitude'] lon1 = ex['Longitude'] Id = i for b,c in df.iterrows(): lat2 = c['latitude'] lon2 = c['longitude'] nameVen = c['name'] listEmpty.append((distanceCalculator(float(lat1),float(lon1),float(lat2),float(lon2)),Id,b)) demian = [] listEmpty.sort() demian = listEmpty[0] dictionaryTeacher[ex['Name']] = demian listEmpty = [] demian = [] DataTeacher = pd.DataFrame(columns=['Teacher','Distance','Venue','Eng','Hindi','Maths']) number = 3 for ex in dictionaryTeacher: DataTeacher= DataTeacher.append({'Teacher':ex,'Distance':dictionaryTeacher[ex][0],'Venue':df.loc[dictionaryTeacher[ex][2]]['name'],'Eng':teacher.loc[dictionaryTeacher[ex][1]]['Eng'],'Hindi':teacher.loc[dictionaryTeacher[ex][1]]['Hindi'],'Maths':teacher.loc[dictionaryTeacher[ex][1]]['Maths']},ignore_index=True) days = pd.read_csv("days.csv") days.columns = ['January', 'January:Days', 'February', 'Feburary:Days', 'March', 'March:Days', 'April', 'April:Days', 'May', 'May:Days', 'June', 'June:Days', 'July', 'July:Days', 'August', 'August:Days', 'September', 'September:Days', 'October', 'October:Days', 'November', 'November:Days', 'December', 'December:Days'] df.columns=['name','latitude','longitude'] df['name'] = df['name'].apply(lambda x: x.rstrip()) df2['name'] =df2['name'].apply(lambda x: x.rstrip()) venue = {} for i,k in enumerate(df['name']): if (k not in venue): venue[k] = {'January': 0 ,'February':0 , 'March': 0 , 'April':0, 'May':0,'June ':0 ,'July':0 , 'August':0,'September':0,'October':0,'November':0,'December':0} teacher = {} for i,k in enumerate(df2['name']): if (k not in teacher): teacher[k] = {'January': 0 ,'February':0 , 'March': 0 , 'April':0, 'May':0,'June ':0 ,'July':0 , 'August':0,'September':0,'October':0,'November':0,'December':0} dictionary = {} liste = [] for i,ex in df2.iterrows(): nameT = ex['name'] lat1 = ex['latitude'] lon1 = ex['longitude'] sub = ex['subject'] Id = i for b , x in df.iterrows(): nameM = x['name'] lat2 = x['latitude'] lon2 = x['longitude'] id2 = b liste.append((distanceCalculator(lat1,lon1,lat2,lon2),i,id2)) liste.sort() dictionary[ex['name']] = liste[0:3] liste = [] Data = pd.DataFrame(columns=['Trainer','Venue','Distance','Subjects','Location']) for ex in dictionary: for i , k in enumerate(dictionary[ex]): Data = Data.append({'Trainer':ex ,'Venue': df.loc[dictionary[ex][i][2]]['name'],'Distance': dictionary[ex][i][0],'Subjects':df2.loc[dictionary[ex][i][1]]['subject'],'Location':df2.loc[dictionary[ex][i][1]]['location']},ignore_index=True) Data['Month'] = -1 for i,ex in Data.iterrows(): Train = ex['Trainer'] Venue = ex['Venue'] for ex in teacher[Train]: