Message Boards Message Boards

0
|
4413 Views
|
3 Replies
|
6 Total Likes
View groups...
Share
Share this post:

The result of the splitting List is not as expected.

The results of the two data are different. Does anyone know the reason?

    data1 = {1, 2, 3, 4, 3, 2, 1, 5, 6, 7, 4, 3};
    data2 = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 39, 40, 41, 42, 43, 44, 45, 46};
    Split[data1, Less]
    Split[data2, Less]

result 1:  {{1, 2, 3, 4}, {3}, {2}, {1, 5, 6, 7}, {4}, {3}}

result 2:  {{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 39, 40, 
  41, 42, 43, 44, 45, 46}}

But the answer I want is :

Split[data2, Less] = {{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, {39, 40, 41, 42, 43, 44, 45, 46}}
POSTED BY: Tsai Ming-Chou
3 Replies

Thank Hans Dolhaine & Valeriu Ungureanu.

Let me understand the correct way to use Split~~~

POSTED BY: Tsai Ming-Chou
In[15]:= data1 = {1, 2, 3, 4, 3, 2, 1, 5, 6, 7, 4, 3};
data2 = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
   39, 40, 41, 42, 43, 44, 45, 46};
Split[data1, Less]
Split[data2, #2 - #1 < 2 &]

Out[17]= {{1, 2, 3, 4}, {3}, {2}, {1, 5, 6, 7}, {4}, {3}}

Out[18]= {{5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
  20}, {39, 40, 41, 42, 43, 44, 45, 46}}

Split does exactly what is described in the documentation.

In data2a you have only True, therefore the whole list is given as result

data1 = {1, 2, 3, 4, 3, 2, 1, 5, 6, 7, 4, 3};
data2 = {5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 39, 40, 41, 42, 43, 44, 45, 46};
data1a = Partition[data1, 2, 1]
#[[1]] < #[[2]] & /@ data1a
data2a = Partition[data2, 2, 1]
#[[1]] < #[[2]] & /@ data2a
POSTED BY: Hans Dolhaine
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