W
W
whoareyoutofuckinglecture2018-10-05 20:47:40
Python
whoareyoutofuckinglecture, 2018-10-05 20:47:40

Problem with adding elements to a dictionary using a for loop. What can be done?

Hello!
There is a code:

class Move(object):
    """Описывает ход игроков и сопутствующие методы"""

    @staticmethod
    def who(trump, my_hand, comp_hand):
        trumps = {'MY': [], 'COMP': []}
        for i in my_hand.cards:
            if trump in i:
                trumps['MY'] += i
        for j in comp_hand.cards:
            if trump in j:
                trumps['COMP'] += j
        print(trumps)

The lists my_hand and comp_hand contain values ​​like: ['10h', 'Ks', 'Ah', '9d', '10s', 'Kh']
Here I need to add values ​​for the keys "MY" and "COMP" to the trumps dictionary " if the condition is met. As a value for the dictionary keys, I need to add a whole element, it should look something like this: However, values ​​\u200b\u200bare added to the dictionary in parts, it looks like this:
{ 'MY' : ['10h', 'Кs', 'Ah', '9d', '10s', 'Кh'] }
{'MY': ['В', 's'], 'COMP': ['К', 's', '1', '0', 's']}

It turns out that at the time of adding, for some reason, I am also iterating over the element .. Can you please tell me
why this happens and what can be done so that the values ​​are added to the dictionary as a whole?
Thank you!
UPD.: .cards is a list containing string elements. For example: ['10h', 'Ks', 'Ah', '9d', '10s', 'Kh']

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-10-05
@whoareyoutofuckinglecture

trumps['MY'].append(i)

A
ArtiomK, 2018-10-05
@ArtiomK

alternatively, you can then convert from a sheet to a string and reassign the value to the dictionary key. What are my_hand.cards and comp_hand.cards, are they by any chance strings? Plus a decorator, what does its code do?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question