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

not_speshal

not_speshal 最近创建的主题
not_speshal 最近回复了
3 年前
回复了 not_speshal 创建的主题 » 如何用条件[Python]替换所有字符

不要在适当的位置修改字符串。相反,只需创建一个新字符串:

output = ""
for char in camel_case:
    if char.isupper():
        output += "_"
    output += char.lower()

或者,用一句话来说:

"".join("_"+char.lower() if char.isupper() else char.lower() for char in camel_case)
3 年前
回复了 not_speshal 创建的主题 » Python将列表中的元素以偶发模式提取到元组中

您可以检查“float”、“string”模式,并相应地追加:

output = list()
for i, element in enumerate(arr):
    if isinstance(element, float) and isinstance(arr[i+1], str):
        if isinstance(arr[i+2], str):
            t = tuple(arr[i:i+3])
        else:
            t = tuple(arr[i:i+2]+["NONE"])
        output.append(t)

>>> output
[(1150.1, 'James', 'NONE'),
 (3323.1, 'Steve', 'NONE'),
 (9323.1, 'John', 'NONE'),
 (1233.1, 'Gary', 'criminal'),
 (3293.1, 'Josh', 'NONE'),
 (9232.1, 'Daniel', 'criminal')]

IIUC,你可以试试 pivot :

df["Ratio"] = df["Data A"].div(df["Data B"])

output = df.drop("Ratio", axis=1).join(df.pivot(None, "Place", "Ratio").mul(100).fillna(0).add_prefix("To "))

>>> output
     Place  Data A  Data B  Data C  To England  To France
0   France    0.03    0.06    0.15    0.000000       50.0
1  England    0.05    0.07    0.11   71.428571        0.0
2  England    0.04    0.03    0.87  133.333333        0.0
3 年前
回复了 not_speshal 创建的主题 » 特定格式的Python字典输出列表

尝试创建一个字典,将账号映射到VPC。然后转换这本字典:

dct = dict()
for vpc in vpcs:
    if vpc["ResourceOwnerId"] not in dct:
        dct[vpc["ResourceOwnerId"]] = list()
    dct[vpc["ResourceOwnerId"]].append(vpc["ResourceId"])

output = [{"account_number": k, "vpc": v} for k,v in dct.items()]

>>> output
[{'account_number': '111111111111', 'vpc': ['vpc-aaaaa', 'vpc-ddddd']},
 {'account_number': '222222222222', 'vpc': ['vpc-ccccc']}]
3 年前
回复了 not_speshal 创建的主题 » python中的矩阵(列表)

尝试:

>>> [[0 for i in range(len(mat[0]))] for j in range(len(mat))]
[[0], [0], [0], [0], [0]]

也最好使用不同的循环变量,而不是 i 对于这两种情况:

3 年前
回复了 not_speshal 创建的主题 » Python——如何在数据框中对日期字符串进行排序?

首先将日期转换为时间戳:

import pandas as pd
df["date"] = pd.to_datetime(df["date"], format="%m/%d/%y")
>>> df["date"].max()
3 年前
回复了 not_speshal 创建的主题 » 在没有任何库的情况下从python文本中删除非单词

尝试:

words = list()
with open("file.txt", errors="ignore") as infile:
    for row in infile:
        for word in row.split():
            if word.strip('"(.,!?):').replace("'","").replace("-","").isalpha():
                words.append(word.strip('"(.,!?):').lower())

>>> " ".join(words)
'zwizwai confirmed that there were attempts to place him together with mudzuri on the sanctions list describing them as nicodimous by some fellow mdc-t the cast in order of appearance on universities to ensure the safety of women on'
3 年前
回复了 not_speshal 创建的主题 » 迭代二维python列表以插入每个单元格的x和y坐标

尝试:

grid = [[[i,j] for j in range(colRange)] for i in range(rowRange)]

>>> grid
[[[0, 0], [0, 1]], [[1, 0], [1, 1]], [[2, 0], [2, 1]]]