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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python的userlist_Python Collections.UserList用法及代码示例

發布時間:2023/12/13 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python的userlist_Python Collections.UserList用法及代码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python列表是array-like數據結構,但與之不同的是它是同質的。單個列表可能包含數據類型,例如整數,字符串以及對象。 Python中的列表是有序的,并且有一定數量。根據確定的序列對列表中的元素進行索引,并使用0作為第一個索引來完成列表的索引。

注意:有關更多信息,請參閱Python列表。

Collections.UserList

Python支持一個List,如collections模塊中存在的名為UserList的容器。此類用作List對象的包裝器類。當一個人想要創建自己的具有某些修改功能或某些新功能的列表時,此類非常有用。它可以被視為為列表添加新行為的一種方式。此類將列表實例作為參數,并模擬保存在常規列表中的列表。該列表可通過此類的data屬性訪問。

用法:

collections.UserList([list])

范例1:

# Python program to demonstrate

# userlist

from collections import UserList

L = [1, 2, 3, 4]

# Creating a userlist

userL = UserList(L)

print(userL.data)

# Creating empty userlist

userL = UserList()

print(userL.data)

輸出:

[1, 2, 3, 4]

[]

范例2:

# Python program to demonstrate

# userlist

from collections import UserList

# Creating a List where

# deletion is not allowed

class MyList(UserList):

# Function to stop deleltion

# from List

def remove(self, s = None):

raise RuntimeError("Deletion not allowed")

# Function to stop pop from

# List

def pop(self, s = None):

raise RuntimeError("Deletion not allowed")

# Driver's code

L = MyList([1, 2, 3, 4])

print("Original List")

# Inserting to List"

L.append(5)

print("After Insertion")

print(L)

# Deliting From List

L.remove()

輸出:

Original List

After Insertion

[1, 2, 3, 4, 5]

Traceback (most recent call last):

File "/home/9399c9e865a7493dce58e88571472d23.py", line 33, in L.remove()

File "/home/9399c9e865a7493dce58e88571472d23.py", line 15, in remove

raise RuntimeError("Deletion not allowed")

RuntimeError:Deletion not allowed

總結

以上是生活随笔為你收集整理的python的userlist_Python Collections.UserList用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。