Python 목록에서 중복을 제거하는 방법
Python의 목록에서 중복을 제거하는 방법을 배우십시오.
예시
목록에서 중복 제거:
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
예시 설명
먼저 중복을 포함하는 목록이 있습니다.
중복 목록
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
목록 항목을 키로 사용하여 사전을 만듭니다. 사전은 중복 키를 가질 수 없기 때문에 자동으로 중복을 제거합니다.
사전 만들기
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
그런 다음 사전을 다시 목록으로 변환합니다.
목록으로 변환
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
이제 중복이 없는 목록이 있으며 원래 목록과 동일한 순서를 갖습니다.
결과를 보여주기 위해 목록을 인쇄하십시오
목록 인쇄
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)
함수 생성
목록을 보내고 중복 없이 다시 가져올 수 있는 기능을 원하면 함수를 만들고 위의 예에서 코드를 삽입할 수 있습니다.
예시
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function(["a", "b", "a", "c", "c"])
print(mylist)
예시 설명
List를 인수로 사용하는 함수를 만듭니다.
함수 생성
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function(["a", "b", "a", "c", "c"])
print(mylist)
이 목록 항목을 키로 사용하여 사전을 만듭니다.
사전 만들기
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function(["a", "b", "a", "c", "c"])
print(mylist)
사전을 목록으로 변환합니다.
목록으로 변환
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function(["a", "b", "a", "c", "c"])
print(mylist)
목록 반환
반품 목록
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function(["a", "b", "a", "c", "c"])
print(mylist)
목록을 매개변수로 사용하여 함수를 호출합니다.
함수 호출
def my_function(x):
return list(dict.fromkeys(x))
mylist = my_function(["a", "b", "a", "c", "c"])
print(mylist)
결과 인쇄:
결과 인쇄
def my_function(x):
return list(dict.fromkeys(x))
mylist = my_function(["a", "b", "a", "c", "c"])
print(mylist)