Python list methods All In One
Python list methods All In One
Python 3
#!/usr/bin/env python3
# coding=utf-8
__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
"""
"""
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-08-17
*
* @description
* @augments
* @example
* @link
*
*/
"""
# python 模版
join
arr = ['a', 'b', 'c']
strs = ''.join(arr)
print(strs)
# abc
_strs = '-'.join(arr)
print(_strs)
# a_b_c
reverse
arr = ['a', 'b', 'c']
# reverse 没有返回新 list, 没有返回值 ✅ , None
# arr.reverse()
print(arr.reverse())
# None
print(arr)
# cba
string find
arr = ['a', 'b', 'c']
s = 'b'
# AttributeError: 'list' object has no attribute 'find' ❌
if(arr.find(s) > -1):
print(True)
print(arr.find('x'))
ss = 'anc'
s = 'b'
if(ss.find(s) > -1):
print(True)
print(ss.find('x'))
list & string index
arr = ['a', 'b', 'c']
s = 'b'
print(arr.index(s))
# 1
ss = 'anc'
s = 'b'
if(ss.find(s) > -1):
print(True)
print(ss.find('x'))
# -1
# find 比 index 更安全,index 如果找不到会报错 ❌
print(ss.index('x'))
# ValueError: substring not found ❌
pop
arr = ['a', 'b', 'c']
print(arr.pop())
# c
print(arr)
# ['a', 'b']
append
arr = ['a', 'b', 'c']
s = 'd'
print(arr.append(s))
# None
print(arr)
# ['a', 'b', 'c', 'd']
remove
arr = ['a', 'b', 'c']
s = 'b'
print(arr.remove(s))
# None
print(arr)
# ['a', 'c']
count
arr = ['a', 'b', 'c', 'b']
s = 'b'
print(arr.count(s))
# 2
print(arr.count())
# TypeError: count() takes exactly one argument (0 given) ❌
sort
strs = ['a', 'c', 'b', 'bb', 'aa', 'cc']
nums = [1, 3, 2, 21, 12, 11]
print(strs.sort())
# None
print(strs)
# ['a', 'aa', 'b', 'bb', 'c', 'cc']
print(nums.sort())
# None
print(nums)
# [1, 2, 3, 11, 12, 21]
clear
strs = ['a', 'c', 'b']
nums = [1, 3, 2]
print(strs.clear())
# None
print(strs)
# []
print(nums.clear())
# None
print(nums)
# []
copy
strs = ['a', 'c', 'b']
print(strs.copy())
# None
print(strs)
insert
strs = ['a', 'c', 'b']
s = 'd'
# print(strs.insert(s))
# TypeError: insert expected 2 arguments, got 1 ❌
# print(strs.insert(2, s))
print(strs.insert(len(strs), s))
# None
print(strs)
# ['a', 'c', 'd', 'b']
extend
strs = ['a', 'c', 'b']
s = 'd'
print(strs.extend(s))
# None
print(strs)
# ['a', 'c', 'b', 'd',]
print(strs.extend([s, s]))
# None
print(strs)
# ['a', 'c', 'b', 'd', 'd']
for...in
strs = ['a', 'c', 'b']
result = "";
for str in strs:
result += str + "=>";
print(result)
# a=>c=>b=>
Python 函数
https://www.runoob.com/python/python-functions.html
build in function
Python 内置函数
https://www.runoob.com/python/python-built-in-functions.html
https://www.runoob.com/python/python-functions.html
min & max
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78]
#your code goes here
smallest = min(data)
largest = max(data)
data.remove(smallest)
data.remove(largest)
total = 0;
for item in data:
total += item
print(total)
# print(sum(data))
sum
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78]
#your code goes here
# total = 0;
# for item in data:
# total += item
# print(total)
print(sum(data))
range
# 左闭右开 [begin, end), 步长 step
steps = list(range(0, 10, 3))
# [0, 3, 6, 9]
arr = range(10)
print(arr)
# range(0, 10)
slice
# class slice(stop)
# class slice(start, stop[, step])
# 设置截取5个元素的切片
myslice = slice(5)
print(myslice)
# slice(None, 5, None)
arr = range(10)
print(arr)
# range(0, 10)
# 截取 5 个元素
subArr = arr[myslice]
print(subArr)
# range(0, 5)
# list
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(nums[myslice])
# [0, 1, 2, 3, 4]
# print(nums.slice(5))
# AttributeError: 'list' object has no attribute 'slice'
print(nums[slice(5)])
# [0, 1, 2, 3, 4]
print(slice(nums, 5, 1))
# slice([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 1)
len
nums = [1, 3, 5, 2, 4]
print(len(nums))
# 5
refs
https://www.runoob.com/python/python-func-slice.html
https://www.programiz.com/python-programming/methods/string/join
https://www.runoob.com/python3/python3-list.html
https://stackoverflow.com/questions/493819/why-is-it-string-joinlist-instead-of-list-joinstring
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16599715.html
未经授权禁止转载,违者必究!