N
N
Nikita Kuzovkov2020-04-26 16:56:18
Python
Nikita Kuzovkov, 2020-04-26 16:56:18

How to write dictionary processing in VK?

I have a dictionary for a bot like:
hello\hi\hello == Hello\Hello\Hi, how are you?
How to make the bot check for the presence of each value before "==" in the message, and if it is present in it, then give a random answer from the right side

f = open('rad.txt', 'r', encoding='utf-8')
            d = {}
            for line in f:
                d[line[:line.index('==')-1]] = line[3+line.index('=='):].split('\\')
            f.close()

            response = body
            for key in d.keys():
                if key in response.lower():
                    lep=random.choice(d[key])

This is an example that simply takes a random value from the right side. Only one option is allowed on the left, but I need to be able to write several options there (see above) and at the same time he went through all of them for the presence of this text in the message. thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Panuhin, 2020-04-26
@StivenHolland

Here is one option:

import random

f = open('rad.txt', 'r', encoding='utf-8')

d = {}
for line in f:
    keys = line[:line.index('==') - 1].split('\\')
    values = line[3 + line.index('=='):].split('\\')
    for key in keys:
        d[key] = values

f.close()

response = "привет"
for key in d.keys():
    if key in response.lower():
        lep = random.choice(d[key])

print(lep)

  1. Getting all the keys
  2. Get all values
  3. For each key, create an item dequal to an array with all values

If memory consumption is important, then it is better to make, for example, the following structure:
spoiler
[
  [
    ["привет", "хай", "здравствуй"], ["Привет", "Здравствуй", "Привет"]
  ],
  [
    Другая фраза
  ]
]

А затем делать:
For по фразам:
    For по ключам:
        Random по значениям

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question