首页 > 编程语言 >python总结

python总结

时间:2022-11-08 11:25:31浏览次数:38  
标签:总结 python s2 s1 l1 print abcdef d1

#字符串的合并
s1 = "abc"
s2 = "def"
s = s1 + s2
print(s)#"abcdef"
s = "".join([s1,s2]) #前提:序列里面的元素必须是字符串
print(s)#"abcdef"
from functools import reduce #累积
s = reduce(lambda x,y:x+y,[s1,s2])
print(s)#"abcdef"

#列表的合并
l1 = [1,2,3]
l2 = [4,5,6]
l = l1 + l2
print(l)#[1, 2, 3, 4, 5, 6]
l = l1.extend(l2)
print(l1)#[1, 2, 3, 4, 5, 6] 注意是l1列表扩充了,不是l

#元组的合并
t1 = (1,2,3)
t2 = (4,5,6)
t = t1 + t2
print(t)#(1, 2, 3, 4, 5, 6)

#字典的合并,没有+
d1 = {1:1,2:2}
d2 = {3:3,4:4}
d = d1.update(d2)
print(d1)#{1: 1, 2: 2, 3: 3, 4: 4} 注意是d1扩充了,不是d

#集合 没有+
s1 = set([1,2,3])
s2 = set([4,5,6])
s = s1.union(s2)
print(s)#{1, 2, 3, 4, 5, 6} 注意是s扩充了
s = s1 | s2
print(s)#{1, 2, 3, 4, 5, 6}

标签:总结,python,s2,s1,l1,print,abcdef,d1
From: https://www.cnblogs.com/mlping/p/16869035.html

相关文章