| 1 | initial version |
First, for n in len(S) won't work, because len(S) is an integer:
TypeError: 'int' object is not iterable
Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.
Next, to create an object for each element of S, you can use list comprehension:
sage: [i for i in S] # or [(stuff depending on i) for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)] # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]
| 2 | No.2 Revision |
First, for n in len(S) won't work, because len(S) is an integer:
TypeError: 'int' object is not iterable
Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.
Next, to create an object for each element of S, you can use list comprehension:
sage: [i for i in S] # or [(stuff depending on i) for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)] # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]
Edit: you can also do this without list comprehension:
K = []
for i in S:
K.append(stuff)
| 3 | No.3 Revision |
First, for n in len(S) won't work, because len(S) is an integer:
TypeError: 'int' object is not iterable
Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.
Next, to create an object for each element of S, you can use list comprehension:
sage: [i for i in S] # or [(stuff depending on i) for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)] # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]
Edit: you can also do this without list comprehension:
K = []
for i in S:
(create stuff)
K.append(stuff)
| 4 | No.4 Revision |
First, for n in len(S) won't work, because len(S) is an integer:
TypeError: 'int' object is not iterable
Instead you could either do for n in S (if you want to access the elements of S) or for n in enumerate(S) if you want to count the elements — in this case n will actually range through pairs (i, elt) where i is an integer and elt is an element of S.
Next, to create an object for each element of S, you can use list comprehension:
sage: [i for i in S] # or [(stuff depending on i) for i in S]
['b', 'a', 'c']
sage: [i for i in enumerate(S)] # or ...
[(0, 'b'), (1, 'a'), (2, 'c')]
Edit: you can also do this without list comprehension:
K = []
for i in S:
(create stuff)
K.append(stuff)
Then K will be a list with one entry for each element of S.
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.