after the for loop arrays gets the values from tmp1. How can i store values for later use?
def getCardRank(self, arrays, straight = False):
print arrays
tmp1 = arrays[:]
tmp2 = {i: arrays[i] for i in range(0, len(arrays))}
print tmp1
for array in tmp1:
for card in array:
i = array.index(card)
mod = card % 13
if (mod == 0 and False != straight):
card = 0 if sum(array) == 27 else 13
else:
card = 13 if mod == 0 else mod
array[i] = card
print tmp1
print tmp2
that gives me:
[[44, 43, 42, 41, 40], [37, 36, 35, 34, 33], [17, 16, 15, 14, 26]]
[[44, 43, 42, 41, 40], [37, 36, 35, 34, 33], [17, 16, 15, 14, 26]]
{0: [44, 43, 42, 41, 40], 1: [37, 36, 35, 34, 33], 2: [17, 16, 15, 14, 26]}
[[5, 4, 3, 2, 1], [11, 10, 9, 8, 7], [4, 3, 2, 1, 13]]
{0: [5, 4, 3, 2, 1], 1: [11, 10, 9, 8, 7], 2: [4, 3, 2, 1, 13]}
I’m a little unclear on the question, but I’ll assume that you want
print(tmp1)to alwaysprint [44, 43, 42]...etc instead of[5, 4, 3].The problem with the assignment
tmp1 = arrays[:]is that if you create a copy of a list to iterate over using slice notation, the original list is changed.In order to store the original array in tmp1, a rather inefficient solution would be to create a tuple
tmp1and iterate over arrays appending each element totmp1. Another would be, as previously suggested, to pickle your data structures.However, it looks like the real solution is to change how you’re thinking about storing
tmp1– consider that if you need to retain the original arrays after callinggetCardRank, you probably want to think of it as part of the object thatselfrefers to in the function’s signature. Therefore, you should store the original copy of arrays in a member variable (self.foo = [array for array in arrays]).As a side note, it makes your code somewhat more difficult to read when you use inline conditionals.