Strings for a newbie

M

Malcolm Wooden

I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

UGH!

Malcolm
(a disillusioned Python newbie)
 
J

John Machin

Malcolm said:
I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

UGH!

Malcolm
(a disillusioned Python newbie)


>>> s = "This is a sentence of words"
>>> a = s.split()
>>> a ['This', 'is', 'a', 'sentence', 'of', 'words']
>>>
 
W

Wolfram Kraus

Malcolm said:
I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

UGH!

Malcolm
(a disillusioned Python newbie)
Use split:
['This', 'is', 'a', 'sentence', 'of', 'words']

See:
http://www.python.org/doc/2.4.1/lib/string-methods.html#l2h-202

HTH,
Wolfram
 
M

Mage

Malcolm said:
In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

s = "This is a sentence of words"
print s.split(' ')

Mage
 
S

Sergei Organov

Malcolm Wooden said:
I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Simple?! 7 lines of code for such a trivial task is simple?!
Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

from string import split
s = "This is a sentence of words"
a = split(s)

or,

s = "This is a sentence of words"
a = s.split()

or even

a = "This is a sentence of words".split()
 
M

Malcolm Wooden

Sorry John but that don't do it for me. Just get errors comming back


John Machin said:
Malcolm said:
I'm trying to get my head around Python but seem to be failing miserably.
I use RealBasic on a Mac and find it an absolute dream! But
Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence
of words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

UGH!

Malcolm
(a disillusioned Python newbie)
s = "This is a sentence of words"
a = s.split()
a ['This', 'is', 'a', 'sentence', 'of', 'words']
 
J

John Machin

Malcolm said:
Sorry John but that don't do it for me. Just get errors comming back


s = "This is a sentence of words"
a = s.split()
a

['This', 'is', 'a', 'sentence', 'of', 'words']

Malcolm,

What errors did you get? Please post a copy of what you see on your
screen; my palantir is on the fritz :)

Cheers,
John
 
M

Malcolm Wooden

my actual code is:

for x in range(len(l)):
h = string.split(l[x])

where the sentence string is in an array of one element 'l'
Error is:

Traceback (most recent call last):
File "<string>", line 34, in ?
File "<string>", line 27, in SentenceText
File "C:\PYTHON22\lib\string.py", line 122, in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'


John Machin said:
Malcolm said:
Sorry John but that don't do it for me. Just get errors comming back


s = "This is a sentence of words"
a = s.split()
a

['This', 'is', 'a', 'sentence', 'of', 'words']

Malcolm,

What errors did you get? Please post a copy of what you see on your
screen; my palantir is on the fritz :)

Cheers,
John
 
J

John Machin

Malcolm said:
my actual code is:

for x in range(len(l)):
h = string.split(l[x])

where the sentence string is in an array of one element 'l'
Error is:

Traceback (most recent call last):
File "<string>", line 34, in ?
File "<string>", line 27, in SentenceText
File "C:\PYTHON22\lib\string.py", line 122, in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'
Can't tell from that -- do you have "import string" above that, or what?

Please post the whole file, which BTW you should not call "string".
In any case you should not be using the string module; it's old and
deprecated. Use string methods instead. Why are you using Python 2.2??

I have to go now; it's way past bedtime at this longitude. Like I said,
post the *whole* file; others will be able to straighten you out when
they see the gory detail :)

Cheers,
John
 
W

Wolfram Kraus

Malcolm said:
my actual code is:

for x in range(len(l)):
h = string.split(l[x])

where the sentence string is in an array of one element 'l'
Error is:

Traceback (most recent call last):
File "<string>", line 34, in ?
File "<string>", line 27, in SentenceText
File "C:\PYTHON22\lib\string.py", line 122, in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

What is l[x]? Are you sure that l[x] is a string? Do a print before the
split to see what l[x] is. Oh, and no need for range here, if l is a list:

for x in l:
print x
h = x.split()

HTH,
Wolfram
 
M

Malcolm Wooden

Yes Sergei, as 3 of the lines are Dim statements, the real code is just 4
lines, a totally logical. It's not the amout of code thats a probelm, it's
following the logic and structure thats important. As I said Python.. UGH!

Malcolm
 
K

Kalle Anka

Malcolm Wooden said:
Yes Sergei, as 3 of the lines are Dim statements, the real code is just 4
lines, a totally logical. It's not the amout of code thats a probelm, it's
following the logic and structure thats important. As I said Python.. UGH!

Since I both use RB and Python I can say the the "logic and structure"
(if you by that means the semantics) is more or less the exactly the
same in RB and Python. The only major difference is that in RB you
have to declare the type.
 
K

Kalle Anka

Malcolm Wooden said:
I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

