[Tutor] Updating index of a list
Alan Gauld
alan.gauld at btinternet.com
Fri Oct 9 02:38:29 CEST 2015
On 08/10/15 23:17, Alan Gauld wrote:
> On 08/10/15 20:40, Andrea Nguy wrote:
>
>> What’s happening is that I am trying to take a list of a list as such:
>
> [['1', ' james', ' 1', ' 90'],
> ['2', ' alice', ' 1', ' 95'],
> ['5', ' jorgen', ' 1', ' 99’]] (it continues) from a text file.
>
> When you say from a text file, do you mean you are reading the
> values in and converting them to the appropriate types? Or are
> you just adding them directly to the list? In which case they
> will all be strings?
Its late so I'm not reading clearly enough. They are all strings.
So I suspect before multiplying by a float your first job is to convert
the numbers to floats(or ints) using float() (or int())
>> However, what I am trying to do take the indexes of thelist[0][1]
And I didn't notice this was [0,1] (' james') but read it
as [1,0] (2)
> just use a for loop like this:
>
> for sublist in thelist:
> sublist[0] *= someFloat
> sublist[3] *= someFloat
>
> Which changes the values in place.
Provided its the numbers you want and you convert
them to float/int first.
Sorry for my inattention,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list