Logo

Programming-Idioms

History of Idiom 7 > diff from v141 to v142

Edit summary for version 142 by sev:
[Python] get inversion

Version 141

2023-01-25, 13:00:10

Version 142

2023-03-25, 02:08:25

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Idiom #7 Iterate over list indexes and values

Print each index i with its value x from an array-like collection items

Variables
i,x,items
Variables
i,x,items
Extra Keywords
indices traverse traversal
Extra Keywords
indices traverse traversal
Code
for i, x in enumerate(items):
    print i, x
Code
def getInvCount(arr, n):
    inv_count = 0
    for i in range(n):
        for j in range(i + 1, n):
            if (arr[i] > arr[j]):
                inv_count += 1
  
    return inv_count
Doc URL
https://docs.python.org/3/library/functions.html#enumerate
Doc URL
https://docs.python.org/3/library/functions.html#enumerate
Origin
https://stackoverflow.com/a/522578/871134
Origin
https://stackoverflow.com/a/522578/871134