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
ss = 'anc'
s = 'b'
if(ss.find(s) > -1):
print(True)
print(ss.find('x'))
# -1
print(ss.index('x'))
# ValueError: substring not found ❌
pop
append
remove
count
sort
clear
copy
insert
extend
for...in
build in function
Python 内置函数
https://www.runoob.com/python/python-built-in-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, 禁止转载 🈲️,侵权必究⚠️!