다음과 같은 매우 긴 목록이 있습니다.
triangles = [[(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
[(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)],
[(4, 48, 53), (4, 48, 52), (4, 48, 52)],
[(4, 48, 52), (4, 48, 53), (4, 48, 53)],
[(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
[(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)],
[(4, 48, 53), (4, 48, 52), (4, 48, 52)],
[(4, 48, 52), (4, 48, 53), (4, 48, 53)],
[(-4, 48, 52), (-4, 48, 52), (-4, 48, 53)],
[(-4, 48, 53), (-4, 48, 53), (-4, 48, 52)]]
삼각형의 모서리에 대한 3 개의 좌표 목록이 포함되어 있습니다. 이제 중복 삼각형을 모두 제거해야합니다. 그냥하는 list(set(triangles))
것은 효과가 없습니다. 다음 오류가 발생합니다.
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 96, in <module>
TypeError: unhashable type: 'list'
중복 삼각형을 제거하려면 어떻게합니까?
목록은 해시 할 수 없으므로 모든 것을 튜플으로 만듭니다.
list({tuple(sorted(x)) for x in triangles})
정점이 다른 순서에있는 경우 정렬이 있습니다. 다른 순서의 정점이 제거되어서는 안되는 경우 (또는 존재하지 않는 경우) 정렬을 제거하고 전체 map
를 map(tuple, triangles)
.
개별 요소를 다시 목록으로 만들려면 목록 이해를 사용하여 그렇게 만드십시오.
[list(x) for x in {tuple(sorted(x)) for x in triangles}]
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다