Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

find position in a list made by string by searching incomplete string

7 views
Skip to first unread message

Joug Raw

unread,
Apr 4, 2013, 1:10:06 PM4/4/13
to

I have a list of strings like
list1={{"a", "b something", "a", "a", "b", "c", "b"}
In this list, 'something' is a sting that I will not know in advanced and
there are several spaces between the 'b' and the 'something' in one string.
The number of the spaces is unknown.

I want to find out the position of the this "b something" sting in the
list. So, I did,

Position[list1, "b*"]

and I also tried,
Position[list1,"b"~~___ ]

both of them gave me {} as results.

Also Cases[list1,"b"~~___] or Cases[list1, "b*"] does not work. I can get
the correct position only if I give the exact "b something" as the
searching pattern. What is the easiest way to search with a incomplete
string pattern in this case? Thanks for your help.


Bill Rowe

unread,
Apr 4, 2013, 10:29:07 PM4/4/13
to
On 4/3/13 at 10:50 PM, jou...@gmail.com (Joug Raw) wrote:

>I have a list of strings like list1={{"a", "b something", "a",
>"a", "b", "c", "b"} In this list, 'something' is a sting that I will
>not know in advanced and there are several spaces between the 'b'
>and the 'something' in one string. The number of the spaces is
>unknown.

>I want to find out the position of the this "b something" sting in
>the list. So, I did,

>Position[list1, "b*"]

>and I also tried, Position[list1,"b"~~___ ]

>both of them gave me {} as results.

In[1]:= list1 = {"a", "b something", "a", "a", "b", "c", "b"};

In[2]:= Position[Length /@ StringCases[list1, "b" ~~ __], 1]

Out[2]= {{2}}


debguy

unread,
Apr 4, 2013, 10:30:01 PM4/4/13
to
Map[(StringPosition[#, "b "]) &, list1[[1]]]

I don't think Position looks inside strings. You need to use a string
function to do it. You gave a list of strings so Map checks each and
returns all finds in a List.

Peter Pein

unread,
Apr 4, 2013, 10:30:17 PM4/4/13
to
Hi Joug,

first use StringPosition with a StringExpression as pattern. Then wrap
a Position[] around that:

list1={"a","b something","a","a","b","c","b","b sth. else"};
Position[StringPosition[list1, "b "~~__], Except[{}], 1, Heads->False]

--> {{2},{8}}

Peter

Alexei Boulbitch

unread,
Apr 6, 2013, 5:09:42 AM4/6/13
to
I have a list of strings like
list1={{"a", "b something", "a", "a", "b", "c", "b"}
In this list, 'something' is a sting that I will not know in advanced and
there are several spaces between the 'b' and the 'something' in one string.
The number of the spaces is unknown.

I want to find out the position of the this "b something" sting in the
list. So, I did,

Position[list1, "b*"]

and I also tried,
Position[list1,"b"~~___ ]

both of them gave me {} as results.

Also Cases[list1,"b"~~___] or Cases[list1, "b*"] does not work. I can get
the correct position only if I give the exact "b something" as the
searching pattern. What is the easiest way to search with a incomplete
string pattern in this case? Thanks for your help.



Hi,

It will go easily, if you transform the list of strings (or characters) into the list of symbols:

list1 = {"a", "b something", "a", "a", "b", "c", "b"}

{"a", "b something", "a", "a", "b", "c", "b"}

list2 = Map[ToExpression, list1]

{a, b something, a, a, b, c, b}

Now, one can do it by several ways. For instance, this:

Position[list2, (b a_)]

{{2}}

One can use the fact that the special form of "b something" gives it a Head that differs from that of all other terms. Indeed,

Head /@ list2

{Symbol, Times, Symbol, Symbol, Symbol, Symbol, Symbol}


this item has the head Times, while the others - Symbol. We can, therefore, do the following:

Position[list2, Select[list2, Head[#] == Times &][[1]]]

{{2}}

or otherwise

Position[Head /@ list2, Times]

{{2}}

I guess that one can see also few other ways.

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




0 new messages