python中join,split,strip,eval,getattr,字典的值为列表,判断两个列表包含关系,python判断变量是否为int,str,list,tuple,dict

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值