Lists(Arrays)


Arrays (Lists)

First of all, bear in mind that Python is calling the thing that I knew as an 'array' in MEL, a 'list'--so if you go to Python.org and look for help on how to deal with lists (not arrays) it will be somewhat less initially confusing.

How can I populate an array(list) in Python?

Use myArray.insert(index, element you want in the index)

Example:

alph=['A','B','C','D','E']

n=4

jt = []

for i in range(n):

a = mc.joint(p=(0,1,0), r=True, name= alph[i] + 'JT')

jt.insert(i, a)

print jt[1]

Or you can use this:

list.append(x) Add an item to the end of the list;

equivalent to a[len(a):] = [x].

alpha = ['A', 'B', 'C', 'D']

alpha.append('sunshine')

print alpha

['A', 'B', 'C', 'D', 'sunshine']

Removing item from an array (a list)

list.remove(x)

Adding one array(list) to another

list.extend(anotherList)

How can I reorder elements in an array (list)?

To reverse them:

members = ['a','b','c']

uprRange = len(members)

arrayNew =[]

for i in range(uprRange):

a= members[uprRange-i-1]

arrayNew.insert(i, a)

There is also array.reverse() for which you need to import the array class.

import array

bob = array.array('c', 'a string')

bob.reverse()

print bob

#array('c', 'gnirts a')

OR--screw the array class, just reverse your 'list' with .reverse(), and you don't even have to worry about the string:

alpha = ['fooA', 'fooB', 'fooC', 'fooD']

alpha.reverse()

print alpha

['fooD', 'fooC', 'fooB', 'fooA']

Also sorted(myArray, reverse=True), for numerical arrays. And googling that will lead to further insight on how to reorder indices.

Make all array elements into one string?

That's all you need, but for future reference

myCapitalArray=[R,U,D,I].

''.join(myCapitalArray)

The '' are two single quotes, which creates an empty string, which joins

the elements of the array. (Dado)

Split a string into a List using Regex

import re

string = 'a b c'

reObj = re.split('(\\s+)', string)

L = [string for string in reObj if [ char for char in string if char != ' ']]

print L

(from 3dDevArtist)

Break up Arrays (actual arrays) Inside a List?

jtOri = mc.getAttr(tempOriJt + '.jointOrient')

myArray = jtOri[0]

print myArray[0]

might be a better way..

myList = [(0,1,2), (3,4,5)]

val = list(myList[0])

#val is [0,1,2], val[0] is 0.

Return Just One Element of Your Array

fkHookup = mc.listConnections(getMeta + '.fkCtrlHookup')[0]

instead of writing:

getHookup = mc.listConnections(blah)

whatIReallyWant = getHookup[0]

Find out if your variable matches a List member

def tickerNumber(num):

numList = ['1', '2', '3', '4', '5']

if num in numList:

print 'yess!!!!'

else:

print 'nooooooo!'

tickerNumber('2')

Empty a List

There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a list or clear the entire list (which we did earlier by assignment of an empty list to the slice). For example:

>

>> a = [-1, 1, 66.25, 333, 333, 1234.5]>>> del a[0]>>> a[1, 66.25, 333, 333, 1234.5]>>> del a[2:4]>>> a[1, 66.25, 1234.5]>>> del a[:]>>> a[]

(hot!)

del can also be used to delete entire variables:

>>> del a

Referencing the name a hereafter is an error (at least until another value is assigned to it). We’ll find other uses for del later.

(this is all from the python.org website..)

Break a tuple into a list?

list( (1, 2, 3) ) returns

[1, 2, 3].

Bear in mind that if your tuple is already nested in a list, you might have to make arrangements for that.

I.e.,

myList = [(0,1,2), (3,4,5)]

val = list(myList[0])

#val is [0,1,2], val[0] is 0.

Find the highest value in a list of integers?

just use max()

myList = [20, 50, 178, 2]

max(myList) will give you 178!

Find the lowest value in a list of integers?

min()

Find size of the list?

len(myList)

Find index of a list member

list.index(x)

Return the index in the list of the first item whose value is x. It is an error if there is no such item.

(from python.org, like so much else here..)

Remove Stuff In One List From Another List

Python has a language feature called List Comprehensions that is perfectly suited to making this sort of thing extremely easy. The following statement does exactly what you want and stores the result in l3:

l3 = [x for x in l1 if x not in l2]

l3 will contain [1, 6].

(from stackOverflow)


Enumerate

Unlike mel, Python lets you easily make enumerated arrays

L = ['a', 'b', 'c']

results = [ (index, object) for index, object in enumerate(L) ]

print results

(from 3dDevArtist)