Using slices error:
list_one = cards_18[0::3] # from index 0 to end, add 3
list_two = cards_18[1::3]
list_three = cards_18[2::3]
Using slices error:
list_one = cards_18[0::3] # from index 0 to end, add 3
list_two = cards_18[1::3]
list_three = cards_18[2::3]
A simple way:
list_one = cards_18[0::3]
list_two = cards_18[1::3]
list_three = cards_18[2::3]
In [0::3]
, the 0
means start at the first element, the missing value in the middle means go right to the end, and the 3
at the end means use every third element.
Edit: slightly neater, but the same basic idea:
splits = 3
lists = [cards_18[i::splits] for i in range(splits)]
list_one, list_two, list_three = lists
The third line is not necessary if you can just work with lists
directly (which will probably end up giving you cleaner code). The first line isn't necessary because you can just substitute the number 3 directly into the two places on the next line that it's used, but using a variable is clearer IMO.
I guess list_one is [0,3,6,9,12,15], list_two is [1,4,7,10,13,16], list_three [2,5,8,11,14,17].
If you don't need to do it row by row:
list_one = cards_18[0::3] # from index 0 to end, add 3
list_two = cards_18[1::3]
list_three = cards_18[2::3]
If I am interpreting your question correctly, you want to deal the cards between the three lists.
Just loop through cards_18
using the modulus operator (%
) to cycle between the lists.
Example:
for i, card in enumerate(cards_18):
if i%3 == 0:
list_one.append(card)
if i%3 == 1:
list_two.append(card)
if i%3 == 2:
list_three.append(card)
I am assuming you want to deal every third card to each column, you could do
for i in range(0,18,3):
list_one.append(cards_18[i])
list_two.append(cards_18[i+1])
list_three.append(cards_18[i+2])
The range will consist of the integers between 0 and 18 (excluding 18 itself) with a step of 3, so 0, 3, 6, ... 15
You could also use slices:
list_one = cards_18[0::3]
list_two = cards_18[1::3]
list_three = cards_18[2::3]
You can simplify by using slices:
list_one = cards_18[:2]
list_two = cards_18[2:4]
list_three = cards_18[4:6]
Does the following do what you want?
list_one = [card for (i, card) in enumerate(cards_18) if i % 3 == 0]
list_two = [card for (i, card) in enumerate(cards_18) if i % 3 == 1]
list_three = [card for (i, card) in enumerate(cards_18) if i % 3 == 2]
This uses the fact that the result of the "modulo 3" operation will cycle between 0, 1 and 2 when applied to successive integers.
enumerate
will generate the indices of the cards in the list along with the cards themselves.
List comprehensions enable this kind of short notation.