Hướng dẫn do sets have unique values python? - các bộ có giá trị duy nhất là python?

Nếu chúng ta cần giữ thứ tự các yếu tố, làm thế nào về điều này:

used = set[]
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = [x for x in mylist if x not in used and [used.add[x] or True]]

Và thêm một giải pháp sử dụng

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
1 và không có
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
2 var tạm thời.

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]

CẬP NHẬT - Tháng 12 năm 2020 - Có thể là cách tiếp cận tốt nhất!

Bắt đầu từ Python 3.7, Dict tiêu chuẩn bảo tồn thứ tự chèn.

Thay đổi trong phiên bản 3.7: Thứ tự từ điển được đảm bảo là thứ tự chèn. Hành vi này là một chi tiết thực hiện của CPython từ 3.6.

Vì vậy, điều này cho chúng ta khả năng sử dụng

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
3 để sao chép!

Lưu ý: Tín dụng được gửi tới @rlat vì đã cho chúng tôi cách tiếp cận này trong các bình luận!@rlat for giving us this approach in the comments!

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]

Về tốc độ - đối với tôi, nó đủ nhanh và đủ đọc để trở thành cách tiếp cận yêu thích mới của tôi!

Cập nhật - Tháng 3 năm 2019

Và một giải pháp thứ 3, là một giải pháp gọn gàng, nhưng loại chậm vì

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
4 là O [n].

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = [x for i, x in enumerate[mylist] if i == mylist.index[x]]

Cập nhật - Tháng 10 năm 2016

Một giải pháp khác với

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
1, nhưng lần này không có
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
6 làm cho nó dễ hiểu hơn và dễ hiểu hơn.

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]

Lưu ý: Hãy nhớ rằng chúng ta có thể đọc được nhiều hơn, kịch bản không phù hợp hơn. Ngoại trừ chỉ cho phương pháp

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
3 là Python 3.7+ cụ thể. Have in mind that more human-readable we get, more unperformant the script is. Except only for the
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
3 approach which is python 3.7+ specific.

import timeit

setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"

#10x to Michael for pointing out that we can get faster with set[]
timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
0.2029558869980974

timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
0.28999493700030143

# 10x to rlat for suggesting this approach!   
timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
0.31227896199925453

timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7149233570016804

timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7379565160008497

timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
0.7400134069976048

timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
0.9154880290006986

Trả lời ý kiến

Bởi vì @monica đã hỏi một câu hỏi hay về "Điều này hoạt động như thế nào?". Đối với tất cả mọi người có vấn đề tìm ra nó. Tôi sẽ cố gắng đưa ra một lời giải thích sâu sắc hơn về cách thức hoạt động của nó và những gì Sorcery đang xảy ra ở đây;]@monica asked a good question about "how is this working?". For everyone having problems figuring it out. I will try to give a more deep explanation about how this works and what sorcery is happening here ;]

Vì vậy, lần đầu tiên cô ấy hỏi:

Tôi cố gắng hiểu tại sao

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
8 không hoạt động.

Vâng, nó thực sự hoạt động

