Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个字符串列表,其中包含某些进程的pid,这些进程必须在某个时间被杀死。
pidList = ['1234', '1235', '1236']
一个人如何遍历列表,首先将所有元素转换为 int,然后终止该 pid 表示的进程?
本质上是这样做的:
os.kill(int(pidList[0]), signal.SIGTERM)
os.kill(int(pidList[1]), signal.SIGTERM)
os.kill(int(pidList[2]), signal.SIGTERM)
只需遍历pidlist
pidlist
for proc_id in pidlist: os.kill(int(proc_id), signal.SIGTERM)
你可以使用列表理解
[os.kill(int(pid), signal.SIGTERM) for pid in pidlist]
地图也可以
map(lambda x: os.kill(int(x), signal.SIGTERM), pidlist)