Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Checking if the combination of two keys is in a dictionary?
#1
Hello,

Hope you are well.

See below in bold, I want check if a key combination of club and year is present in a dictionary and then if it is append the value in [0] to a list but I am having trouble.

Any ideas?

Regards,
MRSENORCHUCK

# dictionary keys will be the (Club)
premier = {}
print()
# open the file
with open(r"C:\AOC_ASL_ASN\Project\League_Table2.csv") as data_file:
# read in the first line containing the headers
headers = data_file.readline()

# for each other line in the file
for line in data_file:
# split each line into components (remove white space from ends of line)
Team,Pos,Pld,HW,HD,HL,HG,HC,AW,AD,AL,AG,AC,W,D,L,G,C,Pts,Champ,Rel,Year = line.strip().split(",")
# insert the data into the dictionary
premier[str(Team),int(Year)] = ((int(Pos), int(Pld), int(HW), int(HD), int(HL), int(HG), int(HC), int(AW), int(AD), int(AL), int(AG), int(AC), int(W), int(D), int(L), int(G), int©,int(Pts),Champ,Rel))


# played list used in calc
Played = []

# go through each key in the dictionary

strTeam = input("Please pick a team: ")

for i in range(1993,2019):
if strTeam, i in premier.keys()
Played.append = premier[strTeam, i][0]


print(Played)


See sample data set
Team,Pos,Pld,HW,HD,HL,HG,HC,AW,AD,AL,AG,AC,W,D,L,G,C,Pts,Champ,Rel,Year
Manchester United,1,42,14,5,2,39,14,10,7,4,28,17,24,12,6,67,31,84,Yes,No,1993
Aston Villa,2,42,13,5,3,36,16,8,6,7,21,24,21,11,10,57,40,74,No,No,1993
Norwich City,3,42,13,6,2,31,19,8,3,10,30,46,21,9,12,61,65,72,No,No,1993
Blackburn Rovers,4,42,13,4,4,38,18,7,7,7,30,28,20,11,11,68,46,71,No,No,1993
Quote
#2
The current implementation does not work because keys in a dict cannot be looked up like that. You could use the namedTuple in the Collections module or try a Numpy Dataframe. If you go down the namedTuple route, it could benefit you to inherit it and write a method to do the search.
Quote
#3
(Dec-03-2019, 10:28 PM)stullis Wrote: The current implementation does not work because keys in a dict cannot be looked up like that. You could use the namedTuple in the Collections module or try a Numpy Dataframe. If you go down the namedTuple route, it could benefit you to inherit it and write a method to do the search.
How would the named tuple approach work?
Quote
#4
Here's an example of what you could do:

from collections import namedtuple

test = namedtuple("test", ["x","y","z"])
x = [test(4,5,6), test(5,6,7)]

for each in x:
    print(x.x)
    print(x.y)
    print(x.z)

Quote
#5
(Dec-03-2019, 10:58 PM)stullis Wrote: Here's an example of what you could do:

from collections import namedtuple

test = namedtuple("test", ["x","y","z"])
x = [test(4,5,6), test(5,6,7)]

for each in x:
    print(x.x)
    print(x.y)
    print(x.z)


Thank you for your reply.

So currently my premier dictionalry has two keys and it has to have two to make the key value unique.

These keys are Team and Year.

Would my best option be to create a new dictionary based on my existing dictionary for a named team?

For example a team like Leeds United dosen't have a year value for 2008 etc
Quote
#6
(Dec-03-2019, 11:12 PM)mrsenorchuck Wrote: So currently my premier dictionalry has two keys and it has to have two to make the key value unique.

These keys are Team and Year.

Would my best option be to create a new dictionary based on my existing dictionary for a named team?

For example a team like Leeds United dosen't have a year value for 2008 etc

