问题您有一个或多个列表或矩阵向量中的数据需要排序。
解决方案利用排序列表()或者将列表排序为() ,秩() ,或将列表和向量放入数据表中并对其进行排序。
// Sort List example (makes new list)
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
sname = Sort List( names );
Show( names, sname );names = {"fred", "Andy", "Rory", "bess", "susy", "perl"}; // 不变
sname = {"Andy", "bess", "fred", "perl", "Rory", "susy"}; // 排序 // Sort List Into example (in-place sort)
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
Sort List Into( names );
Show( names );names = {"Andy", "bess", "fred", "perl", "Rory", "susy"}; //就地排序上面的两个例子都在一个列表上运行。 如果有第二个电话号码列表,他们不会坚持姓名。
要使多个列表都具有相同的排序,您可以使用 Rank() 函数。 这些列表应该具有相同数量的元素,并且在排序键列表中没有缺失值。
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
phone = {"x321", "x322", "x323", "x324", "x325", "x326"};
rooms = {"1003", "1001", "1005", "1002", "1006", "1004"};
rnk = Rank( names ); // [2, 4, 1, 6, 3, 5]
// use the rnk vector to make sorted lists from the old lists
snames = names[rnk];
sphone = phone[rnk];
srooms = rooms[rnk];
Show( snames, sphone, srooms );snames = {"Andy", "bess", "fred", "perl", "Rory", "susy"}; sphone = {"x322", "x324", "x321", "x326", "x323", "x325"}; srooms = {"1001", "1002", "1003", "1004", "1005", "1006"};
上面,以 s 为前缀的新列表都是按名称排序的。
如果你有多个列表,长度相同,但在列表中配对,也许数据表会是更好的数据结构。 您可能会发现使用表格更容易,因为您可以查看、保存、划分子集……但是,如果您已经有了这些列表,则可以从中构建一个表格。 这是一个可重复使用的用户函数:
// the zip function, named after a Python function that, like a zipper, zips two lists together
zip = Function( {variableNameList}, // a list of variable names
{iVarName, dt, data}, // local variables
dt = New Table( "zip", "invisible" ); // a hidden table, returned at end of this function
For( iVarName = 1, iVarName <= N Items( variableNameList ), iVarName += 1,
data = Eval( variableNameList[iVarName] ); // makes a copy of the data from the name
If( Is Matrix( data ) | Is Number( data[1] ), // handle lists of numbers same as a matrix
dt << New Column( Char( variableNameList[iVarName] ), "numeric", values( data ),
setProperty( "wasMatrix", eval(Is Matrix( data ) ) ) ) // remember how to convert back, could be list
, // else a list of strings
dt << New Column( Char( variableNameList[iVarName] ), "character", values( data ),
setProperty( "wasString", eval( Is String( data[1] ) ) ) ) // remember how to convert back, could be names
);
);
dt << New Column( "_original order_", values( 1 :: N Rows( dt ) ) ); // this extra column is not required, but might be useful
dt << setdirty( 0 ); // not required but avoids a "save?" prompt
dt; // return
);例子:
cars = {chevy, volkswagon, honda, ford, toyota}; // list of unquoted names
doors = [4, 2, 2, 5, 2]; // matrix of numbers
miles = {30000, 20000, 40000, 10000, 50000}; // list of numbers
owner = {"fred", "susan", "ralph", "penelope", "terry"}; // list of quoted strings
zippedDt = zip( {cars, doors, miles, owner} );
zippedDt << sort( by( miles ), replacetable( 1 ) );
zippedDt << show Window; // table hidden by default
Lists are in table's columns, sorted by miles列表位于表格的列中,按英里排序如果您正在使用列表,您可能希望重新获得所有列表。 这是一个解压缩函数:
// unzip, similar to Python's unzip
unzip = Function( {dt}, // data table previously returned from zip (above)
{result, cols, iCol, col, names, iRow}, // local variables
cols = dt << getcolumnnames; // below, the _original order_ col gets skipped
result = {}; // assemble all of the lists and matrices here, return at bottom
For( iCol = 1, iCol < N Items( cols )/*skip last one*/, iCol += 1,
col = Column( dt, cols[icol] ); // the Ith col in the table
If( col << getdatatype( "English" ) == "Numeric", //
// return the same type of object, list or matrix, supplied originally
If( col << getproperty( "wasMatrix" ), // by checking the property we set above
Insert Into( result, col << getValues ); // append a matrix
, // else append a list of numbers
result[N Items( result ) + 1] = As List( dt[0, iCol] )
)
, // else character column
// return same type, list of names or list of strings...
If( col << getproperty( "wasString" ), // check character col prop set above
result[N Items( result ) + 1] = (dt[0, iCol]) // list of strings
, // else turn the strings into names again
result[N Items( result ) + 1] = As Name( dt[0, iCol] ) // list of names
)
);
);
result; // return
);例子:
{cars, doors, miles, owner} = unzip( zippedDt );已分配列表(4 个元素)。
汽车:{福特、大众汽车、雪佛兰、本田、丰田}
门:[5, 2, 4, 2, 2]
英里:{10000、20000、30000、40000、50000}
所有者:{“佩内洛普”、“苏珊”、“弗雷德”、“拉尔夫”、“特里”}
最后
// Important! delete the invisible table!
Close( zippedDt, nosave );
讨论如果您使用压缩/解压缩功能,请务必清理不可见的表。 (它们与 .zip 文件的压缩功能无关。 JMP 在 zip 存档对象中支持它。)
数据表是处理复杂数据结构的好工具。 您可以创建隐藏或私有表来管理数据,而无需使用上面的包装函数。 如果您需要添加和删除大量项目,列表可能比数据表更有效(如果实现为从表中添加和删除行,队列和堆栈可能会很慢。)zip 函数(上面)需要一个列表名字. 一个姓名(无引号)与 a 不同字符串(使用引号) . 传递名称列表可减少复制数据的次数。
也可以看看https://www.jmp.com/support/help/14-2/list-functions.shtml