一、 join()方法

    用来作字符串的分隔操作,join()方法只能传一个参数,参数为字符串,列表,元组或者字典,返回结果为通过逗号分隔的新的字符串。

   例, 参数为列表,迭代器为列表

str_in = '(' + ','.join(['%s'] * 3) + ')'
print(str_in)

二、 type()和isinstance()方法

           type() 方法用来变量的类型是什么,比如 a=2, type(a) 的值为:  <class 'int'>, 可以通过转字符串的形式来判断变量的类型

       1. tpye()方法主要有如下常用的6种标准数据类型:         

类型描述
int ,   <class 'int'>   数字
string , <class 'str'> 字符串
[],   <class 'list'> 列表
  {}, <class 'dict'> 字典
(), <class 'tuple'> 元组
{1,2,3} , <class 'set'>集合类型

        

# 测试type()方法
a = 2
print(type(a))
a = "2"
print(type(a))
a = []
print(type(a))
a = {}
print(type(a))
a = ()
print(type(a))
a = 2.2
print(type(a))
a = {1, 2, 3, 4}
print(type(a))

         打印结果为: 

<class 'int'>
<class 'str'>
<class 'list'>
<class 'dict'>
<class 'tuple'>
<class 'float'>
<class 'set'>

       2. 在判断类型时,可以使用isinstance(arg,type)函数来进行判断,两者的区别是:     

           type()不会认为子类是一种父类类型。

           isinstance()会认为子类是一种父类类型。

三、eval()函数

      eval()函数可以将符合元组、列表、字典结构的字符串,转换成列表、元组或者字典。

四、常用字典函数

dict_data = {"age": 23, "name": "bingbing"}
# 1. items()方法,返回字典的所有键值对,以列表的形式返回可遍历的元组数组
items = dict_data.items()
for i in items:
    print(i)

# 2. key in dict 判断键是否在字典里
if "age" in dict_data:
    print("age is in dict")
else:
    print("age is not in dict")

# 3. 可以直接根据键来拿到对应的值
print(dict_data["age"])

# 4. keys()方法,返回该字典的键列表,包含该字典内的所有键
print(dict_data.keys())

# 5. get(key,default=None)返回指定的key,如果key不存在,那么返回default值。
print(dict_data.get("address", "不存在!"))

# 6. setdefault(key,default=None)设置key的默认值,如果key不存在,那么设置的值为default的值
print(dict_data.setdefault("address", "上海浦东新区"))
print(dict_data)

# 7.values()方法, values方法,返回一个迭代器,可以用list转换为值列表
print(dict_data.values())
#  --> 列表
print(list(dict_data.values()))

# 8. pop(key[,default])方法, 返回被删除的key值,如果指定的key值不存在,那么返回设定的default的值。
pop_result = dict_data.pop("add", "666")
print("pop_result", pop_result, ",dict_data", dict_data)

# 9. popitem()方法,随机返回字典的最后一个键值对。
pop_item = dict_data.popitem()
print("pop_item", pop_item, ",dict_data", dict_data)

# 10. .fromkeys()方法,根据元组序列来生成一个字典
tuple_data = ("name", "age", "address")
print("根据元组生成字典:", dict.fromkeys(tuple_data))

打印结果:

D:\pythonWorkspace\django-study\mysite\venv\Scripts\python.exe D:/pythonWorkspace/django-study/mysite/python-study/dict-demo.py
('age', 23)
('name', 'bingbing')
age is in dict
23
dict_keys(['age', 'name'])
不存在!
上海浦东新区
{'age': 23, 'name': 'bingbing', 'address': '上海浦东新区'}
dict_values([23, 'bingbing', '上海浦东新区'])
[23, 'bingbing', '上海浦东新区']
pop_result 666 ,dict_data {'age': 23, 'name': 'bingbing', 'address': '上海浦东新区'}
pop_item ('address', '上海浦东新区') ,dict_data {'age': 23, 'name': 'bingbing'}
根据元组生成字典: {'name': None, 'age': None, 'address': None}

Process finished with exit code 0

五、日期函数

          1. 使用strftime()格式化datetime获取的时间,最后返回的格式为字符串类型。

import datetime

data_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(data_time)

       2. 使用datetime.datetime.strptime(str,"%Y-%m-%d %H:%M:%S") 将字符串时间转换为datetime格式的时间
 

import datetime

data_time = datetime.datetime.strptime("2020-12-12","%Y-%m-%d %H:%M:%S")
print(data_time)

六、高级内置函数

1. lambda表达式的使用,一般函数都可以转换为Lambda表达式
   y= lambda x:x+2
   y(2)
   打印结果: 4

2.map object的使用
   1) map(function,iterator),参数1为函数,参数2为可遍历的迭代器
   2) 可以直接使用list(map(function,iterator)) 将返回的map对象转换为list列


# map函数的使用
a = [1, 2, 3, 4]


def func(x):
    print(x)
    return x + 1


res = map(func, a)
# 参数1为function,参数2为列表或其他迭代器
for i in res:
    print(i)

# map转换为list,将返回的结果转换为列表的形式
res = list(map(func, a))
print(res)
# 打印结果: [2, 3, 4, 5]

3.for 循环遍历
  1) 直接 for i in list: print(i)
  2) 格式化遍历 for (x,y) in [{x1,y1},{x2,y2}]
  3) 带条件的生成器 [x for x in list if x >5]


schedule = [(echo, "apple"), (echo, "banana")]

for (func, args) in schedule:
    func(args)


4. filter 用法,筛选出列表中满足条件的值, 使用list()方法将filter()筛选的结果返回
   例如, 使用filter筛选出(-5,5)之间的所有条件满足大于0的数
   res=list(filter(lambda x:x>0,range(-5,5)))


# filter的使用
# 1.使用filter挑选出序列[-5,5]中大于0的数
res = list(filter(lambda x: x > 0, range(-5, 5)))
print(res)
# 打印结果[1,2,3,4]

5. reduce 用法
   reduce是在functools 类里, 用来计算一个列表中所有元素加起来的和或所有元素的乘积
   比如,计算一个列表中所有元素和
   from functools import reduce
   res = reduce(lambda x, y: x + y, a)


# reduce的用法
from functools import reduce

a = [1, 2, 3, 4, 5]
res = reduce(lambda x, y: x + y, a)
print(res)

七、格式化函数

      1.  .format()函数

          "{}/{}/{}".format(1,2,3)    -->  1/2/3

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