Mục lục bài viết
Update: 2022-03-03 06:16:13,Bạn Cần biết về How remove all duplicates from a list in Java?. Bạn trọn vẹn có thể lại phản hồi ở phía dưới để Admin đc lý giải rõ ràng hơn.
Previous
Next
Learn how to remove duplicates from a List in Python.
Tóm lược đại ý quan trọng trong bài
Remove any duplicates from a List:
mylist = [“a”, “b”, “a”, “c”, “c”]
mylist = list(dict.fromkeys(mylist))
print(mylist)
Try it Yourself »
First we have a List that contains duplicates:
mylist = [“a”, “b”, “a”, “c”, “c”]
mylist = list(dict.fromkeys(mylist))
print(mylist)
Create a dictionary,
using the List items as keys. This will automatically remove any duplicates
because dictionaries cannot have duplicate keys.
mylist = [“a”, “b”, “a”, “c”, “c”]
mylist = list(dict.fromkeys(mylist))
print(mylist)
Then, convert the dictionary back into a list:
mylist = [“a”, “b”, “a”, “c”, “c”]
mylist = list(dict.fromkeys(mylist))
print(mylist)
Now we have a List without any duplicates, and it has the same order as the
original List.
Print the List to demonstrate the result
mylist = [“a”, “b”, “a”, “c”, “c”]
mylist = list(dict.fromkeys(mylist))
print(mylist)
If you like to have a function where you can send your lists, and get them
back without duplicates, you can create a function and insert the code from the
example above.
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Try it Yourself »
Create a function that takes a List as an argument.
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Create a dictionary, using this List items as keys.
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Convert the dictionary into a list.
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Return the list
def my_function(x):
return list(dict.fromkeys(x))
mylist =
my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Call the function, with a list as a parameter:
def my_function(x):
return list(dict.fromkeys(x))
mylist = my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Print the result:
def my_function(x):
return list(dict.fromkeys(x))
mylist = my_function([“a”, “b”, “a”, “c”, “c”])
print(mylist)
Previous
Next
Reply
0
0
Chia sẻ
– Một số Keywords tìm kiếm nhiều : ” đoạn Clip hướng dẫn How remove all duplicates from a list in Java? tiên tiến và phát triển nhất , Chia Sẻ Link Download How remove all duplicates from a list in Java? “.
Bạn trọn vẹn có thể để lại phản hồi nếu gặp yếu tố chưa hiểu nha.
#remove #duplicates #list #Java How remove all duplicates from a list in Java?