0

i have a dynamic group: for example we have a list with 20 items in it in this time.... i wanna make this list in 4 individual list with 5 items something like this is in my head..

a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
z = len(a)/4
b = []
for i in range(z)

and after i don't know what i should do.......

i did something like this but it's completely wrong:

for i in range(len(a)):
    for j in range(z):
        b.append(a)

this is my codes:

import maya.cmds as cm
import random as random
myList = cm.ls(sl = True)
random.shuffle(myList)
b = [myList[i:i+5] for i in range(0, len(myList), 5)]
for i in b:
    print str(i)+".v"

the result is:

[u'curve13', u'curve26', u'curve5', u'curve40', u'curve17'].v

but i need:

[u'curve13.v', u'curve26.v', u'curve5.v', u'curve40.v', u'curve17.v'].v
falsetru
  • 357,413
  • 63
  • 732
  • 636
Amin Khormaei
  • 379
  • 3
  • 8
  • 23
  • If you print `i` and `j` you can probably see why that's not working. And perhaps correct it. It might not be pythonic, but a good exercise nonetheless. – keyser Aug 02 '13 at 18:28
  • _"i wanna make this list in 4 individual list with 5 items"_. Please be more specific. Which items go in which lists? Show us what output you expect. – Kevin Aug 02 '13 at 18:32

1 Answers1

0

Use slice operation:

>>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
>>> b = [a[i:i+5] for i in range(0, len(a), 5)]
>>> b
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
>>> b[2][3]
14

UPDATE response to comment

access items

>>> for item in b[2]: print '{}.v'.format(item)
...
11.v
12.v
13.v
14.v
15.v

UPDATE2

>>> a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
>>> n = len(a)
>>> b = [a[n*i/4:n*(i+1)/4] for i in range(4)]
>>> c, d, e, f = b
>>> c
[1, 2, 3, 4, 5]
>>> d
[6, 7, 8, 9, 10]
>>> e
[11, 12, 13, 14, 15]
>>> f
[16, 17, 18, 19, 20]
falsetru
  • 357,413
  • 63
  • 732
  • 636