Learning-python

参考

python教程-廖雪峰

To-Do

* python -m <package>

Properties

pkg_resources

from pkg_resources import resource_filename

def require_path(name):
    return resource_filename(__name__, name)

*args **kwds

def study(name, age, gender='男', *args, **kwds):
	print('Name:%s, Age:%d, Gender:%s'%(name, age, gender))
	print(args)
	print(kwds)
➜ study('snorlax', 24, '男', 175, 75, math=145, english=135)
Name:snorlax, Age:24, Gender:男
(175, 75)
{'math': 145, 'english': 135}

此外,* 与 ** 还可用于列表、元组、字典的解包:

a = (1,2,3)
print(a)  # (1, 2, 3)
print(*a)  # 1 2 3

b = [1,2,3]
print(b)  # [1, 2, 3]
print(*b)  # 1 2 3

c = {'name':'xufive', 'age':51}
print(c)  # {'name': 'xufive', 'age': 51}
print(*c)  # name age
print('name: {name}, age: {age}'.format(**c))  # name: snorlax, age: 24

三元表达式

>>> y = 5
>>> print('y是一个负数' if y < 0 else 'y是一个非负数')
y是一个非负数
>>> y = 5
>>> x = -1 if y < 0 else 1
>>> x
1

列表推导式

>>> a = [1, 2, 3, 4, 5]
>>> result = [i*i for i in a]
>>> result
[1, 4, 9, 16, 25]

列表索引

>>> a = [0, 1, 2, 3, 4, 5]
>>> b = ['a', 'b']
>>> a[2:2] = b
>>> a
[0, 1, 'a', 'b', 2, 3, 4, 5]  # 2-2=0 在a[2]的位置插入b
>>> a[3:6] = b
>>> a
[0, 1, 'a', 'a', 'b', 4, 5]  # 6-3=3 将a[3:6]三个元素替换为b

lambda函数

>>> a = [{'name':'B', 'age':50}, {'name':'A', 'age':30}, {'name':'C', 'age':40}]
>>> sorted(a, key=lambda x:x['name']) # 按姓名排序
[{'name': 'A', 'age': 30}, {'name': 'B', 'age': 50}, {'name': 'C', 'age': 40}]
>>> sorted(a, key=lambda x:x['age']) # 按年龄排序
[{'name': 'A', 'age': 30}, {'name': 'C', 'age': 40}, {'name': 'B', 'age': 50}]
a = [1,2,3]
for item in map(lambda x:x*x, a):
	print(item, end=', ')   

1, 4, 9, 

yield

def get_square(n):
	for i in range(n):
		yield(pow(i,2))

a = get_square(5)  # a <generator object get_square at 0x000001B2DE5CACF0>

for i in a:
	print(i, end=', ')
print('-'*8)
for i in a:
	print(i, end=', ')

0, 1, 4, 9, 16, --------

装饰器

import time
def timer(func):
	def wrapper(*args,**kwds):
		t0 = time.time()
		func(*args,**kwds)
		t1 = time.time()
		print('耗时%0.3f'%(t1-t0,))
	return wrapper

@timer
def study(delay):
	print('函数study开始')
	time.sleep(delay)
	print('函数study结束')
➜ study(3)
函数do_something开始
函数do_something结束
耗时3.004

timer() 是我们定义的装饰器函数,使用@把它附加在任何一个函数(比如study)定义之前,就等于把新定义的函数,当成了装饰器函数的输入参数。运行 study() 函数,可以理解为执行了timer(study) 。细节虽然复杂,不过这么理解不会偏差太大,且更易于把握装饰器的制造和使用。

assert

所谓断言,就是声明表达式的布尔值必须为真的判定,否则将触发 AssertionError 异常。严格来讲,assert是调试手段,不宜使用在生产环境中,但这不影响我们用断言来实现一些特定功能,比如,输入参数的格式、类型验证等。

def i_want_to_sleep(delay):
	assert(isinstance(delay, (int,float))), '函数参数必须为整数或浮点数'
	print('开始睡觉')
	time.sleep(delay)
	print('睡醒了')

>>> i_want_to_sleep(1.1)
开始睡觉
睡醒了
>>> i_want_to_sleep(2)
开始睡觉
睡醒了
>>> i_want_to_sleep('2')
Traceback (most recent call last):
  File "<pyshell#247>", line 1, in <module>
    i_want_to_sleep('2')
  File "<pyshell#244>", line 2, in i_want_to_sleep
    assert(isinstance(delay, (int,float))), '函数参数必须为整数或浮点数'
AssertionError: 函数参数必须为整数或浮点数

Utilities

ARGS

import argparse

parser = argparse.ArgumentParser(description="this is a sample.")
parser.add_argument("-n", "--number", default=233, type=int, help="number value.")
parser.add_argument("-s", "--string", default='hello', type=str, help="string")
parser.add_argument("-t", "--tuple", default=("05", 23), type=tuple, help="tuple")
parser.add_argument("-l", "--list", default=[10, "27"], type=list, help="list")
args = parser.parse_args()

print(args.number, args.string, args.tuple, args.list)
sub-command
# test.py

import argparse

def build_argparser():

    parser = argparse.ArgumentParser(prog='PROG')
    subparsers = parser.add_subparsers(help='sub-command help', dest = 'action')
    #添加子命令
    parser_a = subparsers.add_parser('action_a', help='action_a help')
    parser_b = subparsers.add_parser('action_b', help='action_b help')

    # 设置子命令参数 action_a
    parser_a.add_argument("-i", "--i", type=int, help="i int")
    parser_a.add_argument("-j", "--j", type=int, help="j int")

    # 设置子命令参数 action_b
    parser_b.add_argument("-x", "--x", type=int, help="x int")
    parser_b.add_argument("-y", "--y", type=int, help="y int")

    return parser

