Home > database >  Dictionary: replacing characters (especially problems with values needing [] and commas) for print s
Dictionary: replacing characters (especially problems with values needing [] and commas) for print s

Time:10-29

I am aware that there are many posts about replacing characters in Python with dictionaries, however no response here or on any other platform has helped me find a solution.

In a list of US presidents (key) including their year of birth and death (value), to input them into the dictionary the value has to be in the format [1894, 1970], however in my print statement I need these dates to appear with no square brackets and with a dash (-) instead of the comma.

I've tried many things but later found out they don't work for dictionaries, the things I have found which are specific to dictionaries don't work and the only reason I can think of is that both [] and the , have functions within the dictionary and therefore need to be treated differently when I try to remove them from the print statement.

I hope that was not too messy of an explanation, basically I suppose the simple question is "do I need to change my method in doing this because of the fact these characters have important functions in my dictionary?"

Basically, input:

US_Presidents = {
    'George Washington': [1732,1799],
    'Thomas Jefferson': [1743,1826],
    'Benjamin Harrison': [1833,1901],
    'Theodore Roosevelt': [1858,1919]
}

This would be how I would want my output to look like: US_Presidents = { George Washington 1732-1799 Thomas Jefferson 1743-1826 Benjamin Harrison 1833-1901 Theodore Roosevelt 1858-1919

Thanks in advance!

CodePudding user response:

Try this :

my_dict= {"john":["1950","2010"]}
my_dict2={}
for key in my_dict:
  my_dict2[key] = "-".join(my_dict[key])
print(my_dict2)

output:

{'john': '1950-2010'}

CodePudding user response:

Presuming the dict looks like this:

KEY: Tim VALUE: [1900,1990]
KEY: Bob VALUE: [1957,2019]
KEY: Fred VALUE: [2023,2115]

And you want the output to be like "Bob 1957 - 2019" then this code should do the job by treating the value you get as a list, which it is and then indexing said list

people = {
    "Tim" :  [190,1990],
    "Bob" :  [1957,2019],
    "Fred" : [2023,2115]
}
personKey = "Bob" #Change this to who you want to access
personValues = people[personKey]

#This puts all the data together into one string, using ' ' to join strings
result = personKey   "  "   str(personValues[0])   " - "   str(personValues[1])
print(result)

This should output Bob 1957 - 2019

EDIT:

If you would like to loop through, and print out all people, here is code for that.

people = {
    "Tim" :  [190,1990],
    "Bob" :  [1957,2019],
    "Fred" : [2023,2115]
}


#for loop to loop through all the people (key/values) in the list.
#
for key, value in people.items():
    #"key" Is like "personKey"
    #and "value" is like "personValue"
    #This puts all the data together into one string, using ' ' to join strings

    result = str(key)   "  "   str(value[0])   " - "   str(value[1])
    print(result)

And this will output

Tim  190 - 1990
Bob  1957 - 2019
Fred  2023 - 2115

And to make it look perfectly like your example

US_Presidents = {
    'George Washington': [1732,1799],
    'Thomas Jefferson': [1743,1826],
    'Benjamin Harrison': [1833,1901],
    'Theodore Roosevelt': [1858,1919]
}

#Need to define result beforehand, else we get an error
result = ""

#for loop to loop through all the people (key/values) in the list.
#
for key, value in US_Presidents.items():
    #"key" Is like "personKey"
    #and "value" is like "personValue"
                  
    #This puts all the data together into one string, using ' ' to join strings
    #now the result instead gets added to itsself making a longer string
    result = result   str(key)   "  "   str(value[0])   "-"   str(value[1])   "   "

#This will now print the result after all the presidents have been looped through and added to the "results" variable                  
print(result)

#and if you want it to print with the "US_Presidents = {" bit at the front, just add it to the string!
#like this
print("US_Presidents = { "   result   "}")


And the output is:

George Washington  1732-1799   Thomas Jefferson  1743-1826   Benjamin Harrison  1833-1901   Theodore Roosevelt  1858-1919   
US_Presidents = { George Washington  1732-1799   Thomas Jefferson  1743-1826   Benjamin Harrison  1833-1901   Theodore Roosevelt  1858-1919   }
  • Related