Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #57676

Re: .Re: splitting a string into 2 new strings

From "Robert L." <No_spamming@noWhere_7073.org>
Newsgroups comp.lang.forth
Subject Re: .Re: splitting a string into 2 new strings
Date 2017-04-21 00:19 +0000
Organization Aioe.org NNTP Server
Message-ID <odbj70$ina$1@gioia.aioe.org> (permalink)
References <od8gd9$1hsj$1@gioia.aioe.org>

Show all headers | View raw


On 4/19/2017, 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?

Shorter:

["0-9","A-Z"].map{|s| 'C6 H12 O6'.delete s}

 ===>
["C H O", "6 12 6"]

-- 
Jewish drug dealers, child porn pushers, and slave traders are free
from prosecution in Israel.  Israel does not consider these to be
crimes ... so long as the victims are non-Jews.
www.theoccidentalobserver.net/2009/08/the-culture-of-deceit/

Back to comp.lang.forth | Previous | Next — Previous in thread | Find similar | Unroll thread


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