Olismith1
Olismith1

Reputation: 1

Testing if a dictionary key ends in a letter?

tools = {"Wooden_Sword1" : 10, "Bronze_Helmet1 : 20}

I have code written to add items, i'm adding an item like so:

tools[key_to_find] = int(b)

the key_to_find is the tool and the b is the durability and i need to find a way so if i'm adding and Wooden_Sword1 already exists it adds a Wooden_Sword2 instead. This has to work with other items as well

Upvotes: 0

Views: 96

Answers (4)

John Luscombe
John Luscombe

Reputation: 383

You could write a function that determines if a character is a letter:

def is_letter(char):
    return 65 <= ord(char) <= 90 or 97 <= ord(char) <= 122

Then when you are looking at a key in your dictionary, simply:

if is_letter(key[-1]):
    ...

Upvotes: 0

Blckknght
Blckknght

Reputation: 104792

As user3483203 and ShadowRanger commented, it's probably a bad idea to use numbers in your key string as part of the data. Manipulating those numbers will be awkward, and there are better alternatives. For instance, rather than storing a single value for each numbered key, use simple keys and store a list. The index into the list will take the place of the number in the key.

Here's how you could implement it:

tools = {"Wooden_Sword" : [10], "Bronze_Helmet" : [20]}

Add a new wooden sword with durability 10:

tools.setdefault("Wooden_Sword", []).append(10)

Find how many bronze helmets we have:

helmets = tools.get("Bronze_Helmet", [])
print("we have {} helmets".format(len(helmets)))

Find the first bronze helmet with a non-zero durability, and reduce it by 1:

helmets = tools.get("Bronze_Helmet", [])
for i, durability in helmets:
    if durability > 0:
         helmets[i] -= 1
         break
else: # this runs if the break statement was never reached and the loop ran to completion
    take_extra_damage() # or whatever

You could simplify some of this code by using a collections.defaultdict instead of a regular dictionary, but if you learn how to use get and setdefault it's not too hard to get by with the regular dict.

Upvotes: 2

user8866053
user8866053

Reputation:

From what I understand of the question, your keys have 2 parts: "Name" and "ID". The ID is just an integer that starts at 1, so you can initialize a counter for every name:

numOfWoodenSwords = 0

And to add to the array:

numOfWoodenSwords += 1
tools["wodden_sword" + str(numOfWoodenSwords)] = int(b)

If you need to have an unknown amount of tools, I recommend looking at the re module: https://docs.python.org/3/library/re.html.

Or you could iterate over tools.keys to see if the entry exists.

Upvotes: 0

Jongware
Jongware

Reputation: 22478

To ensure a key name is not taken yet, and add a number if it is, create the new name and test. Then increment the number if it is already in your list. Just repeat until none is found.

In code:

def next_name(basename, lookup):
    if basename not in lookup:
        return basename
    number = 1
    while basename+str(number) in lookup:
        number += 1
    return basename+str(number)

While this code does what you ask, you may want to look at other methods. A possible drawback is that there is no association between, say, WoodenShoe1 and WoodenShoe55 – if 'all wooden shoes' need their price increased, you'd have to iterate over all possible names between 1 and 55, just in case these existed at some time.

Upvotes: 0

Related Questions