python列表基础操作

目录

初始化两个列表

列表长度  len()

索引 index()

统计 count()

判断对象是否存在  in , not in

添加

append(),

extend(),

insert()

删除

        del

        pop()

        remove()

`        clear()

修改 : indedx, sort()

复制 : copy()


初始化两个列表

Test_list = ["Python", "Java", "Go", "C", "C++", "Scala", "PHP", "Vue"]
score = [1, 5, 6, 8, 6, 9, 8, 5, 3, 7, 6, 5, 1, 0, 8, 9, 5, 1, 6, 3]

列表长度  len()

"""
    长度 
        def len(__obj: Sized) -> int
        def __init__(self, o: object) -> None
        
        __len__() : Return len(self)
"""
lenNum = score.__len__()
print("lenNum : ", lenNum, "\nlen : ", len(score))

print(type(Test_list), "\tlen : ", len(Test_list))
lenNum :  20 
len :  20
<class 'list'> 	len :  8

索引 index()

"""
    索引 
    def index(self,
          __value: _T,
          __start: int = ...,
          __stop: int = ...) -> int
    返回下标值
    未查询成功会报错
"""
PythonIndex = Test_list.index("Python", 0, len(Test_list))

print("PythonIndex : ", PythonIndex)

print("Test_list[PythonIndex]", Test_list[PythonIndex])
PythonIndex :  0
Test_list[PythonIndex] Python

统计 count()

"""
    统计 
    def count(self, __value: _T) -> int
    统计指定对象出现的次数
"""


count03 = score.count(3)
count05 = score.count(5)
print("count03 : ", count03)
print("count05 : ", count05)
count03 :  2
count05 :  4

判断对象是否存在  in , not in

"""
    判断存在
    in  
    not in 
"""
print("Python" in Test_list)
print("java" in Test_list)
print("Scala" not in Test_list)
True
False
False

添加

append(),

extend(),

insert()

"""
    增加
        def append(self, __object: _T) -> None
        在末尾增加一个对象
        
        def extend(self, __iterable: Iterable[_T]) -> None
        分裂序列追加到末尾
        
        def insert(self,
           __index: int,
           __object: _T) -> None
        指定下标位置增加对象
    
"""
Test_list02 = ["Css", "Sql"]
Test_list.append("JavaScript")
Test_list.extend(Test_list02)
Test_list.insert(2, "Lua")
print(Test_list, "\n", len(Test_list))
['Python', 'Java', 'Lua', 'Go', 'C', 'C++', 'Scala', 'PHP', 'Vue', 'JavaScript', 'Css', 'Sql'] 
 12

删除

        del

        pop()

        remove()

`        clear()

"""
    删除
        del
        去除列表所有值与定义
        
        def pop(self, __index: int = ...) -> _T
        删除列表指定下标的对象默认是0
        
        def remove(self, __value: _T) -> None
        移除列表中指定数据的第⼀个匹配项。
        
        def clear(self) -> None
        清空列表
"""
del Test_list02
try:
    # name 'Test_list02' is not defined
    print(Test_list02)
except Exception as e:
    print(e)

Test_list.pop(0)
print("after pop : ", Test_list)

Test_list.remove("Sql")
print("after remove : ", Test_list)

Test_list.clear()
print("after clear : ", Test_list)
name 'Test_list02' is not defined
after pop :  ['Java', 'Lua', 'Go', 'C', 'C++', 'Scala', 'PHP', 'Vue', 'JavaScript', 'Css', 'Sql']
after remove :  ['Java', 'Lua', 'Go', 'C', 'C++', 'Scala', 'PHP', 'Vue', 'JavaScript', 'Css']
after clear :  []

修改 : indedx, sort()


"""
    修改 
        通过下标修改内容
        
        逆置
        
        @overload 
        def sort(self: List[SupportsLessThanT],*,
         key: None = ...,
         reverse: bool = ...) -> Non
         
         >reverse: bool => True : 降序, False : 升序
         
"""

print("Original data : ", score)
score[0] = 0
print("after amend : ", score)

score.reverse()
print("after reverse : ", score)

score.sort(key=None, reverse=True)
print("after score : ", score)
Original data :  [1, 5, 6, 8, 6, 9, 8, 5, 3, 7, 6, 5, 1, 0, 8, 9, 5, 1, 6, 3]
after amend :  [0, 5, 6, 8, 6, 9, 8, 5, 3, 7, 6, 5, 1, 0, 8, 9, 5, 1, 6, 3]
after reverse :  [3, 6, 1, 5, 9, 8, 0, 1, 5, 6, 7, 3, 5, 8, 9, 6, 8, 6, 5, 0]
after score :  [9, 9, 8, 8, 8, 7, 6, 6, 6, 6, 5, 5, 5, 5, 3, 3, 1, 1, 0, 0]

复制 : copy()

"""
    复制 
    def copy(self) -> List[_T]
"""
score_c = score.copy()
print("score_c : ", score_c)
score_c :  [9, 9, 8, 8, 8, 7, 6, 6, 6, 6, 5, 5, 5, 5, 3, 3, 1, 1, 0, 0]

二维列表

"""
    二维列表
"""
Two_dimensional = []
score_c.sort(key=None, reverse=False)
for i in zip(score, score_c):
    print(i,  end=",")
    Two_dimensional.append(list(i))
print("\n", Two_dimensional)
(9, 0),(9, 0),(8, 1),(8, 1),(8, 3),(7, 3),(6, 5),(6, 5),(6, 5),(6, 5),(5, 6),(5, 6),(5, 6),(5, 6),(3, 7),(3, 8),(1, 8),(1, 8),(0, 9),(0, 9),
 [[9, 0], [9, 0], [8, 1], [8, 1], [8, 3], [7, 3], [6, 5], [6, 5], [6, 5], [6, 5], [5, 6], [5, 6], [5, 6], [5, 6], [3, 7], [3, 8], [1, 8], [1, 8], [0, 9], [0, 9]]

*以上内容仅供参考

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值