Message Boards Message Boards

0
|
2875 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to "Lower Triangularize" a list of ordered pairs?

Suppose I have a list of mixed types and want to construct a list of ordered pairs from that list where the first element comes before the second element in the original list. I'm sure there's a better approach than what I've come up with:

In[15]:= lst = {0, "a", 2, "cd", 10};

In[16]:= Select[Flatten[Outer[List, lst, lst], 1], 
 Position[lst, #[[1]]][[1, 1]] < Position[lst, #[[2]]][[1, 1]] &]

Out[16]= {{0, "a"}, {0, 2}, {0, "cd"}, {0, 10}, {"a", 2}, {"a", 
  "cd"}, {"a", 10}, {2, "cd"}, {2, 10}, {"cd", 10}}
POSTED BY: Frank Kampas
2 Replies

Thanks.

POSTED BY: Frank Kampas

Use Subsets[]:

In[8]:= Subsets[lst, {2}]
Out[8]= {{0, "a"}, {0, 2}, {0, "cd"}, {0, 10}, {"a", 2}, {"a", "cd"}, {"a", 10}, {2, "cd"}, {2, 10}, {"cd", 10}}
POSTED BY: Szabolcs Horvát
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