Finding Elements within a list


6 posts
by ibanezmatt13 » Sat Dec 29, 2012 11:31 am
Hi,

I am creating a program where which the user can input a string into a variable. The program will then convert the variable into a list. The program is a string to morse code program. It makes a list of a string inputed by the user, then it searches the characters of the list and then performs a function whereby an LED is flashed using morse code depending upon what the user enters. Here is my code so far:

string = raw_input("Enter ")
list_string = list(string)

last_element = len(list_string) -1

print list_string[0]
print list_string[last_element]

Fundamentally, I can work out the first and last element of a list and can print the first and last element onto the screen. However, I need to be able to print all of the elements in between the first element (0) and the last element (last_element -1)
Would I do something like:

print list_string[0: last_element] ???

This doesn't work, any suggestions on the correct way to enter this line of code would be greatly appreciated.

Many thanks in advance
Ibanezmatt13
Posts: 128
Joined: Fri Dec 28, 2012 9:49 am
by poing » Sat Dec 29, 2012 11:56 am
Just iterate through the elements with a for loop?
Posts: 728
Joined: Thu Mar 08, 2012 3:32 pm
by alexeames » Sat Dec 29, 2012 2:35 pm
poing wrote:Just iterate through the elements with a for loop?


Yep

Code: Select all
for element in string:


will treat string as a list and iterate through each item, assigning the value of element to each successive list member.
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 1713
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by -rst- » Mon Dec 31, 2012 5:39 pm
...and the 'elements' of string are...? ;)

Code: Select all
mystring = "Hello, world!"
for current_char in mystring:
    print current_char

...will treat the string as a list of characters, of course. And 'assigning the value of element to each successive list member' is most likely the other way around, isn't it?

Another way to put it - some might say more explanatory, some may complain that it's too low-level or non-object-oriented:
Code: Select all
mystring = "Hello, world!"
for cur_char_index in range(0, len(mystring)):
    print mystring[cur_char_index]
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 852
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland
by alexeames » Mon Dec 31, 2012 11:15 pm
-rst- wrote:...and the 'elements' of string are...? ;)


Carbon, Hydrogen, Oxygen and probably some Nitrogen and maybe a dash of Phosphorus and a few trace impurities. :lol:

No I mean each character. Or are you asking me
Code: Select all
len(piece_of_string)
?;) I was expecting the OP to come back and discuss it, but I think he's more occupied in the other thread. :lol:

Good point about the non-alphanumeric characters, but I would filter those out if not required in the morse version.
My Pi uses 2 watts - what what? ---- HiRes early production Pi photos RS Front Back | Farnell Front Back
User avatar
Posts: 1713
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
by -rst- » Wed Jan 02, 2013 1:33 pm
Haha :D 'Are we talking about strings you use when sewing or maybe guitar strings or...'

What I meant, is that while saying 'iterate over the elements in a string' is technically correct, it might be more educational to point out that the 'elements' of a string are characters and naming the variable something that tells it is a character (instead of the generic 'element') would be a good practice.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 852
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland