Python/Data Structure & Algorithm in Python
[Python 기초] 선형 배열(Linear Array)
Air’s Big Data
2020. 9. 9. 21:12
#Python 리스트에 활용할 수 있는 연산들
(1) 리스트 길이과 관계 없이 빠르게 실행 결과를 보게되는 연산들
- 원소 덧붙이기 .append()
- 원소 하나를 꺼내기 .pop()
(2) 리스트의 길이에 비례해서 실행 시간이 걸리는 연산들
- 원소 삽입하기 .insert()
- 원소 삭제하기 .del()
#리스트에서 원소 찾아내기
Example 1:
Input:
L = [64, 72, 83, 72, 54]
x = 72
Output:
[1, 3]
Example 2:
Input:
L = [64, 72, 83, 72, 54]
x = 83
Output:
[2]
Example 3:
Input:
L = [64, 72, 83, 72, 54]
x = 49
Output:
[-1]
Solution 1:
def solution(L,x):
result = list()
for i in range(len(L)):
if L[i] == x:
result.append(i)
if result == []:
result = [-1]
return result