Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
little exercise
#1
hi, i got a list:

my_list = [1,4,8,17,18,24]
i want the output to be :

Output:
1 3 4 9 1 6
i would like to print the result of number in index2 - number in index1 (4-1) , number in index3 - number in index2 (8-4), number in index4 - number in index3 (17-8) ... etc
and there's probably a "trap" with how to get the "1" in the output?
any help?
Reply
#2
what have you tried? :-)
Reply
#3
im trying things starting with :

for i, number in enumerate(my_list):
like:

    for i, number in enumerate(my_list):
        print i, number - number-1
but its obviously not that, i'm newbie
Reply
#4
OK, that is simple but nevertheless working approach.
Do you know how to access list elements by index? (look here)
Then, try to describe in plain language how you will approach  "the trap" :-) - i.e. to print the first element of the output.
Reply
#5
still didnt find,
i only know it has to start with:
for i, number in enumerate(my_list): (exo starts with that, so i must use it)

sorry for double post.
I just tried :
for i, number in enumerate(my_list):
       print my_list[i] - my_list[i-1]]
my output is :

Output:
-44 1 2 2 15 24
but i want it to be :

Output:
1 1 2 2 15 24
Reply
#6
Did you check second part of my post?
(Sep-20-2017, 05:56 PM)buran Wrote: Then, try to describe in plain language how you will approach "the trap" :-) - i.e. to print the first element of the output. Hide/Show
Reply
#7
fixed, thanks!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020