問題您有一個或多個列表或矩陣向量中的數據需要排序。
解決方案利用排序列表()或者將列表排序為() ,秩() ,或將列表和向量放入數據表中並對其進行排序。
// 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