2 回答

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
先壓平:
a = [['a', 's'],
['a', 's'],
['a', 's']]
print(set(y for x in a for y in x)) # {'a', 's'}
編輯:從更新的問題中,先將其轉(zhuǎn)換為元組,然后將最終輸出轉(zhuǎn)換為集合。請(qǐng)注意,集合并不總是像原始值那樣排列。
a = [['a', 's'],
['a', 'b'],
['a', 's']]
print([set(y) for y in set(tuple(x) for x in a)]) # [{'a', 's'}, {'a', 'b'}]

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
根據(jù)您的澄清評(píng)論,您顯然是在尋找不同的列表。
list對(duì)象在 Python 中不可散列,因?yàn)閺谋举|(zhì)上講,它們是可變的,并且可以通過更改數(shù)據(jù)來更改它們的散列碼。所以你想要/需要制作一個(gè)set可哈希對(duì)象。
a = [['a', 's'],
... ['a', 'b'],
... ['a', 's']]
>>> set(tuple(t) for t in a) # << unique tuples made of arrays in 'a'
{('a', 's'), ('a', 'b')}
>>> [list(x) for x in set(tuple(t) for t in a)] # << list of lists, will be unique by set(...)
[['a', 's'], ['a', 'b']]
>>> [set(x) for x in set(tuple(t) for t in a)] # << further, unique elements of the unique lists in a
[{'s', 'a'}, {'b', 'a'}]
但是由于散列問題,set您無法實(shí)現(xiàn)。lists
添加回答
舉報(bào)