首页 飞桨领航团 帖子详情
AI达人养成营——函数总结
收藏
快速回复
飞桨领航团 文章AI达人创造营 304 0
AI达人养成营——函数总结
收藏
快速回复
飞桨领航团 文章AI达人创造营 304 0

列 表
insert 插入:
list1.insert(0#位置,1#插入的元素)
count 计数(出现的次数):
list1.count('4')
append()增加列表的元素
list1.append(5)#在末尾增加数字5
extend() 将列表放在末尾:
list1.extend(list2)
pop() 弹出指定位置:
list1.pop(0)
split 分裂(将字符串分成列表)
u='www.dadsda.com'
c=u.split('.'#以.为分割点)
平时分割就是u.split()
sorted组织好的(按从小到大顺序排列)
sorted(list1)
reversed(1.随机打乱2.从大到小排列)
1.for i in reversed(list1)
2.sorted(list1,reverse=True)
字 符 串
find
info = 'abca'
print info.find('a') # 从下标0开始,查找在字符串里第一个出现的子串,返回结果:0
0
3
print info.find('3') # 查找不到返回-1
-1
replace(代替)
string1.replace('old ','new''[max]#最多替换不超过max次)
upper(小写转大写)、lowper(大写转小写)
str = "this is string example....wow!!!";
print ( str.upper())
capitalize() 将字符串的第一个字母变成大写,其他字母变小写
>>>s = 'a, B'
>>> s.capitalize()
'A, b'
定义
global(全局的):将变量名定义在全局
def change_my_name():
global name
print('我的名字曾经是', name)
name = '李四'
print('我的名字现在是', name)
name = '张三'
change_my_name()
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
>>> def square(x) : # 计算平方数
... return x ** 2
...
>>> map(square, [1,2,3,4,5]) # 计算列表各个元素的平方
# 返回迭代器
>>> list(map(square, [1,2,3,4,5])) # 使用 list() 转换为列表
[1, 4, 9, 16, 25]
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5])) # 使用 lambda 匿名函数
[1, 4, 9, 16, 25]

0
收藏
回复
在@后输入用户全名并按空格结束,可艾特全站任一用户