大家好,小编来为大家解答以下问题,python简单代码编程 贪吃蛇,python简单代码画图表白,今天让我们一起来看看吧!
在当今数字化时代,Python语言因易懂、易维护、具有丰富的功能函数等特点,被广泛应用于各行各业中。
"冰冻三尺非一日之寒,水滴石穿非一日之功",想学好一门语言最重要的方法就是日常积累和实践,今天这篇文章将给大家分享一些看似简单,日常工作中却非常实用的函数及技巧python四瓣花怎么画。
1. argpartition()
借助于 argpartition(),Numpy 可以找出 N 个最大数值的索引,也会将找到的这些索引输出。然后我们根据需要对数值进行排序。
# argpartition()
import numpy as np
x = np.array([11, 10, 15, 1, 0, 7, 8, 6, 9, 15, 16, 5, 4, 6, 0])
index_val = np.argpartition(x, -4)[-4:]
print(np.sort(x[index_val]))
[11 15 15 16]
2. clip()
clip() 使得一个数组中的数值保持在一个区间内。有时,我们需要保证数值在上下限范围内。为此,我们可以借助 Numpy 的 clip() 函数实现该目的。给定一个区间,则区间外的数值被剪切至区间上下限(interval edge)。
import numpy as np
x = np.array([11, 10, 15, 1, 0, 7, 8, 6, 9, 15, 16, 5, 4, 6, 0])
y = np.clip(x, 4, 10)
print(y)
[10 10 10 4 4 7 8 6 9 10 10 5 4 6 4]
3. zip()
用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。
afc = ['I', '人工智能']
nfc = ['python', '机器学习']
for teama, teamb in zip(afc, nfc):
print(teama + '与' + teamb)
I与python 人工智能与机器学习
4. 列表推导式
已知一个列表,我们可以刷选出偶数列表方法:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even = []
for number in numbers:
if number%2 == 0:
even.append(number)
print(even)
转变如下:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even = [number for number in numbers if number%2 == 0]
print(even)
[2, 4, 6, 8]
和列表推导类似,字典可以做同样的工作:
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
teams = ['Packers', '49ers', 'Ravens', 'Patriots']
print({key: value for value, key in enumerate(teams)})
{'Packers': 0, '49ers': 1, 'Ravens': 2, 'Patriots': 3}
5. 字符串合并与拆解
字符串合并
list = ['I', 'love', 'Python与人工智能']
print(' '.join(list))
字符串拆解
list = 'I love Python与人工智能'
print(list.split(' '))
['I', 'love', 'Python与人工智能']
6. 字典的合并
dict1 = {'a':1, 'b':2}
dict2 = {'c':3, 'd':4}
combine_dict = {**dict1, **dict2}
print('-------方法1--------')
print(combine_dict)
print('-------方法2--------')
dict1.update(dict2)
print(dict1)
-------方法1-------- {'a': 1, 'b': 2, 'c': 3, 'd': 4} -------方法2-------- {'a': 1, 'b': 2, 'c': 3, 'd': 4}
7. where()
where() 用于从一个数组中返回满足特定条件的元素。比如,它会返回满足特定条件的数值的索引位置。where() 与 SQL 中使用的 where condition 类似,如以下示例所示:
import numpy as np
y = np.array([11, 10, 15, 1, 0, 7, 8, 6, 9, 15, 16, 5, 4, 6, 0])
print(np.where(y > 5))
(array([ 0, 1, 2, 5, 6, 7, 8, 9, 10, 13], dtype=int64),)
8. eval()
eval(str_expression)作用是将字符串转换成表达式,并且执行。
a = eval('[1, 2, 3, 4]')
print(type(a))
b = eval('max([2,4,5])')
print(b)
9. sorted()
在处理数据过程中,我们经常会用到排序操作,比如将列表、字典、元组里面的元素正/倒排序。这时候就需要用到sorted() ,它可以对任何可迭代对象进行排序,并返回列表。对列表升序操作:
a = sorted([2, 4, 3, 7, 1, 9, 0, 5])
print(a)
[0, 1, 2, 3, 4, 5, 7, 9]
10. map()
map() 会根据提供的函数对指定序列做映射。第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
# 提供了两个列表,对相同位置的列表数据进行相加
a = map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
print(list(a))
[3, 7, 11, 15, 19]
11. reduce()
前面说到对列表里的每个数字作平方处理,用map()函数。那我想将列表里的每个元素相乘,该怎么做呢?这时候用到reduce()函数。
from functools import reduce
nums = [1, 2, 3, 4, 5]
a = reduce(lambda x,y:x*y, nums)
print(a)
120
12. data_range
从外部 API 或数据库获取数据时,需要多次指定时间范围。Pandas 的 data_range 覆盖了这一需求。
import pandas as pd
date_from = '2019-01-01'
date_to = '2019-01-12'
date_range = pd.date_range(date_from, date_to, freq='D')
print(date_range)
DatetimeIndex(['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', '2019-01-07', '2019-01-08', '2019-01-09', '2019-01-10', '2019-01-11', '2019-01-12'], dtype='datetime64[ns]', freq='D')
13. lambda
很多语言都有匿名函数,python的匿名函数写作lambda,当需要实现一定功能而又不想“大张旗鼓”的 def 一个函数时,lambda就是最优的选择。
f = lambda x:x**2
f(2)
4
14. format
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。基本语法是通过 {} 和 : 来代替以前的 % 。format 函数可以接受不限个参数,位置可以不按顺序。
print("{} {}".format("hello", "world")) # 不设置指定位置,按默认顺序
print("{1} {0} {1}".format("hello", "world")) # 设置指定位置
hello world world hello world