繁体   English   中英

Python-计数器将列表中的元素合并在一起

[英]Python - Counter merges elements from list together

我有一个完整的Windows API调用列表:

 listOfSequences = 
    ['GetSystemDirectoryA',
     'IsDBCSLeadByte',
     'LocalAlloc',
     'CreateSemaphoreW',
     'CreateSemaphoreA',
     'GlobalAddAtomW',
     'lstrcpynW',
     'LoadLibraryExW',
     'SearchPathW',
     'CreateFileW',
     'CreateFileMappingW',
     'MapViewOfFileEx',
     'GetSystemMetrics',
     'RegisterClipboardFormatW',
     'SystemParametersInfoW',
     'GetDC',
     'GetDeviceCaps',
     'ReleaseDC', ...... and so on .....]

由于其中一些事件发生多次,因此我想收集它们的发生次数。 因此,我使用了collections.Counter。 但这将一些API连接在一起:

lCountedAPIs = Counter(listOfSequences)

当我打印lCountedAPI时 ,得到以下提示:

Counter({'IsRectEmptyLocalAlloc': 2,
         'DdePostAdvise': 3,
         'DispatchMessageWGetModuleFileNameA': 2,
         'FindResourceExW': 50318,
         'ReleaseDCGetModuleFileNameW': 7,
         'DefWindowProcAGetThreadLocale': 1,
         'CoGetCallContext': 40,
         'CoGetTreatAsClassGetCommandLineA': 1,
         'GetForegroundWindowGetSystemDirectoryW': 1,
         'GetModuleHandleWGetSystemTimeAsFileTime': 2,
         'WaitForSingleObjectExIsChild': 1,
         'LoadIconAGetWindowsDirectoryW': 2,
         'GlobalFreeLocalAlloc': 10,
         'GetMapModeCreateSemaphoreW': 1,
         'HeapLock': 11494,                  <---------- A
         'CharNextAGetCurrentProcessId': 11, <---------- B
         'RemovePropWGetStartupInfoA': 1,
         'GetTickCountGetVersionExW': 55,

因此,例如: HeapLock (参见A)未与其他API合并,但CharNextAGetCurrentProcessId串联(参见B)

有人可以告诉我为什么会这样以及如何解决吗?

在此先感谢&最好的问候:)

检查您的列表定义。 Python将相邻的字符串文字串联在一起,因此您一定在中间的某个地方错过了逗号:

listOfSequences = [
    'GetSystemDirectoryA',
    'IsDBCSLeadByte',
    'LocalAlloc',
    ...
    'CharNextA'
    #          ^ comma missing here
    'GetCurrentProcessId',
    ...
]

这伤了我好几次。

Counter没有做任何事情。 您必须在listOfSequences出现11次'CharNextAGetCurrentProcessId' 您可以通过运行'CharNextAGetCurrentProcessId' in listOfSequences

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM