How to check if input is a key in dictionary even partially? - python-3.x

Lets say you have a dictionary that has a grocery item (key) and its price(value). We ask the user for input (grocery item) and they type in whatever. They dont know what item is in the dictionary or not. Im trying to specify that even if the input contains 1 word of the item in the dictionary, to use that as the key and get the price.
So if the official key in the dictionary was "Milk Gallon", and the user types in "milk", how can i make them reference the same thing? If they type milk i want the output to be the value of "Milk gallon"?

you can use if in
you can try this:-
store = {"milk gallon":100,"tomato":50,"fish":190}
user = input("Whatever you want-->")
for i in store:
if user.lower() in i:
user = i
print(user)

Related

Terraform variable user inputs long key value pair

I am using a 3rd party app called Morpheus (not a fan of it but its enforced)
I built some modules which are easy enough to request the end user selecting a "Catalog item" to be provisioned and asked for name, and drop down list with ex: instance size, etc.
My issue is thou, I need the user to enter in 1 long string of key value pair to be added to an attribute.
for example
parameters = [var.params]
and the end user inputs in the text field
"example1": "value1", "example2": "value2"
but of course this does not work. I tried {"example1": "value1", "example2": "value2"} and
parameters = [jsondecode(var.params)]
also does not work. Stuck on what / how to request end user to enter in the values in a specific format. The txt box is 1 liner btw :(, hence why im not a fan of morpheus but whatev's

How can I make a demand command loop?

I have a command named demand and need to make a limit to amounts per user. There are roles in the server named "team_role" and then there's also "2 demands", "1 demand", and "0 demands". After the user demands, I need it to role them down a demand until eventually, they hit 0. Once they hit 0, it should send them a message saying they can't demand. Here's my code for one of the teams (the Dallas Cowboys).
#bot.command(aliases=["<:DallasCowboys:788796627161710592>"])
#commands.has_any_role("Dallas Cowboys")
async def t(ctx):
guild = bot.get_guild(766292887914151949)
role_name = discord.utils.get(guild.roles, name='Free Agent')
role = discord.utils.get(guild.roles, name='Dallas Cowboys')
embed = discord.Embed()
embed.add_field(name="<a:CheckMark:768095274949935146> Successful Demand", value=f"{ctx.author.mention} has demanded from the <:DallasCowboys:788796627161710592>")
await ctx.send(embed=embed)
await ctx.author.add_roles(role_name)
await ctx.author.remove_roles(role)
Since you said I can just provide the logic:
Create a means of persistently storing the data, whether that be
json file or a database.
a) If you choose the json file, the file will consist of key value
pairs of user ids to number of demands remaining. For example, the
file might look something like this:
{ "UserDemandsRemaining" : { 12345 : 2, 45678: 1, 09876: 0 } }
b) If you choose to use to use a SQL database, create a table called
UserDemandsRemaining with two fields: UserId and
DemandsRemaining. The primary key would be UserId.
Each time a user makes a demand, you will check how many remaining
demands the user has.
If there is no matching UserId stored:
a) If you chose to use a json file, and there is no matching key in the json value associated with the UserDemandsRemaining key, then create a key-value pair with the key as the UserId that made the demand, and the value as 3.
b) If you chose to use a SQL db, and there is no matching UserId matching the UserId that made the demand in the UserDemandsRemaining table of the database, then insert a row into the table containing the UserId that made the demand and a DemandsRemaining value of 3.
Since the user isn't stored, we know that they have not yet made any demands. Execute the demand. Then decrement the `DemandsRemaining` by 1.
Else if there is a matching UserId:
if DemandsRemaining > 0:
#Execute the command. Decrement `DemandsRemaining` by 1.
else:
#Notify the user that they have no more remaining demands.
In order to reset the number of demands each person has remaining,
just set the value pair associated with the users in the json file
or the DemandsRemaining in the table to 3 for everyone.

Adding a dictionary inside a dictionary

I have a dictionary which looks like this:
store = {}
And I have a bunch of data in another dictionary that looks like this:
items = {"hardware_items":23, "fruit_items":5, "fish_items": 23}
How can I put the items dictionary inside the store dict so I can achieve the following result?
store = {"hardware_items":23, "fruit_items":5, "fish_items": 23}
Thank you
Use the update method:
store.update(items)
This will add everything in items to store; beware though that if store already has existing keys with those names they will be overwritten.
If store dict is empty you can use the copy() method :
store = items.copy()
You can use store = items but it works as a reference (then modifying store will modify items)
Else, you can use a for loop :
for keys in items:
store[keys] = items[keys]
It will overwrite value if a key is already declared.
The output you want would appear to be a copy of the "items" dictionary in "store". You can do this, basically, in two ways.
1. Simple copy
You can write
store = items
and the output will be what you asked for.
Changing one of the two dictionaries, however, will also change the other.
2. Deep Copy
One of the ways to do a deep copy is:
store=copy.deepcopy(items)
In this case you will have two dictionaries with the same content, but they will be independent and you can edit them separately. Let me show an example:
import copy
store = {}
items = {"hardware_items":23, "fruit_items":5, "fish_items": 23}
#Print
print("Before operations")
print("ITEMS> "+str(items))
print("STORE> "+str(store))
store=copy.deepcopy(items)
#Print
print("After deep copy")
print("ITEMS> "+str(items))
print("STORE> "+str(store))
items["hardware_items"]=3
#Print
print("let's modify the first key value")
print("ITEMS> "+str(items))
print("STORE> "+str(store))
Your output will be:

dict comprehension with list as value

I have a program that prompts the user for three pieces of input:
Student No.
Name
Age
I then populate a list with this info. This currently works fine.
I was wanting to change the list construct to a dictionary that has the Student No. as the key and a list with the Name and Age as values. I also want the list in case I decide to enlarge the number of inputs in the future.
I was wondering if I could use a dict comprehension but I can't find any examples of using a dict comp being used to create a dict with a list as the value.
The resulting dictionary would look like:
st_dict = {101: [Mark, 54],
102: [Bob, 49]
}
Is it possible, using a dict comp, to create this structure?
Basically I am wanting to do this:
st_dict = {Student_no: [Name, Age] per student entered}

How do you query a CouchDB view that emits complex keys?

Given a CouchDB view that emits keys of the following format:
[ "part1", { "property": "part2" } ]
How can you find all documents with a given value for part1?
If part2 was a simple string rather than an object startkey=["part1"]&endkey=["part1",{}] would work. The CouchDB docs state the following:
The query startkey=["foo"]&endkey=["foo",{}] will match most array keys with "foo" in the first element, such as ["foo","bar"] and ["foo",["bar","baz"]]. However it will not match ["foo",{"an":"object"}]
Unfortunately, the documentation doesn't offer any suggestion on how to deal with such keys.
The second element of your endkey value needs to be an object that collates after any possible value of the second element of your key. Objects are compared by property-by-property (for example, {"a":1} < {"a":2} < {"b":1}) so the best way to do this is to set the first property name in your endkey to a very large value:
startkey=["part1"]&endkey=["part1", { "\uFFF0": false }]
The property name of \uFFF0 should collate after any other property names in the second key element, and even works when the second element is an empty object or has more than one property.

Resources