Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #57638
| From | "HAA" <someone@microsoft.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: splitting a string into 2 new strings |
| Date | 2017-04-20 19:07 +1000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <od9tpp$1mmg$1@gioia.aioe.org> (permalink) |
| References | <od8gd9$1hsj$1@gioia.aioe.org> |
Robert L. wrote:
> > > I have a string e.g. 'C6 H12 O6' that I wish to split up to give 2
> > > strings
> > > 'C H O' and '6 12 6'. I have played with string.split() and the re module
> > > - but can't quite get there.
> > >
> > > Any help would be greatly appreciated.
> ...
> > I'm, assuming that these are chemical compounds, so you're not limited to
> > one-character symbols.
> ...
> > import re
> >
> > re_pat = re.compile('([A-Z]+)(\d+)')
> > text = 'C6 H12 O6'
> >
> > # find each component, returns list of tuples (e.g. [('C', '6'), ...]
> > component = re_pat.findall(text)
> >
> > #split into separate lists
> > symbols, counts = zip(*component)
> >
> > # create the strings
> > symbols = ' '.join(symbols)
> > counts = ' '.join(counts)
>
> 'C6 H12 O6'.split.map{|s| s.scan /\d+|\D+/}.transpose.
> map{|a| a.join ' '}
>
> ===>
> ["C H O", "6 12 6"]
>
> In Forth?
Forth has sufficient flexibility so when there's no library or not fit for purpose, you
won't be left stranded. Your code will likely be more efficient than a library because
you won't be solving problems you don't have. You can code anything because
Forth encourages you to be a programmer - not a user dependent upon libraries.
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
.Re: splitting a string into 2 new strings "Robert L." <No_spamming@noWhere_7073.org> - 2017-04-19 20:13 +0000
Re: splitting a string into 2 new strings "HAA" <someone@microsoft.com> - 2017-04-20 19:07 +1000
Re: splitting a string into 2 new strings "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-20 17:08 +0000
Re: splitting a string into 2 new strings "HAA" <someone@microsoft.com> - 2017-04-21 11:54 +1000
Re: splitting a string into 2 new strings "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-21 09:45 +0000
Re: splitting a string into 2 new strings "HAA" <someone@microsoft.com> - 2017-04-21 23:32 +1000
Re: splitting a string into 2 new strings john <anon@example.com> - 2017-04-21 16:36 +0100
Re: splitting a string into 2 new strings "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-21 20:15 +0000
Re: splitting a string into 2 new strings Paul Rubin <no.email@nospam.invalid> - 2017-04-21 17:06 -0700
Re: splitting a string into 2 new strings "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-22 04:59 +0000
Re: splitting a string into 2 new strings "HAA" <someone@microsoft.com> - 2017-04-23 10:55 +1000
Re: splitting a string into 2 new strings "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-04-29 03:15 +0000
Re: splitting a string into 2 new strings The Beez <the.beez.speaks@gmail.com> - 2017-05-02 04:07 -0700
Re: splitting a string into 2 new strings "Rudy Velthuis" <newsgroups@rvelthuis.de> - 2017-05-05 15:26 +0000
Re: splitting a string into 2 new strings hughaguilar96@gmail.com - 2017-04-20 13:32 -0700
Re: .Re: splitting a string into 2 new strings "Robert L." <No_spamming@noWhere_7073.org> - 2017-04-21 00:19 +0000
csiph-web