Just a little background: I'm making a program where a user inputs a skeleton text, two numbers (lower and upper limit), and a list of words. The outputs are a series of modifications on the skeleton text.
Sample inputs:
text = "Player # likes @." (replace # with inputted integers and @ with words in list)
lower = 1
upper = 3
list = "apples, bananas, oranges"
The user can choose to iterate over numbers first:
Player 1 likes apples.
Player 2 likes apples.
Player 3 likes apples.
Or words first:
Player 1 likes apples.
Player 1 likes bananas.
Player 1 likes oranges.
I chose to split these two methods of outputs by creating a different type of dictionary based on either number keys (integers inputted by the user) or word keys (from words in the inputted list) and then later iterating over the values in the dictionary.
Here are the two types of dictionary creation:
def numkey(dict): # {1: ['Player 1 likes apples', 'Player 1 likes...' ] }
text, lower, upper, list = input_sort(dict)
d = {}
for num in range(lower,upper+1):
l = []
for i in list:
l.append(text.replace('#', str(num)).replace('@', i))
d[num] = l
return d
def wordkey(dict): # {'apples': ['Player 1 likes apples', 'Player 2 likes apples'..] }
text, lower, upper, list = input_sort(dict)
d = {}
for i in list:
l = []
for num in range(lower,upper+1):
l.append(text.replace('#', str(num)).replace('@', i))
d[i] = l
return d
It's fine that I have two separate functions for creating different types of dictionaries but I see a lot of repetition between the two. Is there any way I could make one dictionary function and pass in different values to it that would change the order of the nested for loops to create the specific {key : value} pairs I'm looking for?
I'm not sure how this would be done. Is there anything related to functional programming or other paradigms that might help with this? The question is a little abstract and more stylistic/design-oriented than anything.