2

For example in python:

employees[x][i] = float(employees[x][i]) 
2
  • employees[x][i] represents a value,, this syntax will just convert your value to float and store it back... Commented Nov 28, 2012 at 4:48
  • @dallen: It's hard to search "multi-dimensional array/list syntax" when you don't know it's about multi-dimensional array/lists. Commented Nov 28, 2012 at 4:52

4 Answers 4

3

Like most languages, it refers to an element in a multidimensional list:

l = [[0,1,2,3], [1,1,1,1]] l[1] == [0,1,2,3] l[1][2] == 2 
Sign up to request clarification or add additional context in comments.

Comments

2

The two brackets mean you're accessing an element in a list of lists (or dictionaries)

So in this example, it might look something like this

In [17]: employees = {'joe': ['100', 0], 'sue': ['200', 0]} In [18]: x = 'joe' In [19]: i = 0 In [20]: employees[x][i] Out[20]: '100' 

3 Comments

so if i=3, and x=4, you are accessing the fourth element in the third element,which is also a list, in the list employees?
@usmcs: Note that dictionary vs. list is determined by the type of the employees or employees[x] object, not by whether the thing inside the brackets is a string or an integer. Integers can be dictionary keys, but strings cannot be array indexes.
@GregHewgill Yes, that was a sloppy statement on my part, thanks for the clarification. ShaltNot: I think you have it backwards. In this example, x=4 would be the 4th element of employees and i would be the 3rd element of employees[x]
2

I put in extra parens to show how this is evaluated

(employees[x])[I] = float((employees[x])[i]) 

and an example

>>> foo = dict(name="Foo", salary=10.00) >>> bar = dict(name="Bar", salary=12.00) >>> employees = dict(foo=foo, bar=bar) >>> employees {'foo': {'salary': 10.0, 'name': 'Foo'}, 'bar': {'salary': 12.0, 'name': 'Bar'}} >>> employees['foo']['name'] 'Foo' >>> employees['bar']['salary'] 12.0 

employees could also be a list (or any other kind of container)

>>> employees = [foo, bar] >>> employees [{'salary': 10.0, 'name': 'Foo'}, {'salary': 12.0, 'name': 'Bar'}] >>> employees[0]['name'] 'Foo' >>> employees[1]['salary'] 12.0 

Comments

2

Syntax meaning of [] in python:

In python, [] operator is used for at least three purposes (maybe incomplete):

  1. define in literal an array, like xx = [0,1,2,3]
  2. array element indexing, like x1 = xx[1], which requires index be integer or evaluated to a integer
  3. dictionary member retrieval, like s = person['firstname'] // person = {'firstname':'san', 'lastname':'zhang'}, in this case, the index can be anything that a dict tag can be

Thing goes complex when embedding [] in [] or [] next to [], see examples below:

matrix = [[0,1],[2,3]] e01 = matrix[0][1] people = [{'fname':'san','lname':'zhang'}, {'fname':'si', 'lname':'li'}] last1 = people[1]['lname'] 

[[]] and [][] are reciprocal to each other.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.