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

Arthur King

Arthur King 最近创建的主题
Arthur King 最近回复了
4 年前
回复了 Arthur King 创建的主题 » 使用Python中的任务管理器在Python进程终止时调用函数

我不知道任务管理器是如何终止进程的。它可能会发送两个信号中的一个——SIGTERM或SIGKILL。

如果是前者,那么你可以为此设置一个处理程序。看见 signal

如果是后者,那你就无能为力了

下面是一种允许数字字符串位于子列表中任意位置且子列表长度任意的方法:

wholeList = [ ['apple','1','2'],['banana','2','3'],['cherry', '3','4'],['downypitch', '4','5'] ]
newList = wholeList[:]
for e in newList:
    for i, x in enumerate(e):
        try:
            e[i] = int(e[i])
        except ValueError:
            pass
print(newList)

输出:

[['apple', 1, 2], ['banana', 2, 3], ['cherry', 3, 4], ['downypitch', 4, 5]]
4 年前
回复了 Arthur King 创建的主题 » 在Python中查找n个元素相等的索引

有很多方法可以做到这一点。这里有一个:

A = [False, False, True, True, True, False, True, True, True]

def findSequence(lst, v, n):
    offset = 0
    while offset < len(lst) - n:
        try:
            i = lst[offset:].index(v)
            offset += i
            if lst[offset:offset+n].count(v) == n:
                return offset
            offset += 1
        except IndexError:
            break


print(findSequence(A, True, 3))

输出:

2
4 年前
回复了 Arthur King 创建的主题 » 如何用python从这个字典中提取值?

这是另一种方法:

import json

D = {"message":"{\"_\":\"user\",\"pFlags\":{\"contact\":true},\"flags\":2175,\"id\":379951860,\"access_hash\":\"6967195540985199805\",\"first_name\":\"پژوا\",\"last_name\":\"روزبهی\",\"username\":\"mramtd2\",\"phone\":\"989157145632\",\"photo\":{\"_\":\"userProfilePhoto\",\"photo_id\":\"1631880813210609625\",\"photo_small\":{\"_\":\"fileLocation\",\"dc_id\":4,\"volume_id\":\"448413446\",\"local_id\":476387,\"secret\":\"655623158723369503\"},\"photo_big\":{\"_\":\"fileLocation\",\"dc_id\":4,\"volume_id\":\"448413446\",\"local_id\":476389,\"secret\":\"13993366131879811943\"}},\"status\":{\"_\":\"userStatusOffline\",\"was_online\":1558046876}}","phone":"989157145632","@version":"1","typ":"telegram_contacts","access_hash":"6967195540985199805","id":379951860,"@timestamp":"2020-01-26T13:50:12.793Z","path":"/home/user/mirror_01/users_5d65f610ec18aa615a5f580c.log","username":"mramtd2","type":"redis","flags":2175,"host":"ubuntu","imported_from":"telegram_contacts"}

data = json.loads(D['message'])

for key in ['first_name', 'last_name', 'phone', 'id']:
    print(f'{key} -> {data.get(key, "?")}')