args = build_argparser().parse_args()

if args.action == 'action_a':
    print(f"{args.i}+{args.j}={args.i+args.j}")
elif args.action == 'action_b':
    print(f"{args.x}*{args.y}={args.x*args.y}")
➜ python test.py action_a -i 5 -j 10
5+10=15
➜ python test.py action_b -x 5 -y 10
5*10=50
sys.argv[]
import sys

print(sys.argv[:])
➜  python study.py -a 111 -bb two
['study.py', '-a', '111', '-bb', 'two']

Write Record File

TXT
def list2txt(txt_list, txt_file):
    with open(txt_file, mode='w') as f:
        for item in txt_list:
            f.write(f"{item}\n")

def dict2txt(txt_dict, txt_file):
    with open(txt_file, 'w') as f:
        for i in txt_dict:
            print(f"{i} {txt_dict[i]}\n", end="")
            f.write(f"{i} {txt_dict[i]}\n")

def txt2list(txt_file):
    txt_list = []
    with open(txt_file, "r") as f:
        for line in f.readlines():
            line = line.strip('\n')  #去掉列表中每一个元素的换行符
            txt_list.append(line)
    return txt_list
JSON
def dict2json(json_dict, json_path):
    import json
    with open(json_path,'w') as f:
        f.write(json.dumps(json_dict, ensure_ascii=False, indent=2))
    pass

def json2dict(json_path:str):
    import json
    with open(json_path,'r', encoding='UTF-8') as f:
        load_dict = json.load(f)
    return load_dict

def view_dict(json_dict:dict):
    for key,value in json_dict.items():
        print('{key} {value}'.format(key = key, value = value))

if __name__ == "__main__":

    json_dict = {}
    var = "a"
    json_dict[var] = 1  # {'a': 1}
    json_dict["b"] = 2  # {'a': 1, 'b': 2}

    dict2json(json_dict)
    print(json2dict('./output.json'))

× output.json

{
  "a": 1,
  "b": 2
}

Filename

splitext
>>> import os
>>> a = "1/2/3/4/5/6.txt"
>>> os.path.splitext(a)  # 提取扩展名
('1/2/3/4/5/6', '.txt')
>>>
basename
>>> import os
>>> a = "1/2/3/4/5/6.txt"  # 提取文件名
>>> os.path.basename(a)
'6.txt'
>>>

Win or Linux or OSX

import sys
import platform
 
print(sys.platform)

def ShowPlatform():
    print ("--------------Operation System-----------------------")
    print(platform.architecture())
    print(platform.platform())
    print(platform.system())
    # print(platform.uname())
    print ("--------------Python Version-------------------------")
    print(platform.python_version())
 
def UsePlatform():
    print ("--------------Platform Version-----------------------")
    sysstr = platform.system()
    if sysstr == "Windows":
        print ("Call Windows tasks")
    elif sysstr == "Linux":
        print ("Call Linux tasks")
    else:
        print ("Call System tasks: %s" % sysstr)
    return sysstr

ShowPlatform()
UsePlatform()

Win10

win32
--------------Operation System-----------------------
('64bit', 'WindowsPE')
Windows-10-10.0.18362-SP0
Windows
--------------Python Version-------------------------
3.7.3
--------------Platform Version-----------------------
Call Windows tasks

Linux

linux
--------------Operation System-----------------------
('64bit', 'ELF')
Linux-5.4.0-40-generic-x86_64-with-glibc2.10
Linux
--------------Python Version-------------------------
3.8.3
--------------Platform Version-----------------------
Call Linux tasks

MacOS

darwin
--------------Operation System-----------------------
('64bit', '')
Darwin-19.6.0-x86_64-i386-64bit
Darwin
--------------Python Version-------------------------
3.7.6
--------------Platform Version-----------------------
Call System tasks: Darwin


写在最后:若本文对您有帮助,请点个赞啦 ٩(๑•̀ω•́๑)۶

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Q-learning is a popular reinforcement learning algorithm used to solve Markov Decision Processes (MDPs). In Python, you can implement Q-learning using various libraries such as NumPy and TensorFlow. Here's a basic implementation of Q-learning in Python: ```python import numpy as np # Define the Q-learning function def q_learning(env, num_episodes, learning_rate, discount_factor, epsilon): # Initialize the Q-table num_states = env.observation_space.n num_actions = env.action_space.n Q = np.zeros((num_states, num_actions)) # Q-learning loop for episode in range(num_episodes): state = env.reset() done = False while not done: # Choose an action using epsilon-greedy policy if np.random.uniform() < epsilon: action = env.action_space.sample() else: action = np.argmax(Q[state]) # Perform the action and observe the next state and reward next_state, reward, done, _ = env.step(action) # Update the Q-table Q[state, action] += learning_rate * (reward + discount_factor * np.max(Q[next_state]) - Q[state, action]) state = next_state return Q # Example usage env = gym.make('your_environment') # Replace 'your_environment' with the name of your environment num_episodes = 1000 learning_rate = 0.1 discount_factor = 0.9 epsilon = 0.1 Q_table = q_learning(env, num_episodes, learning_rate, discount_factor, epsilon) ``` In this example, `env` represents the environment you want to train your agent on (e.g., a grid world). `num_episodes` is the number of episodes the agent will play to learn the optimal policy. `learning_rate` controls the weight given to the new information compared to the old information, while `discount_factor` determines the importance of future rewards. `epsilon` is the exploration rate that balances exploration and exploitation. Note that you need to install the required libraries (e.g., NumPy and gym) before running the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的卡比兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值