0

I have a list full of Windows API calls:

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

Since some of them occurs several times, I wanted to collected their number of occurences. Thus, I used collections.Counter. But it concatenates some APIs together:

lCountedAPIs = Counter(listOfSequences)

when I print the lCountedAPIs I get the folowing:

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,

So for ex.: HeapLock (see A) was not merged with another API But CharNextA was concatenated with GetCurrentProcessId (see B)

Can somebody tell me why this happens and how to fix that ?

Thanks in advcance & best regards :)

aminakoy
  • 413
  • 1
  • 5
  • 13
  • It is duplicated at https://stackoverflow.com/questions/2161752/how-to-count-the-frequency-of-the-elements-in-a-list – caot Jul 22 '17 at 14:59
  • 1
    @caot: no, the question isn't about using `Counter`, but about an hypothetic "bug". – Jean-François Fabre Jul 22 '17 at 15:01
  • @aminakoy search "In Python 2.7, you can use collections.Counter" in the page. Another duplicated at https://stackoverflow.com/questions/3496518/python-using-a-dictionary-to-count-the-items-in-a-list?noredirect=1&lq=1 – caot Jul 22 '17 at 15:02

2 Answers2

4

Check your list definition. Python concatenates adjacent string literals, so you must have missed a comma somewhere in the the middle:

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

This has bitten me several times.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
1

Nothing in Counter does that. You must necessarily have 11 occurrences of 'CharNextAGetCurrentProcessId' in listOfSequences. You can check this by running 'CharNextAGetCurrentProcessId' in listOfSequences.

fuglede
  • 17,388
  • 2
  • 54
  • 99