>>> used = []
>>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
>>> unique = [used.append[x] for x in mylist if x not in used]
>>> print used
[u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
>>> print unique
[None, None, None, None, None]

Vấn đề là chúng tôi chỉ không nhận được kết quả mong muốn bên trong biến

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
9, nhưng chỉ bên trong biến
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
2. Điều này là do trong danh sách hiểu
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
6 sửa đổi biến
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
2 và trả về
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
3.

Vì vậy, để đưa kết quả vào biến

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
9 và vẫn sử dụng cùng logic với
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
5, chúng ta cần di chuyển cuộc gọi
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
6 này ở phía bên phải của danh sách hiểu và chỉ trả lại
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
7 ở phía bên trái.

Nhưng nếu chúng ta quá ngây thơ và chỉ đi với:

>>> unique = [x for x in mylist if x not in used and used.append[x]]
>>> print unique
[]

Chúng tôi sẽ không nhận được gì để đáp lại.

Một lần nữa, điều này là do phương thức

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
6 trả về
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
3 và điều này mang lại cho biểu thức logic của chúng tôi cái nhìn sau đây:

x not in used and None

Điều này về cơ bản sẽ luôn luôn:

  1. Đánh giá là
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = [x for i, x in enumerate[mylist] if i == mylist.index[x]]
    
    0 khi
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = list[dict.fromkeys[mylist]]
    
    7 là trong
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
    
    2,
  2. Đánh giá thành
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = list[dict.fromkeys[mylist]]
    
    3 khi
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = list[dict.fromkeys[mylist]]
    
    7 không có trong
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
    
    2.

Và trong cả hai trường hợp [____ 30/________ 23], điều này sẽ được coi là giá trị

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = [x for i, x in enumerate[mylist] if i == mylist.index[x]]
8 và kết quả là chúng tôi sẽ nhận được một danh sách trống.

Nhưng tại sao điều này đánh giá là

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
3 khi
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
7 không có trong
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
2? Ai đó có thể hỏi.

Vâng, vì đây là cách các nhà khai thác ngắn mạch của Python hoạt động.

Biểu thức

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
2 đầu tiên đánh giá x; Nếu x là sai, giá trị của nó được trả về; Mặt khác, y được đánh giá và giá trị kết quả được trả về.

Vì vậy, khi

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
7 không được sử dụng [tức là khi
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
4], phần tiếp theo hoặc biểu thức sẽ được đánh giá [
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
5] và giá trị của nó [
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
3] sẽ được trả về.

Nhưng đó là những gì chúng tôi muốn để có được các yếu tố độc đáo từ một danh sách với các bản sao, chúng tôi muốn

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
6 chúng vào một danh sách mới chỉ khi chúng tôi bắt gặp trong một thời gian nắm đấm.

Vì vậy, chúng tôi thực sự muốn đánh giá

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
5 chỉ khi
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
7 không có trong
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
2, có lẽ nếu có cách để biến giá trị
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = list[dict.fromkeys[mylist]]
3 này thành
import timeit

setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"

#10x to Michael for pointing out that we can get faster with set[]
timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
0.2029558869980974

timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
0.28999493700030143

# 10x to rlat for suggesting this approach!   
timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
0.31227896199925453

timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7149233570016804

timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7379565160008497

timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
0.7400134069976048

timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
0.9154880290006986
2 chúng ta sẽ ổn, phải không?

Vâng, vâng và đây là nơi loại người vận hành

import timeit

setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"

#10x to Michael for pointing out that we can get faster with set[]
timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
0.2029558869980974

timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
0.28999493700030143

# 10x to rlat for suggesting this approach!   
timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
0.31227896199925453

timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7149233570016804

timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7379565160008497

timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
0.7400134069976048

timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
0.9154880290006986
3 thứ 2 đến chơi.

Biểu thức

import timeit

setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"

#10x to Michael for pointing out that we can get faster with set[]
timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
0.2029558869980974

timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
0.28999493700030143

# 10x to rlat for suggesting this approach!   
timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
0.31227896199925453

timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7149233570016804

timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7379565160008497

timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
0.7400134069976048

timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
0.9154880290006986
4 đầu tiên đánh giá x; Nếu x là đúng, giá trị của nó được trả về; Mặt khác, y được đánh giá và giá trị kết quả được trả về.

Chúng tôi biết rằng

import timeit

setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"

#10x to Michael for pointing out that we can get faster with set[]
timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
0.2029558869980974

timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
0.28999493700030143

# 10x to rlat for suggesting this approach!   
timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
0.31227896199925453

timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7149233570016804

timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7379565160008497

timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
0.7400134069976048

timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
0.9154880290006986
5 sẽ luôn là
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = [x for i, x in enumerate[mylist] if i == mylist.index[x]]
8, vì vậy nếu chúng tôi chỉ thêm một
import timeit

setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"

#10x to Michael for pointing out that we can get faster with set[]
timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
0.2029558869980974

timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
0.28999493700030143

# 10x to rlat for suggesting this approach!   
timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
0.31227896199925453

timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7149233570016804

timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
0.7379565160008497

timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
0.7400134069976048

timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
0.9154880290006986
7 bên cạnh anh ấy, chúng tôi sẽ luôn nhận được phần tiếp theo. Đó là lý do tại sao chúng tôi viết:

x not in used and [used.append[x] or True]

Vì vậy, chúng tôi có thể đánh giá

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
5 và kết quả là
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
4, chỉ khi phần đầu tiên của biểu thức
>>> used = []
>>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
>>> unique = [used.append[x] for x in mylist if x not in used]
>>> print used
[u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
>>> print unique
[None, None, None, None, None]
0 là
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
4.evaluate
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
5 and get
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
4 as a result, only when the first part of the expression
>>> used = []
>>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
>>> unique = [used.append[x] for x in mylist if x not in used]
>>> print used
[u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
>>> print unique
[None, None, None, None, None]
0 is
mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]
#which can also be writed as:
unique = reduce[lambda l, x: l if x in l else l+[x], mylist, []]
4.

Thời trang tương tự có thể được nhìn thấy theo cách tiếp cận thứ 2 với phương pháp

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
1.

mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
0

nơi chúng tôi:

  1. Nối lại
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = list[dict.fromkeys[mylist]]
    
    7 đến
    >>> used = []
    >>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    >>> unique = [used.append[x] for x in mylist if x not in used]
    >>> print used
    [u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
    >>> print unique
    [None, None, None, None, None]
    
    4 và trả lại
    >>> used = []
    >>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    >>> unique = [used.append[x] for x in mylist if x not in used]
    >>> print used
    [u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
    >>> print unique
    [None, None, None, None, None]
    
    4 khi
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = list[dict.fromkeys[mylist]]
    
    7 không có trong
    >>> used = []
    >>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    >>> unique = [used.append[x] for x in mylist if x not in used]
    >>> print used
    [u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
    >>> print unique
    [None, None, None, None, None]
    
    4. Nhờ tuyên bố
    import timeit
    
    setup = "mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']"
    
    #10x to Michael for pointing out that we can get faster with set[]
    timeit.timeit['[x for x in mylist if x not in used and [used.add[x] or True]]', setup='used = set[];'+setup]
    0.2029558869980974
    
    timeit.timeit['[x for x in mylist if x not in used and [used.append[x] or True]]', setup='used = [];'+setup]
    0.28999493700030143
    
    # 10x to rlat for suggesting this approach!   
    timeit.timeit['list[dict.fromkeys[mylist]]', setup=setup]
    0.31227896199925453
    
    timeit.timeit['reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
    0.7149233570016804
    
    timeit.timeit['reduce[lambda l, x: l+[x] if x not in l else l, mylist, []]', setup='from functools import reduce;'+setup]
    0.7379565160008497
    
    timeit.timeit['reduce[lambda l, x: l if x in l else l+[x], mylist, []]', setup='from functools import reduce;'+setup]
    0.7400134069976048
    
    timeit.timeit['[x for i, x in enumerate[mylist] if i == mylist.index[x]]', setup=setup]
    0.9154880290006986
    
    7
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = reduce[lambda l, x: l.append[x] or l if x not in l else l, mylist, []]
    
    6 được đánh giá và
    >>> used = []
    >>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    >>> unique = [used.append[x] for x in mylist if x not in used]
    >>> print used
    [u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
    >>> print unique
    [None, None, None, None, None]
    
    4 được trả lại sau đó.
  2. Trả lại
    >>> used = []
    >>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    >>> unique = [used.append[x] for x in mylist if x not in used]
    >>> print used
    [u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
    >>> print unique
    [None, None, None, None, None]
    
    4 không bị ảnh hưởng khi
    mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    unique = list[dict.fromkeys[mylist]]
    
    7 ở trong
    >>> used = []
    >>> mylist = [u'nowplaying', u'PBS', u'PBS', u'nowplaying', u'job', u'debate', u'thenandnow']
    >>> unique = [used.append[x] for x in mylist if x not in used]
    >>> print used
    [u'nowplaying', u'PBS', u'job', u'debate', u'thenandnow']
    >>> print unique
    [None, None, None, None, None]
    
    4

Là đặt các giá trị duy nhất python?

Sử dụng thuộc tính Set [] của Python, chúng ta có thể dễ dàng kiểm tra các giá trị duy nhất.Chèn các giá trị của danh sách trong một tập hợp.Đặt chỉ lưu trữ một giá trị một lần ngay cả khi nó được chèn nhiều lần.. Insert the values of the list in a set. Set only stores a value once even if it is inserted more than once.

Tập có có giá trị duy nhất không?

Bộ chỉ chứa các giá trị duy nhất.Chúng ta không thể có các giá trị trùng lặp trong một tập hợp.Nó chỉ giống như các tập hợp trong toán học.Nó đề cập đến các giá trị hoặc đúng hơn là dữ liệu chúng ta nhập, không phải là khóa.. we can't have duplicate values in a set. it's just same as sets in mathematics. it refers to values or rather data we enter, not the key.

Làm thế nào để bạn đếm các giá trị duy nhất trong một tập hợp trong Python?

Sử dụng Numpy để đếm các giá trị duy nhất trong danh sách Python..
Chúng tôi đã nhập Numpy dưới dạng NP và tạo một mảng bằng hàm mảng [] ..
Chúng tôi đã sử dụng hàm duy nhất [] từ Numpy để loại bỏ bất kỳ bản sao nào ..
Cuối cùng, chúng tôi đã tính độ dài của mảng đó ..

Bài Viết Liên Quan

Chủ Đề