Reputation: 9
Basically I have some values in a list that are integers. I want to give each value a name that is a string within the list so that it shows what the name of the value i have taken out is.
I have already tried putting the name in quotation marks but that does that seem to work
eliminate = [Couple1,Couple2,Couple3,Couple4,Couple5,Couple6]
length = len(eliminate)
FSmallest = eliminate[0]
SSmallest = eliminate[1]
print(eliminate)
gone = print(FSmallest," and ",SSmallest," have been eliminated!")
When i run this code. It gives me the numbers of each couple and says which number i have eliminated. I want it to tell me which couple i have eliminated. For example, "Couple1: 30 and Couple2: 20 have been eliminated!" but it does not let me add the text and the number while it is eliminating.
Upvotes: 0
Views: 125
Reputation: 693
You have 3 options (atleast).
1- Class implementation :
#Define the Couple class
class Couple:
name = ""
value = 0
# The Constructor
def __init__(self, the_name, the_value):
self.name = the_name
self.value = the_value
# In this method we handle the output , when object is being printed.
def __str__(self):
return self.name + " : " + str(self.value)
#Create some objects
couple_1 = Couple('couple1', 30)
couple_2 = Couple('couple2', 20)
couple_3 = Couple('couple3', 10)
couple_4 = Couple('couple4', 40)
couple_5 = Couple('couple5', 35)
#Put them in a list
eliminate = [couple_1, couple_2, couple_3, couple_4, couple_5]
length = len(eliminate)
FSmallest = eliminate[0]
SSmallest = eliminate[1]
print(FSmallest, ' and ', SSmallest, ' have been eliminated!')
Now your output will be like this :
couple1 : 30 and couple2 : 20 have been eliminated!
2- List of Dictionaries Implementation:
eliminate = [
{'name':'couple1', 'value':30},
{'name':'couple2', 'value':20},
{'name':'couple3', 'value':10},
{'name':'couple4', 'value':40},
{'name':'couple5', 'value':35},
]
length = len(eliminate)
FSmallest = eliminate[0]
SSmallest = eliminate[1]
print(FSmallest['name'], ' : ', FSmallest['value'], ' and ',\
SSmallest['name'], ' : ', SSmallest['value'], ' have been eliminated!')
Which will result the same.
3- Named Tuples implementation:
namedtuple is kind of a combination of class and dictionary. we have to import it from collections module.
from collections import namedtuple
# Define a new namedtuple with 2 fields , name and value
# There are several ways to declare fields.
# Couple = namedtuple('Couple', ['name', 'value'])
# Couple = namedtuple('Couple', "name, value")
# Couple = namedtuple('Couple', "name value")
Couple = namedtuple('Couple', 'name value')
# Create some instances of our tuple
# We can declare the values of fields in 2 way
# First way is :
couple_1 = Couple(name='couple1', value=30)
# And the second way :
couple_2 = Couple('couple2', 20)
couple_3 = Couple('couple3', 10)
couple_4 = Couple('couple4', 40)
couple_5 = Couple('couple5', 35)
eliminate = [couple_1, couple_2, couple_3, couple_4, couple_5]
FSmallest = eliminate[0]
FSmallest = eliminate[1]
print(FSmallest.name, ' : ', FSmallest.value, ' and ',\
SSmallest.name, ' : ', SSmallest.value, ' have been eliminated!')
Upvotes: 2