Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Lists are mutable, which means that the List can be changed, as well as its children.

Code Block
titlelists
borderStylesolid
titlelists
print([0, 'alpha', 4.5, char('d')])
print List('abcdefghij')
l = List(range(5))
print l
l[2] = 5
print l
l[3] = 'banana'
print l
l.Add(100.1)
print l
l.Remove(1)
print l
for item in l:
    print item
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
[0, alpha, 4.5, d]
[a, b, c, d, e, f, g, h, i, j]
[0, 1, 2, 3, 4]
[0, 1, 5, 3, 4]
[0, 1, 5, 'banana', 4]
[0, 1, 5, 'banana', 4, 100.1]
[0, 5, 'banana', 4, 100.1]
0
5
'banana'
4
100.1

...

Slicing is quite simple, and can be done to strings, Lists, and arrays.
It goes in the form var[start:end]. both start and end are optional, and must be integers, even negative integers.
To just get one child, use the form var[position]. It will return a char for a string, an object for a List, or the specified type for an array.
Slicing counts up from the number 0, so 0 would be the 1st value, 1 would be the 2nd, and so on.

Code Block
titleslicing
borderStylesolid
titleslicing
list = List(range(10))
print list
print list[:5] // first 5
print list[2:5] // starting with 2nd, go up to but not including the 5
print list[5:] // everything past the 5th
print list[:-2] // everything up to the 2nd to last
print list[-4:-2] // starting with the 4th to last, go up to 2nd to last
print list[5] // the 6th
print list[-8] // the 8th from last
print '---'
str = 'abcdefghij'
print str
print str[:5] // first 5
print str[2:5] // starting with 3rd, go up to but not including the 6th
print str[5:] // everything past the 6th
print str[:-2] // everything before the 2nd to last
print str[-4:-2] // starting with the 4th to last, to before the 2nd to last
print str[5] // the 6th
print str[-8] // the 8th from last
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
[2, 3, 4]
[5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7]
[6, 7]
5
2
---
abcdefghij
abcde
cde
fghij
abcdefgh
gh
f
c

...

  1. by using parentheses ()
    • If you have 0 members, it's declared: (,)
    • If you have 1 member, it's declared: (member,)
    • If you have 2 or more members, it's declared: (one, two)
  2. by creating a new array wrapping an IEnumerator, or an List.
  3. by creating a blank array with a specified size: array(type, size)
Code Block
titlearrays
borderStylesolid
titlearrays
print((0, 'alpha', 4.5, char('d')))
print array('abcdefghij')
l = array(range(5))
print l
l[2] = 5
print l
l[3] = 'banana'
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
(0, alpha, 4.5, d)
(a, b, c, d, e, f, g, h, i, j)
(0, 1, 2, 3, 4)
(0, 1, 5, 3, 4)
ERROR: Cannot convert 'System.String' to 'System.Int32'.

...

If you create a List of ints and want to turn it into an array, you have to explicitly state that the List contains ints.

Code Block
borderStylesolid
titlelist to array conversion
borderStylesolid
list = []
for i in range(5):
    list.Add(i)
    print list
a = array(int, list)
for a_s in a:
    print a_s
a[2] += 5
print a
list[2] += 5
print list[2]
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
[0]
[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]
(0, 1, 2, 3, 4)
(0, 1, 7, 3, 4)
ERROR: Operator '+' cannot be used with a left-hand side of type 'System.Object' and a right-hand side of type 'System.Int32'

...

  1. using var as <type>
  2. using var cast <type>
Code Block
borderStylesolid
titlecasting example
borderStylesolid
list = List(range(5))
print list
for item in list:
    print ((item cast int) * 5)
print '---'
for item as int in list:
    print item * item
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
[0, 1, 2, 3, 4]
0
5
10
15
20
---
0
1
4
9
16

...

  1. by using braces {}
  2. by creating a new Hash wrapping an IEnumerator, or an IDictionary.
Code Block
borderStylesolid
titlehash example
borderStylesolid
hash = {'a': 1, 'b': 2, 'monkey': 3, 42: 'the answer'}
print hash['a']
print hash[42]
print '---'
for item in hash:
    print item.Key, '=>', item.Value

# the same hash can be created from a list like this :
ll = [ ('a',1), ('b',2), ('monkey',3), (42, "the answer") ]
hash = Hash(ll)
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
1
the answer
---
a => 1
b => 2
monkey => 3
42 => the answer

...