Skip to main content

PyPI 包,带有一些更好的 python 语法工具

项目描述

PyPI 版本 GitHub 问题 公关欢迎

下载 下载 下载

真假蟒蛇

PyPI 包,带有一些更好的 python 语法工具

pip install truefalsepython

逻辑工具

对于TrueFalse值,有相等的常数(就像在类 C 语言或 R 中一样)TRUE, T, trueFALSE, F, false; 因为None有一个NULL常数:

from truefalsepython import TRUE, FALSE, T, F, true, false, NULL

print(True == T) # True
print(True == TRUE) # True
print(True == true) # True


print(False == F) # True
print(False == FALSE) # True
print(False == false) # True

print(NULL) # None

小功能

  • is_odd(number)
  • is_even(number)
  • is_number(object)
  • max_fast(a, b)
  • min_fast(a, b)

有用的功能

  • fast_sample(objects, probs)objects--以probs概率返回 1 个随机对象。它比np.random.choice(objects, 1, probs)示例)更快
  • randomTrue(prob = 0.5)--True以概率返回prob,否则False

对调试有用:

  • set_trace()-- 像断点
  • debug(function, *args, **kwargs)-- 用于带有这些参数的调试function函数

类 R 函数

对于数组,有几个类似 R 的函数:

  • ifelse—— 只是包装numpy.where
  • nrow— 返回行数
  • ncol— 返回列数
  • colMeans— 返回每列的平均值
  • rowMeans— 返回每行的平均值
  • colSums— 返回每列的总和
  • rowSums— 返回每一行的总和
  • apply- 将函数应用于数组FUN的维度arr2D(对于行 ifMARGIN == 1和列 if MARGIN == 2
  • lapply- 对(数组/列表或其他)func中的每个元素应用函数array
  • sapply- 喜欢lapply但返回 numpy 数组
  • sample——np.random.choicereplace = False默认情况下是
  • sample_int— 样本编号从0n-1

使用示例:

import numpy as np
from truefalsepython import nrow, ncol, colMeans, rowMeans, colSums, rowSums, apply, lapply, sapply, sample, sample_int


np.random.seed(1)

# some 2D array
random_matrix = np.random.randint(8, size = (5, 3))


# how to get rows and cols counts
print(nrow(random_matrix)) # 5

print(ncol(random_matrix)) # 3

# operations for each row/column
print(rowMeans(random_matrix))
# [4.         2.66666667 5.         0.33333333 5.33333333]

print(colMeans(random_matrix))
# [2.4 4.4 3.6]

print(rowSums(random_matrix))
# [12  8 15  1 16]

print(colSums(random_matrix))
# [12 22 18]

# apply function (MARGIN is 1 for rows and 2 for columns)
print(apply(random_matrix, MARGIN = 1, FUN = np.min))
# [3 0 3 0 4]

# as u can see, it's not necessary to use FUN returns only 1 number by vector
print(apply(random_matrix, MARGIN = 2, FUN = np.sqrt))
#[[2.23606798 0.         1.73205081 0.         2.        ]
# [1.73205081 2.64575131 2.23606798 0.         2.64575131]
# [2.         1.         2.64575131 1.         2.23606798]]

some_arr = np.array([1, 2, 3, 5, 4, 3, 2])

# returns list
print(lapply(some_arr, lambda x: -x))
# [-1, -2, -3, -5, -4, -3, -2]

# returns numpy array
print(sapply(some_arr, lambda x: -x))
# [-1 -2 -3 -5 -4 -3 -2]

# like np.random.choice but replace = False by default
print(sample(some_arr, 4))
# [5 3 2 1]

# sample numbers from 0 to n-1
print(sample_int(n = 100, size = 10))
# [69 46 58 12 73 98 31 53 65 96]

没有类别的函数

  • time_to_seconds(days = 0, hours = 0, minutes = 0, seconds = 5)-- 将时间转换为秒

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

truefalsepython-0.2.0.tar.gz (5.7 kB 查看哈希

已上传 source

内置分布

truefalsepython-0.2.0-py3-none-any.whl (6.7 kB 查看哈希

已上传 py3