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


Groups > comp.lang.forth > #57607

.Re: splitting a string into 2 new strings

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

Show all headers | View raw


> >       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?


-- 
"A Jew always has a much higher soul than a gentile, even if he is a
homosexual," he [Israel's Deputy Defense Minister Eli Ben-Dahan] said.
theoccidentalobserver.net/2015/10/israeli-minister-latest-in-long-line-of-jews-asserting-the-inferiority-of-non-jews

Back to comp.lang.forth | Previous | Next — Next 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