It sounds like you need multiple dimensions to your dictionary.
A dictionary is a singular key: value relationship, so you can only use one key in a dictionary.
For instance:
grades = {"Mike": 99, "Sally": 100, "Thomas": 82}
In this dictionary the students' names are the keys and their grades are the values.
So:
print(grades["Mike"])
Gives us:
Output:
99
If we wanted to store multiple grades per student we could make a dictionary of dictionaries, as such:
grades = {"Mike": {"Test": 100, "Quiz": 98, "Homework": 99},
          "Sally": {"Test": 100, "Quiz": 100, "Homework": 100},
          "Thomas": {"Test": 85, "Quiz": 82, "Homework": 79} }
Then we could access any given grade by first the name of the student, then the type of grade, as such:
print(grades["Mike"]["Quiz"])
Which gives us:
Output:
98
For each type of key that you'd like to use, you will need to nest another dictionary. In you case the inner dictionaries should all be the same as shown in this example. I believe you will need three levels of dictionaries.
Try to put some code together based on this. You are more likely to get help on code that you've made and tested, especially if you post all the code using the formatting tools and your results or tracebacks in the case of errors.
mrsenorchuck and perfringo like this post
Quote
#7
(Dec-04-2019, 02:26 AM)Clunk_Head Wrote:
(Dec-03-2019, 11:12 PM)mrsenorchuck Wrote: So currently my premier dictionalry has two keys and it has to have two to make the key value unique.

These keys are Team and Year.

Would my best option be to create a new dictionary based on my existing dictionary for a named team?

For example a team like Leeds United dosen't have a year value for 2008 etc

It sounds like you need multiple dimensions to your dictionary.
A dictionary is a singular key: value relationship, so you can only use one key in a dictionary.
For instance:
grades = {"Mike": 99, "Sally": 100, "Thomas": 82}
In this dictionary the students' names are the keys and their grades are the values.
So:
print(grades["Mike"])
Gives us:
Output:
99
If we wanted to store multiple grades per student we could make a dictionary of dictionaries, as such:
grades = {"Mike": {"Test": 100, "Quiz": 98, "Homework": 99},
          "Sally": {"Test": 100, "Quiz": 100, "Homework": 100},
          "Thomas": {"Test": 85, "Quiz": 82, "Homework": 79} }
Then we could access any given grade by first the name of the student, then the type of grade, as such:
print(grades["Mike"]["Quiz"])
Which gives us:
Output:
98
For each type of key that you'd like to use, you will need to nest another dictionary. In you case the inner dictionaries should all be the same as shown in this example. I believe you will need three levels of dictionaries.
Try to put some code together based on this. You are more likely to get help on code that you've made and tested, especially if you post all the code using the formatting tools and your results or tracebacks in the case of errors.

Thank you for your reply.

I think my best option is to initally loop through the table and create a list of years "teamyears = []" for the team.

I can then use teamyears along with the club as the basis of filtering.

Regards
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Retrieving dictionary keys within with another dictionay bazcurtis 8 243 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 305 Jun-13-2019, 07:02 AM
Last Post: perfringo
  json.dumps to keep dictionary keys batchenr 1 238 May-14-2019, 11:17 AM
Last Post: buran
  Reference new dictionary keys with a variable slouw 4 363 May-07-2019, 03:30 AM
Last Post: slouw
  while + if + elif + else combination ClassicalSoul 1 346 Mar-27-2019, 03:54 PM
Last Post: ichabod801
  Get specific key from multiple keys in python dictionary pradeepkumarbe 0 360 Mar-24-2019, 07:23 PM
Last Post: pradeepkumarbe
  Most efficient way to define sub keys of a dictionary? wrybread 1 361 Feb-21-2019, 12:23 AM
Last Post: snippsat
  Need help for extractring keys from dictionary dragan979 3 551 Jan-23-2019, 02:44 PM
Last Post: buran
  Is there a datepicker which can be used in combination with the python library urwid? AFoeee 5 1,227 Nov-18-2018, 08:54 PM
Last Post: AFoeee
  A combination of Python and MySql xgrzeniu 2 1,074 Mar-28-2018, 06:50 AM
Last Post: xgrzeniu

Forum Jump:


Users browsing this thread: 1 Guest(s)