日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 唯一元素_检查所有元素在Python中是否唯一

發(fā)布時(shí)間:2025/3/11 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 唯一元素_检查所有元素在Python中是否唯一 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python 唯一元素

Here, we are implementing a python program to check whether all elements of a list are unique or not?

在這里,我們正在實(shí)現(xiàn)一個(gè)python程序來檢查列表的所有元素是否唯一?

It's very simple to check, by following two steps

按照以下兩個(gè)步驟進(jìn)行檢查非常簡單

  • Convert the list in a set (as you should know that set contains the unique elements) – it will remove the duplicate elements if any.

    轉(zhuǎn)換列表中的列表(您應(yīng)該知道該集合包含唯一元素)–它將刪除重復(fù)的元素(如果有)。

  • Then, compare the length of the list and set – if both are the same then all elements are unique.

    然后,比較列表和集合的長度–如果兩者相同,則所有元素都是唯一的。

  • Program:

    程序:

    # function to check unique def check_unique(x):return len(x) == len(set(x))# lists x = [10, 20, 30, 40,50] y = [10, 20, 20, 20, 20] z = [10, 10, 10, 10, 10]print("x: ", x) print("len(x): ", len(x)) print("set(x): ", set(x)) print("len(set(x)): ", len(set(x))) print("check_unique(x): ", check_unique(x)) print()print("y: ", y) print("len(y): ", len(y)) print("set(y): ", set(y)) print("len(set(y)): ", len(set(y))) print("check_unique(y): ", check_unique(y)) print()print("z: ", z) print("len(z): ", len(z)) print("set(z): ", set(z)) print("len(set(z)): ", len(set(z))) print("check_unique(z): ", check_unique(z)) print()

    Output

    輸出量

    x: [10, 20, 30, 40, 50] len(x): 5 set(x): {40, 10, 50, 20, 30} len(set(x)): 5 check_unique(x): Truey: [10, 20, 20, 20, 20] len(y): 5 set(y): {10, 20} len(set(y)): 2 check_unique(y): Falsez: [10, 10, 10, 10, 10] len(z): 5 set(z): {10} len(set(z)): 1 check_unique(z): False

    翻譯自: https://www.includehelp.com/python/check-all-elements-are-unique-or-not.aspx

    python 唯一元素

    總結(jié)

    以上是生活随笔為你收集整理的python 唯一元素_检查所有元素在Python中是否唯一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。