1

I have a list

list = ["Hat({})A", "Tie({})B", "Disk({})C"]

And another list:

inventory = ["Hat(1)A", "Hat(43)A", "Shirt(23)E", "Tie(2)B"]

In python, I want to iterate inventory for each item, if it contains the value except for the number, return true. If not, return false.

"Hat(1)A", "Hat(43)A", "Tie(2)B" = TRUE
"Shirt(23)" = FALSE

Anyone know how this can be done? I've tried everything and I'm stuck :(

1
  • where does list = ["Hat({})A", "Tie({})B", "Disk({})C"] come from? what about Tie(2)C would that be true or false? Nov 10, 2022 at 5:34

1 Answer 1

0

First you remove the curly braces from the list.

l = ["Hat({})A", "Tie({})B", "Disk({})C"]

new_list = [''.join(x.split('{}')) for x in l]

# ['Hat()A', 'Tie()B', 'Disk()C']

Your inventory is:

inventory = ["Hat(1)A", "Hat(43)A", "Shirt(23)E", "Tie(2)B"]

To find the numbers in inventory use re:

indices = re.findall(r'\d+', ''.join(inventory))

# ['1', '43', '23', '2']

Now, you have the corresponding numbers which were present in inventory in indices

new_inventory = [''.join(inventory[x].split(indices[x])) for x in range(len(indices))]

# ['Hat()A', 'Hat()A', 'Shirt()E', 'Tie()B']

Finally, you can iterate over new_inventory to find if they are present in new_list:

[x in new_list for x in new_inventory]

# [True, True, False, True]

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.