text.txt
Natha Keefe 3.14 Engineering Biotech Chemistry
Crescentia Dow 3.79 Chemistry Physics Mathematics
Randon Bradhust 3.68 Biotech Engineering Chemistry
Dashawn Bosley 3.54 Mathematics Chemistry Biotech
Nicolasa Sumpter 3.82 Engineering Mathematics Biotech
Cressie Gillespie 3.10 Physics Mathematics Engineering
Tawny Crockett 3.01 Chemistry Biotech Physics
Kennon Inverarity 3.50 Mathematics Engineering Chemistry
Halima Brydone 3.72 Chemistry Physics Mathematics
Esther Bratby 2.67 Mathematics Chemistry Biotech
Lorry Bunger 3.79 Engineering Biotech Physics
Fatemah Desavigny 3.00 Physics Mathematics Engineering
Marti Mclaws 3.05 Engineering Chemistry Biotech
Estephanie Phelps 3.21 Chemistry Physics Mathematics
Tommi Peerless 3.85 Engineering Physics Mathematics
Cynthia Hermitton 3.10 Engineering Biotech Chemistry
Cheyla Hankinson 3.82 Engineering Mathematics Biotech
Giovanna Keel 3.25 Physics Mathematics Engineering
Narissa Worthington 3.30 Biotech Chemistry Mathematics
Aundria Guthrie 3.51 Mathematics Chemistry Engineering
Teneil Maclean 3.22 Mathematics Physics Chemistry
Shealynn Melville 3.02 Mathematics Chemistry Physics
Darrah Smyth 3.89 Physics Biotech Engineering
with open("test.txt") as open:
initial_list = [i.split() for i in open.read().splitlines()]
applicants = sorted(initial_list, key= lambda x: -float(x[2])) #by rank
by_deps = {"Biotech": [], "Chemistry": [], "Engineering": [], "Mathematics": [], "Physics": []}
limit = int(input())
for ii in range(3, 6):
for i in applicants:
if len(by_deps[i[ii]]) < limit:
by_deps[i[ii]].append(i)
initial_list.remove(i)
What I want to do:
- To open a text file and read values
- To convert in a format I need
- To sort that list according to item 3, 4, 5 in each list usuing a dictionary
- To delete a sorted item from list
The problem is that i have error ValueError: list.remove(x): x not in list . In the final I want to have applicants parsed to dicionary and initial list should be empty. How can I make it?
P.S.: This code:
for i in applicants:
if len(by_deps[i[3]]) < limit:
by_deps[i[3]].append(i)
initial_list.remove(i)
elif len(by_deps[i[4]]) < limit:
by_deps[i[4]].append(i)
initial_list.remove(i)
elif len(by_deps[i[5]]) < limit:
by_deps[i[5]].append(i)
initial_list.remove(i)
does what I want. I`m curios is it possible to have a construction with 2 loops for this?
CodePudding user response:
The first time through the for ii
loop you remove i
from applicants
. So everything will be removed when you do the next iteration.
You can solve this by reordering the loops.
for i in initial_list:
for ii in range(3, 6):
deps[i[ii]].append(i)
applicants.remove(i)
CodePudding user response:
I rewrite code a little bit and it works:
with open("applicants.txt") as open:
applicants = [i.split() for i in open.read().splitlines()]
by_deps = {"Biotech": [], "Chemistry": [], "Engineering": [], "Mathematics": [], "Physics": []}
limit = int(input())
for ii in range(3, 6):
for i in sorted(applicants, key= lambda x: (-float(x[2]), x[0], x[1])): #by rank and names
if len(by_deps[i[ii]]) < limit: #by deps in priority
by_deps[i[ii]].append(i)
applicants.remove(i)
Thanks everyone for help.