Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Java Rome
- 데스크톱
- Blog Code
- mstsc
- nmon
- 3389
- RESTful
- RESTful Web Service
- skip-grant
- error 1418
- JNDI
- error 1045
- JCommander
- Python
- sorted
- Dictionary
- jersey
- REST API
- RESTful java
- Source Code
- 윈도우즈 원격 데스크톱
- DataSource
- Java
- SyntaxHighlighter
- error 1298
- Advanced REST client
- Chrome REST
- list
- error 145
- python3
Archives
- Today
- Total
그래도 개발자일 때 좋았다
Python Dictionary List 검색 본문
Python에서 List가 Dictionary 형태의 데이터를 갖고 있을 때, Dictionary의 특정 Value의 값으로 검색을 하는 경우가 있을 수 있다. 예를 들어서 아래와 같은 List가 있다고 가정해보자.
[
{'Name': 'Tom', 'Age': 30},
{'Name': 'Jack', 'Age': 31},
{'Name': 'Sue', 'Age': 32}
]
이 때, Name값이 Tom인 Dictionary의 Age값을 뽑아올 때는 Generator Expressions를 사용하여 아래와 같은 Code를 사용하면 된다.
>>> list = [
... {'Name': 'Tom', 'Age': 30},
... {'Name': 'Jack', 'Age': 31},
... {'Name': 'Sue', 'Age': 32}
... ]
>>> # Name이 Tom인 Dictionary를 List에서 가져오기
... tom = (item for item in list if item['Name'] == 'Tom')
>>> # 만약에 값이 없을 경우 False가 dict에 저장된다.
... # Tom이라는 값이 더 있을 경우 next시 그 다음 Dictionary를 가져오게 된다.
... dict = next(tom, False)
>>> dict
{'Name': 'Tom', 'Age': 30}
>>> dict['Age']
30
참고:
http://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search
'Development > Python' 카테고리의 다른 글
Python Dictionary List 정렬 (0) | 2018.03.29 |
---|
Comments