简体   繁体   English

如何从字典中为每个进程打印进程名称列表及其值?

[英]How can I print a list of process names and their values from a dictionary for each process?

I have a function that returns a dictionary of processes here is a snippet...我有一个 function ,它返回一个进程字典,这里是一个片段......

from win32com.client import GetObject
wmi = GetObject("winmgmts:")
def get_processes():
    dictprocesses =  {"Caption": [], "Creation Date": []}
    colprocs = wmi.ExecQuery("Select * from Win32_Process)
    for item in colprocs:
        dictprocesses["Caption"].append(item.Caption)
        dictprocesses["Creation Date"].append(item.CreationDate)
    return dictprocesses

I printed the the dictionary like this...我像这样打印字典...

d = get_processes()
for key, value in d.items():
for v in value:
    print(key, v)

Output for each process:
Caption: <>
Caption: <>
Creation Date: <>
Creation Date: <>

I want to print the dictionary so that it looks like this...我想打印字典,使它看起来像这样......

First Process...
Caption: <>
Creation Date: <>

Second Process...
Caption: <>
Creation Date: <>

Etc...

In your code, the second loop continues to run for every value in a key, that's why the output.在您的代码中,第二个循环继续为键中的每个值运行,这就是 output 的原因。

One of the ways to get the expected output could be,获得预期 output 的方法之一可能是,

d = get_processes()

temp_len = len(d["caption"])
i = 0

for i in range(temp_len):
   for key, value in d.items():
      print(key, value[i])
   i += 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 这是 {'': [ {}, {}, {} ]} Dictionary of List of a Dictionary 吗? 如何拆分内部值以便单独打印/处理它们? - Is this {'': [ {}, {}, {} ]} Dictionary of List of a Dictionary? How can I Split the inside values so I can print/process them separately? 如何使代码从列表中选择一项,将其打印并一次又一次重复该过程? - How can I make the code choose one of the items from the list, print it and repeat the process again and again? 如何从 Python 多处理进程打印? - How can I print from a Python multiprocessing process? 如何使用callables处理python字典? - How can I process a python dictionary with callables? 如何在列表中添加附加字典的每个并行进程的时间戳? - how to add the timestamp of each parallel process appending a dictionary in the list? 我如何在Django中打印“进程状态”? - How I can print 'process status' in Django? 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 如何在 Python 的 GUI 中打印字典中的所有值 - How can i print all values from a dictionary in a GUI in Python 如何在 tkinter 的列表框中打印字典列表 - How can i print a dictionary list in a listbox from tkinter 如何在Python中的字典中打印列表? - How can I print list in dictionary in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM