0

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]

  • 2
    Can you give an exemple of what is `cards_18`? Note that [Code Review](http://codereview.stackexchange.com/) is done for this kind of question. – Laurent LAPORTE Oct 28 '16 at 11:16
  • You shouldn't have the cards in individual variables in the first place. Use a single list. – Daniel Roseman Oct 28 '16 at 11:18
  • @DanielRoseman It seems to me that the cards are in a single list, `cards_18`. Or did I misunderstand your comment? – bli Oct 28 '16 at 12:38

6 Answers6

1

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.

Arthur Tacca
  • 8,833
  • 2
  • 31
  • 49
1

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]
Quy Nguyen
  • 124
  • 2
  • 10
1

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)
Anish Gupta
  • 101
  • 1
  • 6
  • Why enumerate ? –  Oct 31 '16 at 14:31
  • @timburnt enumerating is an easy way of accessing the index of the element in the for loop, you could alternatively use `cards_18.index(card)` to get the current index. – Anish Gupta Oct 31 '16 at 17:25
1

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]
smernst
  • 184
  • 1
  • 9
0

You can simplify by using slices:

list_one = cards_18[:2]
list_two = cards_18[2:4]
list_three = cards_18[4:6]
cdarke
  • 42,728
  • 8
  • 80
  • 84
0

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.

bli
  • 7,549
  • 7
  • 48
  • 94