Message Boards Message Boards

Select strings in a list?

Posted 7 years ago

Hi!

Consider a list of strings:

BASES = {"A", "Aéro", "Agro-", "Algie", "Allo-", "Amphi", "Ana-", 
   "Andro", "Anthropos", "Anti-", "Arachn-", "Archi-", "Aristo", 
   "Arthr", "?", "aster", "astr-", "auto", "B", "Bacchus", "Biblio", 
   "Bio", "C", "Cardio", "Cérès", "Chaos", "Chromo", "Chrono", "Ciné",
    "Cosmo", "Crate", "cratie", "D", "Démo-", "Dia-", "Doros", "Dory",
    "Doxa", "Drome", "Dynami", "E", "Echo", "Eco", "Eros", "Ethno", 
   "Eu", "G", "Gamos", "Gaster-", "Gastro-", "Gène", "Géo", "Gér-", 
   "géronto", "gloss", "Glott", "Gonè", "Gonos", "Graphe", "Gyne", 
   "gynéco", "H", "Hermès", "Hippo-", "Histoire", "homéo", "Homo", 
   "Hydro", "Hypno", "Hygi-", "I", "Iatre", "K", "kiné", "L", "Lith", 
   "Logie", "logue", "Lys-", "lyt", "M", "Macro", "Mane", "manie", 
   "Mars", "Méduse", "Méga", "mégalo", "Méter", "Mètre", "métro", 
   "Micro", "Miso", "Mono", "Morphè", "Morphée", "Muses", "musique", 
   "Mytho", "N", "Narcisse", "Naute", "Nécro", "Nésie", "Neuro", 
   "névr", "Nome", "O", "Océan", "-oïde", "Oligo", "Olympe", "Onyme", 
   "Ops", "Ortho", "Ostéo", "P", "Pan", "Patho", "Péd/pod", "Péd", 
   "Phago", "Philo", "Phobie", "Phonè", "Phore", "Photo", "Pneumo", 
   "Pole", "Poly", "Porno", "Proto", "Psychè", "Pyr", "S", "scène", 
   "Scope", "Sèm-", "Sismo", "Sophos", "sosie", "sperm", "spore", 
   "sym", "Syn", "T", "Taur", "Techno", "Théâtre", "Théo", "Théorie", 
   "Thèque", "Therap-", "Thermo", "Titan", "V", "Vénus", "Volcan", 
   "Z", "Zoo"};

I merely want to get rid of single characters, but

Select[BASES, (StringLength[#] > 1) &]

does nothing, while

Map[(StringLength[#] > 1) &, BASES]

gives the expected logical list.

Why? What is the right way to use Select here?

Thanks, Claude

POSTED BY: Claude Mante
3 Replies

Everything works very well now!

Thanks a lot

POSTED BY: Claude Mante

Note that Select does not update the variable BASES. You have to explicitly update it:

BASES = Select[BASES, .......]

This is generally true in Mathematica, though there are exceptions; there are function that update the symbol for you. E.g. Increment, AppendTo, AssociateTo, KeyDromFrom, etc. and the more obvious (mostly because of the infix notation) ones: SubtractFrom, AddTo, TimesBy et cetera,

POSTED BY: Sander Huisman

The function Select[] selects from the list BASES strings with length greater than 1:

 In[2]:= Select[BASES, (StringLength[#] > 1) &]

Out[2]= {"Aéro", "Agro-", "Algie", "Allo-", "Amphi", "Ana-", "Andro", \
"Anthropos", "Anti-", "Arachn-", "Archi-", "Aristo", "Arthr", \
"aster", "astr-", "auto", "Bacchus", "Biblio", "Bio", "Cardio", \
"Cérès", "Chaos", "Chromo", "Chrono", "Ciné", "Cosmo", "Crate", \
"cratie", "Démo-", "Dia-", "Doros", "Dory", "Doxa", "Drome", \
"Dynami", "Echo", "Eco", "Eros", "Ethno", "Eu", "Gamos", "Gaster-", \
"Gastro-", "Gène", "Géo", "Gér-", "géronto", "gloss", "Glott", \
"Gonè", "Gonos", "Graphe", "Gyne", "gynéco", "Hermès", "Hippo-", \
"Histoire", "homéo", "Homo", "Hydro", "Hypno", "Hygi-", "Iatre", \
"kiné", "Lith", "Logie", "logue", "Lys-", "lyt", "Macro", "Mane", \
"manie", "Mars", "Méduse", "Méga", "mégalo", "Méter", "Mètre", \
"métro", "Micro", "Miso", "Mono", "Morphè", "Morphée", "Muses", \
"musique", "Mytho", "Narcisse", "Naute", "Nécro", "Nésie", "Neuro", \
"névr", "Nome", "Océan", "-oïde", "Oligo", "Olympe", "Onyme", "Ops", \
"Ortho", "Ostéo", "Pan", "Patho", "Péd/pod", "Péd", "Phago", "Philo", \
"Phobie", "Phonè", "Phore", "Photo", "Pneumo", "Pole", "Poly", \
"Porno", "Proto", "Psychè", "Pyr", "scène", "Scope", "Sèm-", "Sismo", \
"Sophos", "sosie", "sperm", "spore", "sym", "Syn", "Taur", "Techno", \
"Théâtre", "Théo", "Théorie", "Thèque", "Therap-", "Thermo", "Titan", \
"Vénus", "Volcan", "Zoo"}

The logic of the code with Map[] is to verify if the length of the string is greater than 1 or no:

In[3]:= Map[(StringLength[#] > 1) &, BASES]

Out[3]= {False, True, True, True, True, True, True, True, True, True, \
True, True, True, True, False, True, True, True, False, True, True, \
True, False, True, True, True, True, True, True, True, True, True, \
False, True, True, True, True, True, True, True, False, True, True, \
True, True, True, False, True, True, True, True, True, True, True, \
True, True, True, True, True, True, True, False, True, True, True, \
True, True, True, True, True, False, True, False, True, False, True, \
True, True, True, True, False, True, True, True, True, True, True, \
True, True, True, True, True, True, True, True, True, True, True, \
True, False, True, True, True, True, True, True, True, False, True, \
True, True, True, True, True, True, True, False, True, True, True, \
True, True, True, True, True, True, True, True, True, True, True, \
True, True, True, False, True, True, True, True, True, True, True, \
True, True, True, False, True, True, True, True, True, True, True, \
True, True, False, True, True, False, True}
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract