İngilizce python sorusu yardımcı olabilir misiniz

Merhaba arkadaşlar soru aşağıdaki gibi

Write a function that takes a dictionary, a key and a value and adds the key-value pair according to the following rules:

  • if the key does not exist in the dictionary, then the key-value pair is added normally to the dictionary
  • if the key already exists, and there is only one corresponding value of it
  • then if the new value is different than the existing value, a list of the existing value and the new value is created and stored as the value of the key
  • if the new value is the same as the existing value, then nothing is done
  • if the key already exists, and there is a list of corresponding values
  • if the new value is not contained in this value list, then it is added to the list,
  • otherwise, nothing is done.

The function returns the updated dictionary.
The order of values in value lists must match the order they were inserted.

def multiAdd(dict, key, value):

Merhaba, şöyle bir fonksiyon yazmanız isteniyor:

def multiAdd(dictionary, key, value):
    if key not in dictionary:
        dictionary[key] = value
    else:
        if dictionary[key] == value:
            pass
        else:
            if isinstance(dictionary[key], list):
                if any(values == value for values in dictionary[key]):
                    pass
                else:
                    dictionary[key] = [
                        values for values in dictionary[key]
                    ] + [value]
            else:
                dictionary[key] = [dictionary[key], value]
    return dictionary
1 Beğeni

Teşekkür ediyorum kafam karışmıştı takıldığım noktaları yakaladım sağolun