1.join(),可以对str、lsit、tuple、dict用,但是不能对int,且值不能时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
----------------------------------------------------------------------------------------------------------------
5.Python字典中的值为列表或字典的构造方法
https://blog.csdn.net/buside/article/details/81323871
dic = {}
dic.setdefault(key,[]).append(value)
*********示例如下******
In [1]: dic = {}
In [2]: dic.setdefault('a',[]).append(1)
In [3]: dic.setdefault('a',[]).append(2)
In [4]: dic
Out[4]: {'a': [1, 2]}
----------------------------------------------------------------------------------------------------------------
6.Python中字符串(此时字符串里面为json字符串)转换成字典,eval()和 json.loads()
s_str = '{"ip": "175.155.140.119"}'
s_dict = eval(s_str)
s_dict = json.loads(s_str)
----------------------------------------------------------------------------------------------------------------
7.getattr(object, name[, default])、importlib.import_module()用法
class Test(object):
def run(self):
print("hello world")
test = Test()
ret = getattr(test, "run") # 第一个参数需要实例化对象
print(ret) # <bound method Test.run of <__main__.Test object at 0x0000029E9514D7B8>>
ret() # hello world
import importlib
path = "test1"
ret1 = importlib.import_module(path)
print(ret1) # <module 'test1' from 'E:\\pachong\\test1.py'>
cls = getattr(ret1, "ProcessNum")
print(cls) # <class 'test1.ProcessNum'>
ret = getattr(cls(), "get")
print(ret) # <bound method ProcessNum.get of <test1.ProcessNum object at 0x000002B8E0431BE0>>
ret() # get num
----------------------------------------------------------------------------------------------------------------
8.python判断变量是否为int,str,list,tuple,dict
isinstance(a,int) # 成立返回True
isinstance(a,str)
isinstance(a,list)
isinstance(a,tuple)
isinstance(a,dict)