Python 基础知识(string、list、dict、set、lambda持续更新。。。)

1、string

#定义
str_1 = "Hello World!"
#find 查找指定字符串首次出现的索引,没有返回-1
find_index = str_1.find('a') 
print(find_index)#-1
#count 查找指定字符串出现的次数
print(str_1.count('l'))#3
#len 返回字符串长度
print(len(str_1))# 12
#join 以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
s1 = ","
seq = ("h", "e", "l", "l", "o")  # 字符串序列
print(s1.join(seq))#h,e,l,l,o
#replace 字符串替换
print(str_1.replace("l",'o'))#Heooo Worod!
print(str_1.replace("l",'o',1))#Heolo World!
#字符串切片(截取)
print(str_1[1:])#ello World!
print(str_1[:])#Hello World!
print(str_1[0:5])#Hello
print(str_1[:-1])  # Hello World
#是否包含指定字符串
print('H' in str_1)# True
print('h' in str_1)# False
print('h' not in str_1)# True
#字符串格式化
print("%s %s" % ('Hello', "World!"))#Hello World!

2、list

#定义
list_1 = ["111", "222", 333, 444]
list_d = []
#len 获取列表元素个数
print(len(list_1))#4
#append 追加元素
list_1.append(555)
print(list_1)#['111', '222', 333, 444, 555]
# insert 插入元素并且将其它元素往后移
list_d.insert(0, "2")
list_d.insert(1, "2")
list_d.insert(1, "555")
list_d.insert(0, "333")
print(list_d)#['333', '2', '555', '2']
#remove 移除指定元素的第一个元素
list_d.remove("2")
print(list_d)
#copy 拷贝数组
list_2=list_1.copy();
print(list_2)#['111', '222', 333, 444, 555]
#pop 移除元素,默认最后一个
print(list_1.pop())
print(list_1)#['111', '222', 333, 444]
print(list_2)#['111', '222', 333, 444, 555]
print(list_1.pop(-2))#333
print(list_1)#['111', '222', 444]
#count 获取指定元素的个数
print(list_1.count(444))#1
#分片
print(list_1[:])#['111', '222', 444]
print(list_1[:-2])#['111']
print(list_1[1:])#['222', 444]
#clear 清空列表
list_1.clear()
print(list_1)#[]

3、dict

#定义
dict_1 = {}
print(type(dict_1))#<class 'dict'>
#添加元素
dict_1["a"] = 111
dict_1["b"] = 111
dict_1["c"] = 111
print(dict_1)#{'a': 111, 'b': 111, 'c': 111}
#pop 移出指定元素
print(dict_1.pop("a"))#111
print(dict_1)#{'b': 111, 'c': 111}
#get 获取指定元素
print(dict_1.get("b"))#111
print(dict_1)#{'b': 111, 'c': 111}
#clear 清空
dict_1.clear()
print(dict_1)#{}
#遍历
for (k,v) in dict_1.items():
    print(k,v)
for d in dict_1.items():
    print(d)
for k in dict_1.keys():
    print(k,dict_1[k])    

4、Set
集合(set)是一个无序的不重复元素序列,区分大小写。可以使用大括号 { } 或者 set() 函数创建集合,创建一个空集合必须用 set() 而不是 { }, { }默认是用来创建一个空字典。

#创建一个set
set_1 = set()
set_2 = {'a', 'b'}
set_3 = set("Hello World")
print(set_3) #{'e', 'r', 'l', 'o', 'H', 'W', ' ', 'd'}
print(len(set_2)) #2
#add 添加元素
set_1.add('1')
#pop 随机移除元素
set_2.pop()
print(len(set_2))#1
#remove 移除元素,被移除元素不存在是报错
set_3.remove('l')
set_3.remove('H')
print(set_3)#{'d', 'o', 'e', 'W', 'r', ' '}
#discard 删除指定元素,被移除元素不存在不会报错
set_3.discard('e')
set_3.discard('cc')
print(set_3)#{'d', 'W', 'o', 'r', ' '}

5、lambda
lambda表达式通常是在需要一个函数,但是又不想去命名定义函数的场合下使用,也就是指匿名函数。
语法:lambda argument_list: expression

# 无参匿名函数
fun1 = lambda: True;
print(fun1());

s = "Hello\n\tWorld!"
print(s)
fun2 = lambda s: ' '.join(s.split());
print(fun2(s))  # Hello World!
# 带参数的匿名函数
add = lambda x, y: x + y
print(add(1, 2))  # 结果为3
# 参数有默认值的匿名函数
fun3 = lambda x, y=3: x * y
print(fun3(5))  # 15

fun4 = lambda *x: x  # *x返回的是一个元祖
print(fun4("a", "b"))  # ('a', 'b')

fun5 = lambda **x: x  # **x返回的是一个字典
print(fun5())
# 参数直接跟在lambda表达式后面
print((lambda x, y: x if x > y else y)(1, 2))  # 2


def f1(n):
    c = 1
    for i in range(1, n + 1):
        c = c * i
    return c


def f2(n):
    if n > 1:
        return n * f2(n - 1)
    else:
        return 1


from functools import reduce

# 计算阶乘
print(f2(6))
print(f1(6))
print(reduce(lambda x, y: x * y, range(1, 6 + 1)))

list1 = [1, 5, -4, -2, 0, -3, 6]
# lambda 结合 sorted使用
print(sorted(list1, key=lambda x: abs(x)))

# lambda 结合 filter使用
print(list(filter(lambda x: True if x % 3 == 0 else False, range(100))))
#列表推导式
print([x for x in range(100) if x % 3 == 0])

# lambda 结合 map使用
print(list(map(lambda x: True if x % 3 == 0 else False, range(100))))

str1 = "abcde"
str2 = "abcdefg"
print(list(zip(str1, str2)))

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值