社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

gmds

gmds 最近创建的主题
gmds 最近回复了
6 年前
回复了 gmds 创建的主题 » 如何在Python中表示参数化类型?

拥有 Monad 继承自 Generic

from typing import TypeVar, List, Callable, Generic

a = TypeVar('a')
b = TypeVar('b')

class Monad(Generic[a]):
    # your code here
    pass

def foldM(f: Callable[[a, b], Monad[a]], acc: a, xs: List[b]) -> Monad[a]:
    # your code here
    pass
5 年前
回复了 gmds 创建的主题 » 如何在python中常规检查ValueError?

这个具体案例 ,你可以放弃 try-except :

question = input('Please enter an integer.')
while not question.isdigit():
    question = input(f'{question} is not an integer. Please enter an integer.')

question = int(question)

也就是说,我会说你的代码没有什么问题,与这个解决方案相比,它实际上是这种情况下的惯用方法。

而且,它 与您的解决方案不同的是,此方法检查可完全转换为整数的用户输入,而您的方法则接受浮动。

5 年前
回复了 gmds 创建的主题 » Python,numpy如何基于每行中的值复制数组中的行

由于无法保证执行此操作后三维源阵列中的原始二维子阵列将具有相同的形状,因此通常不能将它们重新堆叠为三维阵列。

你可以得到一个 list 数组的 np.repeat 通过将每个2D数组的第一列作为重复次数传递。然后,它将对每一行重复相应的次数:

from pprint import pprint

result = ([np.repeat(a[i], a[i, :, 0], axis=0) for i in range(a.shape[0])])

pprint(result)

输出:

[array([[3, 1, 3, 1, 2],
       [3, 1, 3, 1, 2],
       [3, 1, 3, 1, 2],
       [4, 4, 4, 2, 0],
       [4, 4, 4, 2, 0],
       [4, 4, 4, 2, 0],
       [4, 4, 4, 2, 0],
       [3, 4, 4, 4, 0],
       [3, 4, 4, 4, 0],
       [3, 4, 4, 4, 0],
       [1, 4, 3, 3, 0]]),
 array([[4, 2, 0, 2, 1],
       [4, 2, 0, 2, 1],
       [4, 2, 0, 2, 1],
       [4, 2, 0, 2, 1],
       [2, 1, 2, 0, 3],
       [2, 1, 2, 0, 3],
       [4, 1, 3, 4, 3],
       [4, 1, 3, 4, 3],
       [4, 1, 3, 4, 3],
       [4, 1, 3, 4, 3],
       [2, 3, 2, 0, 0],
       [2, 3, 2, 0, 0]])]
5 年前
回复了 gmds 创建的主题 » 只读文本文件python中指定行中的数字

可以使用正则表达式:

import re

with open(os.path.join(dir,f)) as file:
    for i, line in enumerate(file):
        if i == 17:
            number1 = float(re.search(r'\d+\.\d+', line).group())

        elif i == 18:
            number2 = float(re.search(r'\d+\.\d+', line).group())
5 年前
回复了 gmds 创建的主题 » 如何在python中使用for循环创建多个数据帧

如果我理解正确,你可以使用 list 对此的理解:

subset_df_list = [df.groupby('Location').get_group(36) for df in df_list]

作为旁白,你的 for 循环不起作用,因为你一直在给 df . 你可能想要这个,这也相当于上面的理解:

subset_df_list = []

for df in df_list:
    subset_df = df.groupby('Location').get_group(36)
    subset_df_list.append(subset_df)
6 年前
回复了 gmds 创建的主题 » Pure Python 3.6.3-查找两个三维向量之间的度数差异

在不太深入数学细节的情况下,有一种叫做 点积 数学方面:

enter image description here

基本上,这是一种组合两个向量的方法(称为 )得到一个号码。这个数字等于 ,乘以 ,乘以它们之间夹角的余弦(我们称之为)。

多亏了这个等式,通过改变事物,我们最终可以得到我们想要的,也就是。

说我们有 : [1, 2, 3] : [4, 5, 6] . 我们可以通过平方它们的元素并取和的平方根来计算它们的大小。因此, (1 ** 2 + 2 ** 2 + 3 ** 2) ** 0.5 = 14 ** 0.5 ,以及 (4 ** 2 + 5 ** 2 + 6 ** 2) ** 0.5 = 77 ** 0.5 .

把它们相乘给我们 1078 ** 0.5 . 因此,点积等于 (1078 ** 0.5) * cos θ .

结果表明,点积可以通过两个矢量的对应元素相乘并求和得到。所以,为了 上面,点积是 1 * 4 + 2 * 5 + 3 * 6 = 32 .

给定这两个不同(但相等)的点积表达式,我们可以将它们等同于求解,如下所示(arccos是将cos转换为:

(1078 ** 0.5) * cos θ = 32
cos θ = 32 / (1078 ** 0.5)
θ = arccos(32 / (1078 ** 0.5))
θ ≈ 12.93 (in degrees)

现在,剩下的就是在代码中实现它:

from numpy import arccos

def angle_between_vectors(v1, v2):
    def magnitude(v):
        return sum(e ** 2 for e in v) ** 0.5

    dot_product = sum(e1 * e2 for e1, e2 in zip(v1, v2))
    magnitudes = magnitude(v1) * magnitude(v2)
    angle = arccos(dot_product / magnitudes)
    return angle

将此函数应用于 从弧度到度数的转换(除以_,再乘以180)得到12.93。

6 年前
回复了 gmds 创建的主题 » 从python中的地址中删除电子邮件域[重复]

正则表达式怎么样?

import re

def extract_user(address):
    result = re.search(r'([\w\d\.]+)@[\w\d\.]+', address)

    if result and address.count('@') == 1:
        return result.group(1)

    else:
        raise ValueError(f'{address} is not a validly formatted e-mail address.')

extract_user('john.doe@generic.com')

输出:

'john.doe'
5 年前
回复了 gmds 创建的主题 » 如何将python模块创建为单个可调用函数?

不要这样做。如果你不确定是否需要这个,你就不需要。让一个模块可调用是一件很奇怪的事情。然而,这是一个有趣的智力好奇心,所以…

可以这样做,利用这样一个事实:一个模块本身就是一个对象,如果一个对象的类具有 __call__ 方法。

然而,一个问题是 module 是内置的,不能修改内置的属性。

因此,最简单的解决方案是创建一个类来代替模块 sys.modules ,但也有一个 阿尔卡拉尔 方法。

greet.py :

import sys

class CallableModule():

    def __init__(self, wrapped):
        self._wrapped = wrapped

    def __call__(self, *args, **kwargs):
        return self._wrapped.main(*args, **kwargs)

    def __getattr__(self, attr):
        return object.__getattribute__(self._wrapped, attr)

sys.modules[__name__] = CallableModule(sys.modules[__name__])

def main(x):
    print(f'Hello {x}!')

来自壳牌:

>>> import greet
>>> greet('there')
Hello there!