Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 List vs index:
#1
Hello all,

i'm trying to understand what should i change on my code so that i can use any words / sentence on the string:

Given a string, display only those characters which are present at an even index number.

1. My code:

Str='pynative'
a=list(Str)
print(a)
for letter in a:
    index=a.index(letter)
    if index % 2==0:
        print('index=',index,'letter=', letter)
2. The result:

Output:
['p', 'y', 'n', 'a', 't', 'i', 'v', 'e'] index= 0 letter= p index= 2 letter= n index= 4 letter= t index= 6 letter= v
3. What to do to use any word and get the correct index position?

result above is ok but not suitable for example if i have a string='Hello World'.
the code does not recognize that each l occupies a different index position.

thanks in advance
frederico
buran wrote Jul-01-2020, 08:21 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
Look up the enumerate function.

Also, you don't need to make a list out of the string - strings are already sequences, so can be indexed the same way as lists.
Quote
#3
What do you want to do? I don't mean "How are you going to program", but "What do you want to do?" Can you write a concise description of what you want your program to do? That is the first step in writing a program, and that description should be the first thing you mention when asking questions about your program.

I am writing a program that prints every other letter in a string starting with the first letter (index 0). If the string is "Hello World" the output should be:
index = 0 letter = H
index = 2 letter = l
index = 4 letter = o
index = 6 letter = W
index = 8 letter = r
index = 10 letter = d

From the description you would see the solution involves calculating even numbers in the range 0 to the length of the string and using the index to get the letter. You could write this in pseudo code:
for index in range 0 to length(string)
    print index and string[index]
Then convert to code.

If you start with code you risk doing weird things like counting by 1 and using the modulo operator to find even numbers.
Quote
#4
(Jul-01-2020, 01:14 PM)deanhystad Wrote: What do you want to do? I don't mean "How are you going to program", but "What do you want to do?" Can you write a concise description of what you want your program to do? That is the first step in writing a program, and that description should be the first thing you mention when asking questions about your program.

I am writing a program that prints every other letter in a string starting with the first letter (index 0). If the string is "Hello World" the output should be:
index = 0 letter = H
index = 2 letter = l
index = 4 letter = o
index = 6 letter = W
index = 8 letter = r
index = 10 letter = d

From the description you would see the solution involves calculating even numbers in the range 0 to the length of the string and using the index to get the letter. You could write this in pseudo code:
for index in range 0 to length(string)
    print index and string[index]
Then convert to code.

If you start with code you risk doing weird things like counting by 1 and using the modulo operator to find even numbers.

Thank you ! got it. i was not thinking correctly. i used what you proposed and used the Range with a step of 2 starting at 0. this solved the exercise.
Quote
#5
You can slice the string:

>>> 'pynative'[::2]
'pntv'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#6
Funny enough, doing this with ord (not the task) gives accidentally the same result.
"".join(c for c in 'pynative' if ord(c) % 2 == 0)
Output:
'pntv'
Coincidence?
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 210 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  list index out of range mcgrim 2 588 May-25-2019, 07:44 PM
Last Post: mcgrim
  IndexError: list index out of range abdullahali 4 945 Jan-17-2019, 07:54 AM
Last Post: buran
  "List index out of range" for output values pegn305 3 2,756 Nov-26-2017, 02:20 PM
Last Post: heiner55
  list index out of range DrPengin 1 1,606 Nov-09-2017, 08:35 PM
Last Post: gruntfutuk

Forum Jump:


Users browsing this thread: 1 Guest(s)