MetalJacket
MetalJacket

Reputation: 71

Get the length of certain values from list

I would like to get the length of certain values in a list like:

lst=['ID_CYTY', 'CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'NAME', 'P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W', 'NZ', 'EG', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS', 'RUT']

I just have another list like:

lst2=['P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W']

I would like to get the length of the values before and after lst2 that are inside lst but the issue here is that lst could change in length from ID_CITY to NAME and NZ to RUT, I mean it could have more or less strings after P to W. Also, do not have to use the names of the strings because they could be different from the 0 index to the last index, example:

lst=['CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'A1', 'P', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'W', 'ND', 'I', 'C', 'IN', 'NZ', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS']

I believe it could be done getting the length of the lst2 and then doing some list indexing to get lengths inside lst.

The lst2 it could have theirs strings mixed but it doesn't matter because it must have the same extension from P to W no matter if starts or ends with those strings.

Note: I would like to get the values before and after lst2 in lst in individual list.

What I mean with before and after lst2 inside lst is: In the first case it would be: The length before P to W:

9

the length from NZ to RUT:

9

But this could be different just how I mentioned before.

Upvotes: 1

Views: 118

Answers (5)

Alexander
Alexander

Reputation: 11

#lst=['ID_CYTY', 'CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'NAME', 'P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W', 'NZ', 'EG', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS', 'RUT']
lst=['CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'A1', 'P', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'W', 'ND', 'I', 'C', 'IN', 'NZ', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS']
lst2=['P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W']

for i in lst:
    if i in lst2:
        start = lst.index(i)
        break
for i in lst[::-1]:
    if i in lst2:
        end = lst.index(i)
        break

print(len(lst[:start]))
print(len(lst[end:-1]))

Now even if both lists changed it finds the first and last value to calculate from

Upvotes: 1

Alexander
Alexander

Reputation: 11

lst=['ID_CYTY', 'CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'NAME', 'P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W', 'NZ', 'EG', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS', 'RUT']
lst2=['P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W']

start = lst.index(lst2[0])
end = lst.index(lst2[-1])

print(len(lst[:start]))
print(len(lst[end:-1]))

>>>
9
9
>>>

Even if the list changes it still returns the difference

lst=['CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'A1', 'P', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'W', 'ND', 'I', 'C', 'IN', 'NZ', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS']

>>>
8
11
>>>

Upvotes: 0

coderoftheday
coderoftheday

Reputation: 2075

This does what comment 'What I was thinking is first get the len(lst2) and then do some string indexing to get the first and last values in differents variables. The len of lst2 always would be a section or slide of lst. –' says.

lst=['ID_CYTY', 'CITY', 'ID_STATE', 'STATE', 'ID_COUNTRY', 'COUNTRY', 'AGE', 'SEX', 'NAME', 'P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W', 'NZ', 'EG', 'LOS', 'TO', 'LS', 'EST', 'TR', 'OBS', 'RUT']
lst2=['P', 'A1', 'PR', 'V', 'Z', 'M1', 'D1', 'R1', 'CPD', 'CA', 'IN', 'ND', 'I', 'C', 'W']

legnth_1= len(lst[:lst.index(lst2[0])])
legnth_2 =  len(lst[lst.index(lst2[-1])+1:])

print(legnth_1)
print(legnth_2)


>>> 9
>>> 9

Upvotes: 0

Barmar
Barmar

Reputation: 782717

See Python: return the index of the first element of a list which makes a passed function true for how to find the index in a list of the first element that matches a condition.

In this case, the length of the list before lst2 is the index of the first element that's in lst2.

before = next(i for i,v in enumerate(lst) if v in lst2)

To get the length of the list after lst2, do the same thing but reverse lst first.

after = next(i for i,v in enumerate(lst[::-1]) if v in lst2)

Upvotes: 1

Alexander
Alexander

Reputation: 11

You could use

start = lst.index(lst2[0])

This will give you the position, in lst, of the first value in lst2

end = lst.index(lst2[-1])

This will give you the position, in lst, of the last value in lst2

print(len(lst[0:start]))
print(len(lst[end:-1]))

This prints the lengths between the values

Upvotes: 0

Related Questions