python中函数join(),split(),strip()
程序员文章站
2023-12-21 14:06:40
...
1.join(),可以对str、lsit、tuple、dict用,但是不能对int,
str.join(sequence)
t1 = ("a", "b", "c")
s1 = "abc"
l1 = ["a", "b", "c"]
d1 = {"a": 1, "b": 2, "c": 3}
n1 = 123456
print(",".join(t1), type(",".join(t1)))
print(",".join(s1), type(",".join(s1)))
print(",".join(l1), type(",".join(l1)))
print(",".join(d1), type(",".join(d1)))
print(",".join(n1), type(",".join(n1)))
a,b,c <class 'str'>
a,b,c <class 'str'>
a,b,c <class 'str'>
a,b,c <class 'str'>
TypeError: can only join an iterable
----------------------------------------------------------------------------------------------------------------
2.split()分割
str.split('第一个参数为以什么分割', '第二个参数是分割多少次,不写默认全部分割' )
输出结果为列表
----------------------------------------------------------------------------------------------------------------
3.stript()去除字符串头尾的指定字符,lstript()去除字符串头的指定字符,rstript()去除字符串尾的指定字符,不写默认去除空格
str.strip([chars]);
str.lstrip([chars]);
str.rstrip([chars]);
----------------------------------------------------------------------------------------------------------------
4.判断一个列表中的每个元素是不是另一个列表中的元素
In [2]: a = [1, 2]
In [3]: b = [1, 2, 3]
In [4]: set(a) < set(b)
Out[4]: True
----------------------------------------------------------------------------------------------------------------
4.Python字典中的值为列表或字典的构造方法
https://blog.csdn.net/buside/article/details/81323871
dic = {}
dic.setdefault(key,[]).append(value)
*********示例如下******
>>dic.setdefault('a',[]).append(1)
>>dic.setdefault('a',[]).append(2)
>>dic