I want to split a sentence and assign each word to a variable.
In Ruby I can do it as:
v1,v2,v3,v4,v5 = str1.split
Which will be the Python equivalent ? Thanks.
Andrew. 7 1032
Andreu wrote:
I want to split a sentence and assign each word to a variable.
In Ruby I can do it as:
v1,v2,v3,v4,v5 = str1.split
Which will be the Python equivalent ? Thanks.
That would be:
str1 = "The quick brown fox jumps"
v1, v2, v3, v4, v5 = str1.split ()
TJG
Wow...about ten seconds to get a kind response ....
Thanks Tim.
Andrew.
Tim Golden wrote:
Andreu wrote:
>I want to split a sentence and assign each word to a variable. In Ruby I can do it as:
v1,v2,v3,v4,v5 = str1.split
Which will be the Python equivalent ? Thanks.
That would be:
str1 = "The quick brown fox jumps"
v1, v2, v3, v4, v5 = str1.split ()
TJG
On Jun 23, 4:45 pm, Andreu <r...@sys1.orgwrote:
I want to split a sentence and assign each word to a variable.
In Ruby I can do it as:
v1,v2,v3,v4,v5 = str1.split
Which will be the Python equivalent ? Thanks.
Andrew.
Well a straight copy would be...
>>example = "Hello, how are you" v1, v2, v3, v4 = example.split() print v1, v2, v3, v4
Hello, how are you
>>print v1
Hello,
However I would make a list of it.
>>v_list = example.split() print v_list
['Hello,', 'how', 'are', 'you']
>>for word in v_list:
print word
Hello,
how
are
you
That seems to me to be the more pythonic way to do it, since it is
dynamic.
Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
does not seem to work. My original problem was I forgot about
the parenthesis as Tim point out. So I ended up converting to a
list as in: v = str1.split() and accessing the elements using
v[0] v[1] ect...it is working now. Thanks.
Andreu. co*********@gmail.com wrote:
However I would make a list of it.
>>>v_list = example.split()
That seems to me to be the more pythonic way to do it, since it is
dynamic.
"Andreu" <ro**@sys1.orgwrote in message news:g3**********@aioe.org...
Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
does not seem to work. My original problem was I forgot about
the parenthesis as Tim point out. So I ended up converting to a
list as in: v = str1.split() and accessing the elements using
v[0] v[1] ect...it is working now. Thanks.
Andreu.
v1,v2,v3 = str1.split() will only work if there are exactly three things in
str1.
>>s = 'this is a test' v1,v2,v3 = s.split()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: too many values to unpack
>>v1,v2,v3 = s.split(' ',2) # limit to two splits maximum v1
'this'
>>v2
'is'
>>v3
'a test'
-Mark
On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft...@mailinator.comwrote:
"Andreu" <r...@sys1.orgwrote in messagenews:g3**********@aioe.org...
Yes, ... don't ask me why, but in fact v1,v2,v3 = str1.split()
does not seem to work. My original problem was I forgot about
the parenthesis as Tim point out. So I ended up converting to a
list as in: v = str1.split() and accessing the elements using
v[0] v[1] ect...it is working now. Thanks.
Andreu.
v1,v2,v3 = str1.split() will only work if there are exactly three things in
str1.
>s = 'this is a test' v1,v2,v3 = s.split()
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: too many values to unpack>>v1,v2,v3 = s.split(' ',2) # limit to two splits maximum
>v1
'this'
>v2
'is'
>v3
'a test'
-Mark
In Python 3k I believe you can put a * next to one of the variables to
hold multiple arguments. That'll be aidful! co*********@gmail.com wrote:
On Jun 24, 5:38 am, "Mark Tolonen" <M8R-yft...@mailinator.comwrote:
In Python 3k I believe you can put a * next to one of the variables to
hold multiple arguments. That'll be aidful!
IDLE 3.0b1
>>a,b,*c=[1,2,3,4,5] c
[3, 4, 5]
>>a,*b,c = [1,2,3,4,5] b
[2, 3, 4]
>>a,b,*c=[1,2] c
[]
Augmented assignment is quite similar to argument passing:
at most one starred target;
at least as many items as un-starred targets (as now).
tjr This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sparks |
last post by:
I know this is a stupid question but I can't find the answer.
Please tell me where to find reference to this so I can print it out
and staple it to my head..
dim I as integer
dim missedstr as...
|
by: John Baro |
last post by:
I have a richtextbox which I want the "literal" rtf of.
richtextbox.rtf returns
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0
when i put this into a string I get...
|
by: Dave |
last post by:
I'm receiving info from a com port into a string. I gradually process the
string which constantly shortens it. The question is how long can a string be
before I need to write some info to disk...
|
by: tshad |
last post by:
Can you do a search for more that one string in another string?
Something like:
someString.IndexOf("something1","something2","something3",0)
or would you have to do something like:
if...
|
by: Dan Schumm |
last post by:
I'm relatively new to regular expressions and was looking for some help on a
problem that I need to solve. Basically, given an HTML string, I need to
highlight certain words within the text of the...
|
by: Zordiac |
last post by:
How do I dynamically populate a string array?
I hope there is something obvious that I'm missing here
Option Strict On
dim s() as string
dim sTmp as string = "test"
dim i as integer
...
|
by: Jeff |
last post by:
In the function below, can size ever be 0 (zero)?
char *clc_strdup(const char * CLC_RESTRICT s)
{
size_t size;
char *p;
clc_assert_not_null(clc_strdup, s);
size = strlen(s) + 1;
|
by: tommaso.gastaldi |
last post by:
Hi,
does anybody know a speedy analog of IsNumeric() to check for
strings/chars. I would like to check if an Object can be treated as a
string before using a Cstr(), clearly avoiding the time...
|
by: sucaba.r |
last post by:
I don't know if this is a unique problem, or I'm going about it the
wrong way. I currently connect to one of our SQL servers via a
priviliged account (by using RUNAS). Works with no problem. I...
|
by: Sky |
last post by:
I have been looking for a more powerful version of GetType(string) that will
find the Type no matter what, and will work even if only supplied
"{TypeName}", not the full "{TypeName},{AssemblyName}"...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |