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

azro

azro 最近创建的主题
azro 最近回复了
3 年前
回复了 azro 创建的主题 » 在python中通过相邻字母计数转换字符串

我建议 itertools.groupby 然后根据需要格式化

from itertools import groupby

# groupby("assdggg")
# {'a': ['a'], 's': ['s', 's'], 'd': ['d'], 'g': ['g', 'g', 'g']}

result = ""
for k, v in groupby("assdggg"):
    count = len(list(v))
    result += (str(count) if count > 1 else "") + k

print(result)  # a2sd3g
3 年前
回复了 azro 创建的主题 » 字符串索引超出范围-基于Python逻辑的错误?

问题是这个范围是从 0 (包含)到字符串的长度(独占),这对于索引来说是可以的,因为字符串是基于0的索引,但您不仅访问当前索引,还访问下一个索引,所以当达到 j 这个 [j+1] 是不是太远了

def double_chars(word):
    chars_count = 0
    for j in range(len(word) - 1):
        if word[j] == word[j + 1]:
            chars_count += 1
    return chars_count
0 1. 2. 3. 4. 5. 6. 7. 8. 9 10 11
x[j] H E L L o . W o R L D
x[j+1] E L L o . . W o R L D !

一个班轮提案,另一个有 zip

def double_chars(word):
    return sum(1 for j in range(len(word) - 1) if word[j] == word[j + 1])

def double_chars(word):
    return sum(1 for ch_curr, ch_next in zip(word, word[1:]) if ch_curr == ch_next)
4 年前
回复了 azro 创建的主题 » NameError:未在Python中定义名称“x”

什么时候做 global price_buy 这意味着您使用全局定义的 price_buy 在方法中本地定义,但

  • 也不 购买价格 也没有 number_exit 全局定义(在方法之外)
  • 你不需要全局变量

它们只是本地的,而且更好:内联的

def fun_price_buy():
    price_buy = np.random.randint(50000,300000)
    return price_buy

# inline, no temporaty variable is needed
def fun_price_buy():
    return np.random.randint(50000,300000)

最后,如果您想从变量中的方法中获取值,以便对其进行操作:

import numpy as np

# This function generates a list of numbers under certain rules
def num_var_list(number_exit):
    number_list = []
    number_target = number_exit
    num_max_robot = (number_target * 20) / 100
    while num_max_robot > 0:
        num_robot = np.random.randint(1,int(num_max_robot))
        if num_robot > number_target:
                number_list.append(number_target)
        else: 
            number_list.append(num_robot)
        number_target = number_target - number_target
    return number_list
        
def fun_price_buy():
    return np.random.randint(50000,300000)

def fun_mx_buy():
    return np.random.randint(50, 150)

lista_number_list = []
lista_price_buy = []
lista_mx_buy = []

# This loop append each function 50 times to a new list
while len(lista_price_buy) <= 50: 
    number_exit = fun_mx_buy()
    price_buy = fun_price_buy()
    vr_list = num_var_list(number_exit)
   
    lista_number_list.append(vr_list)
    lista_price_buy.append(price_buy )
    lista_mx_buy.append(number_exit )
3 年前
回复了 azro 创建的主题 » 在Python中隔离给定结构的所有子字符串

从你共享的结构来看 rolling xdy(1+2+3...+z)=a 将表示数字的everyletter替换为 \d+ (一个或多个数字)并进行一些调整,您将获得

rolling \d+d\d+\((?:\d+\+)*\d+\)=\d+

Regex demo


import re

text = "rolling 7d10(1+4+5+3+8+8+3)=32rolling 7d10(6+8+3+9+7+10+8)=51rolling " \
       "7d10(7+7+6+6+8+3+5)=42rolling 4d10(3+3+3+4)=13rolling " \
       "7d10(5+5+10+7+4+9+10)=50rolling 1d10 + 8(10)+8=18"
results = re.findall(r"rolling \d+d\d+\((?:\d+\+)*\d+\)=\d+", text)
print(results)
['rolling 7d10(1+4+5+3+8+8+3)=32', 'rolling 7d10(6+8+3+9+7+10+8)=51', 
 'rolling 7d10(7+7+6+6+8+3+5)=42', 'rolling 4d10(3+3+3+4)=13', 
 'rolling 7d10(5+5+10+7+4+9+10)=50']

请注意,最后一个是无效的,因为有数字和数字 + 在括号外签名

4 年前
回复了 azro 创建的主题 » 检查是否为数字,并在python上导入sys[已关闭]

方法 A.isalpha() 返回一个布尔值 True False 一根绳子也没有 "True" ,你比较像这样的布尔人

if A.isalpha() is True: 
    print("hello")

但是作为一个 if 需要一个布尔语句,返回值来自 isalpha 已经很好了

if A.isalpha():
    print("hello")

这个 insert 操作就地修改对象,并像几乎所有这些类型的方法一样返回 None 添加它的所有元素,而您不能

the_list.extend(None)

所以就这么做吧

sublist.insert(len(sublist), i + 1)

你只能读取一次文件描述符(如果你不想启动的话),所以你可以在每个函数中打开你的文件

def function_search_search_key():
    search_search_key = ['{search_key:']
    with open('scan_dcn.yaml') as fic:
        for line in fic:
            for word in search_search_key:
                if word in line:
                    print(line)

def function_search_event_type():
    search_event_type = ['event_type:']
    with open('scan_dcn.yaml') as fic:
        for line in fic:
            for word in search_event_type:
                if word in line:
                    print(line)

您可以遍历文件夹的文件,并检查 _done 是以这个名字直接操作的

from os import listdir
from os.path import join

for file in listdir("mydirectory"):
    if "_done" in file:
        full_path = join("mydirectory", file)

或者准备文件列表,然后遍历它们:

files_done = [join("mydirectory", file) for file in listdir("mydirectory") 
                                        if "_done" in file]
for full_path in files_done:
    pass