Compare this RB code

dim s as String
dim a(-1) as String

s = "This is a sentence of words"
a = s.split()

with the python code
s = "This is a sentece of words"
s.split() ['This', 'is', 'a', 'sentece', 'of', 'words']
 
B

bruno modulix

Malcolm said:
I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

Strange enough, Rb was one of my first languages, and last time I played
with it, I founhd it was close to a nightmare when compared to Python...
I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

<sarcastic>
Ho, yes, so simple... only seven lines...
Now can I see how this is done in Python? - nope!

Why not ?

a = "This is a sentence of words".split()

Now *this* is simplicity, my friend...
UGH!

Malcolm
(a disillusioned Python newbie)
So I'll leave you to your disi^M^M^Mdelusion...
 
B

bruno modulix

Malcolm Wooden wrote:
(top post corrected)
Malcolm Wooden wrote:
(snip useless rant)
I want to put a sentence of words into an array, eg "This is a sentence
of words"

In RB it would be simple:
(snip 'simple' example)
(snip other useless rant)
s = "This is a sentence of words"
a = s.split()
a

['This', 'is', 'a', 'sentence', 'of', 'words']
Sorry John but that don't do it for me. Just get errors comming back

http://www.catb.org/~esr/faqs/smart-questions.html

Strange enough, I used variations of the above code - which is a pretty
common Python idiom - in tens and tens of working applications...
 
S

Steve Holden

Malcolm said:
Yes Sergei, as 3 of the lines are Dim statements, the real code is just 4
lines, a totally logical. It's not the amout of code thats a probelm, it's
following the logic and structure thats important. As I said Python.. UGH!

Malcolm
Yes, the weirdest thing about the whole language is the way it jumps off
the Internet and forces you to use it ... :) (Which is my way of saying
that if you are happy with RealBasic then there's no need to pick
Python up in the first place).

I can quite see why you would prefer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

to

s = "This is a sentence of words"
a = s.split()

How dare Python be shorter than RealBasic and easier to understand?
(although you should check the validity of the Python code against a
RealBasic interpreter. You might be surprised.) You also said
my actual code is:

for x in range(len(l)):
h = string.split(l[x])

where the sentence string is in an array of one element 'l'
Error is:

Traceback (most recent call last):
File "<string>", line 34, in ?
File "<string>", line 27, in SentenceText
File "C:\PYTHON22\lib\string.py", line 122, in split
return s.split(sep, maxsplit)
AttributeError: 'list' object has no attribute 'split'

and then wondered why you got error messages trying to apply "split()"
to some list. This is equivalent to saying "somebody told me to hammer
this nail in, but my screwdriver won't hammer things".

Those who don't *want* to understand will find ways not to. Those who
*do* want to understand will engage on a dialog until their problems are
solved.

The fact that you don't even use the split() method that RealBasic makes
available means you are overlooking the fact that the two languages are
actually identical (modulo a few declarations) for your problem!

python-ugh-indeed-ly y'rs - steve
 
J

James Hofmann

Malcolm Wooden said:
I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.

Now can I see how this is done in Python? - nope!

UGH!

Malcolm
(a disillusioned Python newbie)

This is the "verbose" version.

s = "This is a sentence of words"
nextword = ""
words = []
for character in s:
if character==" ":
words.append(nextword)
nextword = ""
else:
nextword+=character # string types use += instead of append
words.append(nextword) # include the last word
print words

The main sticking point for you was probably understanding the way Python lets
you loop through sequence types. In general, sequences(list, string, etc.) can
be iterated either by an explicit function call, item = sequence.next(), or more
commonly with "for item in sequence:" which will make Python go through every
item it sees inside the sequence.

This turns out to be more convenient than keeping a seperate counter in the vast
majority of cases, because it cuts straight to the business of manipulating each
item, rather than tracking where it is.

Now a "slim" version:

s = "This is a sentence of words"
print s.split()

But there's no learning from that one, it's just an easy library call.
 
W

Warren Block

James Hofmann said:
Malcolm Wooden said:
I'm trying to get my head around Python but seem to be failing miserably. I
use RealBasic on a Mac and find it an absolute dream! But Python....UGH!

I want to put a sentence of words into an array, eg "This is a sentence of
words"

In RB it would be simple:

Dim s as string
Dim a(-1) as string
Dim i as integer

s = "This is a sentence of words"
For i = 1 to CountFields(s," ")
a.append NthField(s," ",i)
next

That's it an array a() containing the words of the sentence.
[snip]

Now a "slim" version:

s = "This is a sentence of words"
print s.split()

To match the original program, it should be:

s = "This is a sentence of words"
a = s.split()

....assigning the list returned by split() to a variable called a.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top