일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- ros2 튜토리얼 환경설정
- 코드업
- nav2 dev contatiner
- ros2 transformations 개념
- Nav2 document
- ros2 foxy docker
- nav2 development guides
- ros2 development guides
- ROS FOXY 튜토리얼
- nav2 getting started
- ros2 configuring environment
- CODEUP 6073
- ros2 튜토리얼
- nav2 튜토리얼
- ros2 foxy tutorial
- first-time robot setup guide
- humble development guides
- humble 환경설정
- CodeUp
- ros2 remapping
- ros2 환경설정
- nav2 first-time robot setup guide
- Python
- docker foxy
- setting up transformations
- error
- foxy nav2
- Foxy tutorial
- nav2 설치
- nav2 tutorial
Archives
- Today
- Total
BAN2ARU
[Python] append(), extend(), insert() 비교 본문
반응형
Python에서는 요소를 추가하는 함수로 append(), extend(), insert()가 있는데 각각 어떠한 차이가 있는지 알아보자.
간략하게, 한 줄로 각 함수를 설명하면 다음과 같다.
append()
list.append(x)로 활용하며, list의 끝에 x와 같은 요소를 더해준다.
[기존의 list 마지막 위치에 다른 요소 추가]
extend()
list.extend(iterable)로 활용하며, list의 끝에 iterable의 요소들을 더해준다.
[기존의 list에 다른 list 추가]
insert()
list.extend(i,x)로 활용하며, list의 인데스 i 위치에 x와 같은 요소를 더해준다.
[기존의 list의 지정한 위치에 다른 요소 추가]
<코드로 보는 예시>
# append() 예시 : list의 마지막에 값이 추가됨
>> temp_list = [1,2,3,4]
>> temp_list.append(5)
>> temp_list
[1,2,3,4,5]
# 만약, list를 추가하게 된다면 : list 그대로 추가되는 것을 볼 수 있음
>> temp_list = [1,2,3,4]
>> temp_list.append([5,6,7])
>> temp_list
[1,2,3,4,[5,6,7]]
# 만약 list 내부에 추가하고 싶다면 ?? extend()를 활용하자
>> temp_list = [1,2,3,4]
>> add_list = [5, 6, 7]
>> temp_list.extend(add_list)
[1,2,3,4,5,6,7]
# extend()에 요소를 넣게 된다면 다음과 같은 오류 발생
>> temp_list = [1,2,3,4]
>> temp_list.extend(5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
# insert() 예시 : list에서 원하는 index에 값 추가
>> temp_list = [1,2,3,4]
>> temp_list.insert(1, 5)
>> temp_list
[1,5,2,3,4]
728x90
'Language > Python' 카테고리의 다른 글
[Python] 문자열 포맷팅 - format 및 f-string(formated string literal) (0) | 2022.04.26 |
---|---|
[Python] print() - sep, end, file (print로 텍스트 파일 출력하기) (0) | 2022.04.25 |
[Python / Pytorch] torch.nn.ReLU() (0) | 2022.04.21 |
[Python/ Pytorch] torch.nn.ModuleList() (0) | 2022.04.21 |
[Python/ Pytorch] torch.nn.BatchNorm2d() (0) | 2022.04.21 |
Comments