-2

I have an array with the following ids:values.

For example:

[3] 'hello'
[24] 'tell me a joke'
[34] 'im bored'
[42] 'what time is it'
[56] 'how are you'
[69] 'what are you doing'

I also have some user input that could contain multiple values of the array, for example:

'hello and good evening. how are you. im bored and need some entertainment. please tell me a joke.'

I am able to find all the matches, but not in the right order.

The expected result would have the same order as they appear in the input string

E.g.

"hello", "how are you", "im bored", "tell me a joke"

miquelvir
  • 1,748
  • 1
  • 7
  • 21
Jabawaky
  • 3
  • 1
  • 1
    Please follow the [tour] and read [ask]. Stack Overflow is not a code-writing service, and we also cannot just provide "any information or links". You need to make an attempt at solving the problem, and ask a specific question related to your attempt. – Karl Knechtel Mar 27 '21 at 22:02
  • Does this answer your question? [Check if multiple strings exist in another string](https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string) – miquelvir Mar 27 '21 at 22:11

1 Answers1

0

Iterate over the dictionary and use the in operator to see if each phrase is in the input. If it is, use the index method to find out where so that you can sort the results by that index. Then you can remove the index from the final result so you just have tuples of the ids and values.

>>> user_input = 'hello and good evening. how are you. im bored and need some entertainment. please tell me a joke.'
>>> data = {
...     3: 'hello',
...     24: 'tell me a joke',
...     34: 'im bored',
...     42: 'what time is it',
...     56: 'how are you',
...     69: 'what are you doing',
... }
>>>
>>> [(k, v) for _, k, v in sorted(
...     (user_input.index(v), k, v)
...     for k, v in data.items()
...     if v in user_input
... )]
[(3, 'hello'), (56, 'how are you'), (34, 'im bored'), (24, 'tell me a joke')]

Or, since you already have the database that tells you the string for each id, you could just compute the list of ids like this:

>>> [k for _, k in sorted(
...     (user_input.index(v), k)
...     for k, v in data.items()
...     if v in user_input
... )]
[3, 56, 34, 24]

and then of course you can take that list and do things like:

>>> " ".join(data[i] for i in [3, 56, 34, 24])
'hello how are you im bored tell me a joke'

etc

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • How do you know that there is a dictionary? – mkrieger1 Mar 27 '21 at 22:20
  • A mapping of id: value strongly implies a dictionary of some kind. If there wasn't one, step zero would be to make one. :) – Samwise Mar 27 '21 at 22:23
  • Thank you so much Samwise. Works perfectly. :) I had to check how to convert the sql result into a dictionary and realised, that it dont accept duplicate keys. I have duplicate keys in the data array, because these are category ids. I changed my code that it fits to your solution. Thank you very, very much! :) – Jabawaky Mar 28 '21 at 01:14