Help with Python.. (1 Viewer)

YasoR17

Mirkofan #1
Jul 4, 2008
7,751
#1
I didn't know where to put this, so if it's in the wrong area please move it :)

So I'm pretty new with Python, and I have a little problem with a small task. I have made a list of strings of a text. It looks like this

liste = ["After", "the", "glimpse", "I", "had", "had", "of", "the", "Martians", "emerging", "from", "the", "cylinder", "in", "which", "they", "had", "come", "to", "the", "earth", "from", "their", "planet",",","a", "kind", "of", "fascination", "paralysed", "my", "actions",".", "I", "remained", "standing", "kneedeep", "in", "the", "heather",",", "staring","at", "the","mound", "that", "hid", "them", ".", "I", "was", "a", "battleground", "of", "fear", "and", "curiosity", ".", "I", "did", "not", "dare", "to", "go", "towards", "the", "pit", ".", "but", "I", "felt", "a", "passionate", "longing", "to", "peer", "into", "it", ".", "I", "began", "walking", "therefore", ",", "in", "a", "big", "curve", ",", "seeking", "some", "point", "of", "vantage", "and", "continually", "looking", "at", "the", "sand", "heaps", "that", "hid", "these", "newcomers", "to", "our", "earth", ".", "Once", "a", "leash", "of", "thin", "black", "whips", ",", "like", "the", "arms", "of", "an", "octopus", ",", "flashed", "across", "the", "sunset", "and", "was", "immediately", "withdrawn", ",", "and", "afterwards", "a", "thin", "rod", "rose", "up", ",", "joint", "by", "joint", ",", "bearing", "at", "its", "apex", "a", "circular", "disk", "that", "spun", "with", "a", "wobbling", "motion", ".", "What", "could", "be", "going", "on", "there", "?"]

And I can't figure out how to remove the two last characters of every word. So it looks something like this:

[’Aft’, ’t’, ’glimp’, ’’, ’h’, ’h’, ’’, ’t’, ’Martia’,
’emergi’, ’fr’, ’t’, ’cylind’, ’’, ’whi’, ’th’, ’h’,
’co’, ’’, ’t’, ’ear’, ’fr’, ’the’, ’plane’, ’’, ’ki’,
’’, ’fascinati’, ’paralys’, ’’, ’action’, ’’, ’remain’,
’standi’, ’kneede’, ’’, ’t’, ’heathe’, ’stari’, ’’, ’t’,
’mou’, ’th’, ’h’, ’the’, ’’, ’w’, ’’, ’battlegrou’, ’’,
’fe’, ’a’, ’curiosit’, ’’, ’d’, ’n’, ’da’, ’’, ’’, ’ba’,
’towar’, ’t’, ’pi’, ’b’, ’’, ’fe’, ’’, ’passiona’, ’longi’,
’’, ’pe’, ’in’, ’i’, ’’, ’beg’, ’walkin’, ’therefor’, ’’,
’’, ’b’, ’curv’, ’seeki’, ’so’, ’poi’, ’’, ’vanta’, ’a’,
’continual’, ’looki’, ’’, ’t’, ’sa’, ’hea’, ’th’, ’h’,
’the’, ’newcome’, ’’, ’o’, ’eart’, ’On’, ’’, ’lea’, ’’,
’th’, ’bla’, ’whip’, ’li’, ’t’, ’ar’, ’’, ’’, ’octopu’,
’flash’, ’acro’, ’t’, ’suns’, ’a’, ’w’, ’immediate’,
’withdraw’, ’a’, ’afterwar’, ’’, ’th’, ’r’, ’ro’, ’u’,
’joi’, ’’, ’join’, ’beari’, ’’, ’i’, ’ap’, ’’, ’circul’,
’di’, ’th’, ’sp’, ’wi’, ’’, ’wobbli’, ’motio’, ’Wh’,
’cou’, ’’, ’goi’, ’’, ’ther’]


If any Python experts out there could help me I'd really appreciate it :D

Thanks.
 

