Development/Python
반복문 돌려가며 Dictionarys in Array 로 만들기
evagrim
2022. 8. 28. 11:07
프론트와 데이터 주고 받을 때
api 사용할 때
DB에 낑겨넣을 때
제일 많이 사용하는 형태라 어떻게 만들지 고민해봤다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
for location in locations:
a = location.select_one('div > div.header-wrap > div.header-inner-wrap > h2')
b = location.select_one('div > div.content-wrap > div.description-wrap > p')
if a is not None or b is not None:
title = a.text
desc = b.text
# print(title + '\n' + desc + '\n' + image + '\n')
# 반복문 돌아가며 딕셔너리 배열로 만들기
locationlist += [{
'title': title,
'desc': desc
}]
|
cs |
[ {'title':'title','desc':'desc'}, {'title':'title','desc':'desc'}, {'title':'title','desc':'desc'} ]
위와 같은 형태로 만들어진다
배열끼리 합칠 때 그냥 + 하면 되길래 해봤는데 된다
다른 분이 만든걸 봤는데 list에 append 메소드를 사용한다.
append도 사용해 봐야겠다