문제정렬해야 하는 하나 이상의 목록 또는 행렬 벡터에 데이터가 있습니다.
해결책사용 목록 정렬() 또는 목록을 다음으로 정렬() , 계급() , 또는 목록과 벡터를 데이터 테이블에 넣고 정렬합니다.
// Sort List example (makes new list)
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
sname = Sort List( names );
Show( names, sname );이름 = {"프레드", "앤디", "로리", "베스", "수지", "펄"}; // 변경되지 않음
sname = {"앤디", "베스", "프레드", "펄", "로리", "수지"}; // 정렬됨 // Sort List Into example (in-place sort)
names = {"fred", "Andy", "Rory", "bess", "susy", "perl"};
Sort List Into( names );
Show( names );이름 = {"앤디", "베스", "프레드", "펄", "로리", "수지"}; // 내부 정렬위의 두 예제는 단일 목록에서 작동합니다. 두 번째 전화번호 목록이 있으면 이름에 집착하지 않습니다.
여러 목록을 모두 같은 정렬로 만들려면 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 = {"앤디", "베스", "프레드", "펄", "로리", "수지"}; sphone = {"x322", "x324", "x321", "x326", "x323", "x325"}; 스룸 = {"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/unzip 기능을 사용하는 경우 보이지 않는 테이블을 정리하십시오. (.zip 파일의 압축 기능과는 무관합니다. JMP는 zip 아카이브 개체에서 이를 지원합니다.)
데이터 테이블은 복잡한 데이터 구조를 위한 훌륭한 도구입니다. 위의 래퍼 함수를 사용하지 않고 데이터 관리를 위한 숨겨진 또는 개인용 테이블을 만들 수 있습니다. 많은 항목을 추가하고 제거해야 하는 경우 목록이 데이터 테이블보다 더 효율적일 수 있습니다(대기열과 스택은 테이블에서 행을 추가하고 제거하는 것으로 구현되는 경우 속도가 느릴 수 있음). zip 함수(위)에는 다음 목록이 필요합니다. 이름 . ㅏ 이름 (인용 부호 없음)은 a와 동일하지 않습니다. 문자열(따옴표 사용) . 이름 목록을 전달하면 데이터 복사 횟수가 줄어듭니다.
또한보십시오https://www.jmp.com/support/help/14-2/list-functions.shtml