내가 한 노력들

[ python ] list 에서 value 값으로 다중 index 찾기 본문

IT 공부/python

[ python ] list 에서 value 값으로 다중 index 찾기

JONGI-N CHOI 2020. 11. 18. 11:44
result = [1,2,3,4,5,3]

result 라는  list 에서 원소값이 3인 index번호를 알고 싶을 때는??!

 

print(test_list.index(3))

 

index() 를 사용하게되면 list안에 중복된 value가 있을 때에는 index 번호가 빠른 것만 결가값이 나오게 된다. 

 

결과값

2

 

rest_list = list(filter(lambda x: test_list[x] == 3, range(len(test_list))))

print(rest_list)

list(filter()) 를 이용해서 list안에 중복되는 value값이 있어도 다중의 index 번호를 list의 값을 준다.

 

결과값 

[2, 5]

 

여기서 fillter()는 

 filter 함수는 built-in 함수로 list 나 dictionary 같은 iterable 한 데이터를 특정 조건에 일치하는 값만 추출해 낼때 사용하는 함수이다.

def func(x):

   if x > 0:

     return x

   else:

     return None

 

list(filter(func, range(-5,10)))



>>> [1, 2, 3, 4, 5, 6, 7, 8, 9]

 


참고URL


출처: https://bluese05.tistory.com/66 [ㅍㅍㅋㄷ]