問題並べ替える必要がある 1 つ以上のリストまたは行列ベクトルにデータがあります。
解決使用する並べ替えリスト()またリストを並べ替え() 、ランク() 、またはリストとベクトルをデータテーブルに入れて並べ替えます。
// 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"}; // その場でソート上記の両方の例は、単一のリストで動作します。 電話番号の 2 番目のリストがある場合、それらは名前に固執しません。
複数のリストをすべて同じ並べ替えにするには、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 );List( 4 つの要素) が割り当てられました。
車:{フォード、フォルクスワーゲン、シボレー、ホンダ、トヨタ}
ドア:[5、2、4、2、2]
マイル:{10000、20000、30000、40000、50000}
所有者:{"ペネロペ"、"スーザン"、"フレッド"、"ラルフ"、"テリー"}
ついに
// Important! delete the invisible table!
Close( zippedDt, nosave );
討論zip/unzip 関数を使用する場合は、非表示のテーブルを必ずクリーンアップしてください。 (.zipファイルの圧縮機能とは関係ありません。 JMP は zip アーカイブ オブジェクトでそれをサポートしています。)
データ テーブルは、複雑なデータ構造に最適なツールです。 上記のラッパー関数を使用せずに、データを管理するための非表示またはプライベート テーブルを作成できます。 大量のアイテムを追加および削除する必要がある場合、リストはデータ テーブルよりも効率的です (テーブルに行を追加および削除するように実装されている場合、キューとスタックは遅くなる可能性があります)。zip 関数 (上記) は、名前. あ名前(引用符なし) は a と同じではありません文字列 (引用符を使用) . 名前のリストを渡すと、データがコピーされる回数が減ります。
関連項目https://www.jmp.com/support/help/14-2/list-functions.shtml