python函数例子_Python函数的一些例子,python,使用,示例

目录

zip的

使用示例

a = [1, 2, 3]

b = [11, 12, 13]

c = [21, 22, 23, 24]

zip_a_b = zip(a, b)

print(list(zip_a_b))

# 输出

[(1, 11), (2, 12), (3, 13)]

zip_a_b = zip(a, b)

print(dict(zip_a_b))

# 输出

{1: 11, 2: 12, 3: 13}

zip_a_b = zip(a, b)

for i in zip_a_b:

print(i)

# 输出

(1, 11)

(2, 12)

(3, 13)

# 元素个数与最短的列表一致

zip_a_c = zip(a, c)

print(list(zip_a_c))

# 输出

[(1, 21), (2, 22), (3, 23)]

datetime的使用示例

date_example = datetime.date.today()

# 返回年

print(date_example.year)

# 返回月

print(date_example.month)

# 返回日

print(date_example.day)

# 返回周几

print(date_example.weekday())

# 返回指定格式的日期字符串

print(date_example.strftime('%Y/%m/%d'))

pandas.Series.isin的使用示例

Series.isin(self, values)

# Whether each element in the DataFrame is contained in values.

data

A B C D

0 0 1 2 3

1 4 5 6 7

2 8 9 10 11

data[data['A'].isin([4,8])]

# 返回满足{A列含有数值[4,8]}条件的值

A B C D

1 4 5 6 7

2 8 9 10 11

data[~data['A'].isin([4,8])]

# ~逆函数,返回不满足{A列含有数值[4,8]}的值

A B C D

0 0 1 2 3

str.format( ) 的使用示例

# 按默认顺序

"{} {}".format("hello","world")

# 输出

'hello world'

# 指定位置

"{1} {0} {1}".format("hello","world")

# 输出

'world hello world'

# 指定参数

"网站名:{name}, 地址:{url}".format(name = "菜鸟教程", url = "www.runoob.com")

# 输出

'网站名:菜鸟教程, 地址:www.runoob.com'

map的使用示例

# 根据提供的函数对指定序列做映射,python3返回的是迭代器

map(function, iterable)

# 计算平方数的函数

def square(x) :

return x ** 2

map(square,[1,2,3,4,5])

# 输出

list(map(square,[1,2,3,4,5]))

# 输出

[1, 4, 9, 16, 25]

# 支持lambda匿名函数

map(lambda x: x ** 2, [1, 2, 3, 4, 5])

# 提供了两个列表,对相同位置的列表数据进行相加

map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

list.count()的使用示例

# list.count(obj) 统计某个元素在列表中出现的次数

aList = [123, 'xyz', 'zara', 'abc', 123]

print(aList.count(123))

# 输出

2

pandas.to_datetime的使用示例

# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.to_datetime.html

# pandas.to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False, utc=None, format=None, exact=True, unit=None, infer_datetime_format=False, origin='unix', cache=True)

import pandas as pd

dates = ['2017-01-05', 'Jan 5, 2017', '01/05/2017', '2017.01.05', '2017/01/05','20170105']

pd.to_datetime(dates)

输出:

DatetimeIndex(['2017-01-05', '2017-01-05', '2017-01-05', '2017-01-05',

'2017-01-05', '2017-01-05'],

dtype='datetime64[ns]', freq=None)

end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值