Discussion:
[Tutor] Updating index of a list
Andrea Nguy
2015-10-08 19:40:38 UTC
Permalink
Hi there,

I’m trying to learn some Python programming on my own. 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.

However, what I am trying to do take the indexes of thelist[0][1] and theist[0][3] and times them by a float number - returning the list with all of the values multiplied according to the float.

Is this possible? I am trying to iterate that throughout the entire list or would it be easier if I tried to change the list of the list into a dictionary for efficiency?

Thanks!

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.
Emile van Sebille
2015-10-08 21:29:33 UTC
Permalink
Post by Andrea Nguy
Hi there,
[['1', ' james', ' 1', ' 90'],
['2', ' alice', ' 1', ' 95'],
['5', ' jorgen', ' 1', ' 99’]] (it continues) from a text file.
Post by Andrea Nguy
However, what I am trying to do take the indexes of
thelist[0][1]
which has a value of 'james' (being index 1 of list 0)
Post by Andrea Nguy
and theist[0][3]
which has a value of ' 90'

that should get you going.

Emile
Post by Andrea Nguy
and times them by a float number - returning the list with all of the values multiplied according to the float.
Is this possible? I am trying to iterate that throughout the entire list or would it be easier if I tried to change the list of the list into a dictionary for efficiency?
Thanks!
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinf
Alan Gauld
2015-10-08 22:17:58 UTC
Permalink
[['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?
Post by Andrea Nguy
However, what I am trying to do take the indexes of thelist[0][1]
and thelist[0][3] and times them by a float number
- returning the list with all of the values multiplied according to
the float.
Post by Andrea Nguy
Is this possible?
Yes of course.

just use a for loop like this:

for sublist in thelist:
sublist[0] *= someFloat
sublist[3] *= someFloat

Which changes the values in place.

Or a little more advanced you can use a list comprehension:

newlist = [[sub[0]*someFloat, sub[1],[sub[2],sub[3]*someFloat]
for sub in thelist]

Which will create a new list with the values you desire
leaving the original list unchanged.
Post by Andrea Nguy
would it be easier if I tried to change the list of the
list into a dictionary for efficiency?
While changing data type is often the right thing to do to
make the code clearer, thinking about efficiency before
you know you have a problem is nearly always the wrong
thing to do.
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription op
Alan Gauld
2015-10-09 00:38:29 UTC
Permalink
Post by Andrea Nguy
[['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())
Post by Andrea Nguy
Post by Andrea Nguy
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)
Post by Andrea Nguy
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/lis
Steven D'Aprano
2015-10-08 22:37:00 UTC
Permalink
Hi Andrea, and welcome.

My responses inline, below.
Post by Andrea Nguy
Hi there,
I’m trying to learn some Python programming on my own. 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.
Okay, I assume you know how to read the data from the text file and
build the list of lists. If not, let us know. I'll also assume that the
leading spaces in some of the items are deliberate.

What version of Python are you using? That may make a difference to the
answers.

So let's just say we have:

thelist = [['1', ' james', ' 1', ' 90'],
['2', ' alice', ' 1', ' 95'],
['5', ' jorgen', ' 1', ' 99’],
]
Post by Andrea Nguy
However, what I am trying to do take the indexes of thelist[0][1] and
theist[0][3] and times them by a float number - returning the list
with all of the values multiplied according to the float.
I'm not really sure I understand what you are trying to do here. If I
take you literally, you want something like this:

- take thelist[0][1] # returns ' james'
- extract the indexes of that string # returns [0, 1, 2, 3, 4, 5]
- take thelist[0][3] # returns ' 90'
- extract the indexes of that string # returns [0, 1, 2]
- concatenate with the first list # returns [0, 1, 2, 3, 4, 5, 0, 1, 2]
- multiply by some mystery float (say, 1.5)
# returns [0.0, 1.5, 3.0, 4.5, 6.0, 7.5, 0.0, 1.5, 3.0]


This is certainly possible, but I'm not sure that it is what you want.
Can you confirm that it is, or explain a little better what you want?
If possible, show your expected output.

But, for the record, here is one way to do the above:

results = []
for i in (1, 3):
word = thelist[0][i]
results.extend(range(len(word)))

for i, n in enumerate(results):
results[i] = n*1.5

print(results)


You may or may not understand all that code. If you don't, please say so
and we'll explain.
Post by Andrea Nguy
Is this possible? I am trying to iterate that throughout the entire
list or would it be easier if I tried to change the list of the list
into a dictionary for efficiency?
If you converted the list-of-lists into a dictionary, what would it look
like?


P.S. please keep your replies on the list, so that others may help, or
learn from your questions.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
h
Anshu Kumar
2015-10-09 01:59:44 UTC
Permalink
Hi Andrea,

You can use lambda or list comprehension to solve your problem in one step.

I am more comfortable with lambda so will explain that way.


Please follow below lines of code.

andreea_list = [['1', ' james', ' 1', ' 90'], ['2', ' alice', ' 1', '
95'], ['5', ' jorgen', ' 1', '99']]
andrea_float_list = map(lambda x:float(x[0]),andreea_list)
print andrea_float_list


map is function which takes two arguments first is an anonymous function
called lamda which operates on each of the list element one at a time and
the other is list it self. you can read about lambdas here
http://www.python-course.eu/lambda.php

Thanks ,
Anshu
Post by Andrea Nguy
Hi there,
I’m trying to learn some Python programming on my own. 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.
However, what I am trying to do take the indexes of thelist[0][1] and
theist[0][3] and times them by a float number - returning the list with all
of the values multiplied according to the float.
Is this possible? I am trying to iterate that throughout the entire list
or would it be easier if I tried to change the list of the list into a
dictionary for efficiency?
Thanks!
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options

Loading...