Buy on AliExpress.com

Martin

Senior Member
Dec 31, 2000
56,913
#2
Best. thread. ever. :touched: :touched: :touched:


Let's take this step by step.

1) Remove the last two characters in string
Strings are iterables in Python, so they can be indexed just like lists and other iterables.
string[1] of 'string' gives 't'.
string[-1] of 'string' gives 'g'.

To extract more than one character you can give a range. These are called slices.
s[1:3] of 'string' gives 'tr'
s[1:-1] of 'string' gives 'trin'


2) Changing the list
The easiest option is to use a for loop here.
Code:
for string in liste:
    print string
But because we want to modify the element and not just output it, we can used the list's .index() method to override the current position in the list.
Code:
for string in liste:
    idx = liste.index(string)
    liste[idx] = 'a new string'
That gives you the pieces you need to do your homework :wink:
 
OP
YasoR17

YasoR17

Mirkofan #1
Jul 4, 2008
7,751
  • Thread Starter
  • Thread Starter #3
    Thanks a lot Martin :D

    But :)P) Do I need to slice every single word? Because then I need to know how many characters every one of them contains. Or?
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #4
    Thanks a lot Martin :D

    But :)P) Do I need to slice every single word? Because then I need to know how many characters every one of them contains. Or?
    No, that's the beauty of it. When you can slice with a negative index, it simply counts the letters from the end of the string (in fact it works at both ends). Try it out.
     
    OP
    YasoR17

    YasoR17

    Mirkofan #1
    Jul 4, 2008
    7,751
  • Thread Starter
  • Thread Starter #5
    Yes, but what I don't understand is how I'm going to slice every word in a string that is a text. Like this one.


    string = "After the glimpse I had had of the Martians emerging from the cylinder in which they had come to the earth from their planet, a kind of fascination paralysed my actions. I remained standing kneedeep in the heather, staring at the mound that hid them. I was a battleground of fear and curiosity. I did not dare to go back towards the pit, but I felt a passionate longing to peer into it. I began walking, therefore, in a big curve, seeking some point of vantage and continually looking at the sand heaps that hid these newcomer to our earth. Once a leash of thin black whips, like the arms of an octopus, flashed across the sunset and was immediately withdrawn, and afterwards a thin rod rose up, joint by joint, bearing at its apex a circular disk that spun with a wobbling motion. What could begoing on there?"

    I'm probably not seeing something, like I said very new :p
     

    Martin

    Senior Member
    Dec 31, 2000
    56,913
    #8
    Yes, but what I don't understand is how I'm going to slice every word in a string that is a text. Like this one.


    string = "After the glimpse I had had of the Martians emerging from the cylinder in which they had come to the earth from their planet, a kind of fascination paralysed my actions. I remained standing kneedeep in the heather, staring at the mound that hid them. I was a battleground of fear and curiosity. I did not dare to go back towards the pit, but I felt a passionate longing to peer into it. I began walking, therefore, in a big curve, seeking some point of vantage and continually looking at the sand heaps that hid these newcomer to our earth. Once a leash of thin black whips, like the arms of an octopus, flashed across the sunset and was immediately withdrawn, and afterwards a thin rod rose up, joint by joint, bearing at its apex a circular disk that spun with a wobbling motion. What could begoing on there?"

    I'm probably not seeing something, like I said very new :p
    Oh, okay, you have to split the text into words then.

    liste = string.split(' ')

    Now you have liste with the value in your first post.
     
    OP
    YasoR17

    YasoR17

    Mirkofan #1
    Jul 4, 2008
    7,751
  • Thread Starter
  • Thread Starter #13
    btw I managed to finish it, and found out that a while loop was much easier

    Code:
    mylist =[]
    
    i = 0
    while i < len(liste):
       mylist = mylist + liste[i][:-2].split()
       i = i + 1
    
    print mylist
    thanks again Martin, now I know who to contact when I need help with Python :D
     

    Users Who Are Viewing This Thread (Users: 0, Guests: 1)