社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Chris Doyle

Chris Doyle 最近创建的主题
Chris Doyle 最近回复了
4 年前
回复了 Chris Doyle 创建的主题 » Python:试图形成SQL语句时的字符串操作问题

你可以用正则表达式来代替它们。如果单引号后面跟一个空格和等号,则正则表达式会捕获单引号内的值,然后仅用该值替换引号和值。

import re
query = '''UPDATE table1 SET 'variable1' = 'value1', 'something' = "['bla', 'yada']", location = 'chicago' WHERE id = 9'''

updated_query = re.sub(r"'([^']+)'(?=\s=)", r"\1", query)
print(query)
print(updated_query)

输出

UPDATE table1 SET 'variable1' = 'value1', 'something' = "['bla', 'yada']", location = 'chicago' WHERE id = 9
UPDATE table1 SET variable1 = 'value1', something = "['bla', 'yada']", location = 'chicago' WHERE id = 9
5 年前
回复了 Chris Doyle 创建的主题 » else:不在jupyter笔记本python 3中工作

你的问题是你在else语句中的打印单词后面有一个冒号。改变

print:("a is NOT less than b")

print("a is NOT less than b")

5 年前
回复了 Chris Doyle 创建的主题 » for循环内递归-Python

因此,在递归解决方案中,您不使用for循环,而是使用函数式编程模型,即使用堆栈作为循环

def recur_factorial(n):
    total = 1
    if n > 1:
        total = n * recur_factorial(n - 1)
    print(total)
    return total


recur_factorial(5)

1
2
6
24
120

希望这能给你一个更好的想法,如果你的需求改变了,你可以调整这个模型来实现你所需要的。

更新

刚刚在你的评论中意识到你说函数需要返回 list 从2到n的阶乘。下面是更新的代码,返回一个列表。

def recur_factorial(n, factorials=[]):
    if n == 1:
        factorials.append(1)
    else:
        total = n * recur_factorial(n - 1)[-1]
        factorials.append(total)
    return factorials

factorials = recur_factorial(10)
print(factorials)

输出

[2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
5 年前
回复了 Chris Doyle 创建的主题 » 仅打印.txt文件中的特定单词-Python

假设每个名字只是一个名字和姓氏,没有中间名等等,你可以简单地读取文件的每一行,并根据空格进行拆分。下面将捕获first to fields in first and last,然后捕获dob中的其余字段。然后我们就可以用变量打印这两行。不需要在文件数据中循环两次。

with open('test.txt') as my_file:
    for person in my_file:
        first, last, *dob = person.rstrip().split()
        print(f"Name: {first[0]}. {last}")
        print(f"Birthdate: {' '.join(dob)}")

Name: O. Wright
Birthdate: 21 July 1988
Name: R. Holloway
Birthdate: 13 September 1988
Name: M. Figueroa
Birthdate: 9 October 1988
5 年前
回复了 Chris Doyle 创建的主题 » 自动更正python库抛出错误而不是更正单词

您可能想查看 autocorrect 模块化 speller 类inits签名是 def __init__(self, threshold=0, lang='en'): 因此,当创建类的实例并传入一个参数时,它将假定您传入了一个阈值。

如此呼唤 Speller("something") 将传递一个字符串,该字符串将存储为阈值。然后在init方法的第27行。 if threshold > 0 此字符串将与int进行比较。因此出现错误。因为不能在字符串和int之间执行>。

我建议首先阅读本模块的任何文档。文献中的例子建议像

>>> spell = Speller(lang='en')
>>> spell("I'm not sleapy and tehre is no place I'm giong to.")
"I'm not sleepy and there is no place I'm going to."

当你对各种不同的能力更感兴趣时,你可以写一个函数,它包含你想打印的n个数字和你想使用的能力。然后它将返回当前值的元组 n ,的值 n个 power 然后是一个文本公式。

这个函数也会产生结果,所以如果你在寻找大的N值,它们不会立即加载到内存中。

在这个例子中,我加载了pandas模块,只是为了方便将输出作为数据帧打印。

编辑 更新了根的示例。roots函数可以接受一个可选的十进制精度参数,因为roots通常返回十进制值。如果函数中没有精度值,则将显示完整的精度

import pandas as pd

def get_nth_powers(nth, power):
    for n in range(nth):
        written_formula = (" x ".join([str(n)] * power))
        yield (n, n ** power, written_formula)


def get_nth_roots(nth, root, decimal_precision=0):
    decimal_precision = f'0.{decimal_precision}f' if decimal_precision else ''
    for n in range(nth):
        value = n ** (1/root)
        written_formula = (" x ".join([f'{value:{decimal_precision}}'] * root))
        yield (n, value, written_formula)


data = get_nth_powers(1000, 4)
df = pd.DataFrame(data, columns=('Nth', 'To Power', 'formula'))
print(df)

data = get_nth_roots(1000, 2, 3)
df = pd.DataFrame(data, columns=('Nth', 'value', 'formula'))
print(df)

输出

     Nth      To Power                formula
0      0             0          0 x 0 x 0 x 0
1      1             1          1 x 1 x 1 x 1
2      2            16          2 x 2 x 2 x 2
3      3            81          3 x 3 x 3 x 3
4      4           256          4 x 4 x 4 x 4
..   ...           ...                    ...
995  995  980149500625  995 x 995 x 995 x 995
996  996  984095744256  996 x 996 x 996 x 996
997  997  988053892081  997 x 997 x 997 x 997
998  998  992023968016  998 x 998 x 998 x 998
999  999  996005996001  999 x 999 x 999 x 999

[1000 rows x 3 columns]
     Nth      value          formula
0      0   0.000000    0.000 x 0.000
1      1   1.000000    1.000 x 1.000
2      2   1.414214    1.414 x 1.414
3      3   1.732051    1.732 x 1.732
4      4   2.000000    2.000 x 2.000
..   ...        ...              ...
995  995  31.543621  31.544 x 31.544
996  996  31.559468  31.559 x 31.559
997  997  31.575307  31.575 x 31.575
998  998  31.591138  31.591 x 31.591
999  999  31.606961  31.607 x 31.607

[1000 rows x 3 columns]