[자료구조] List의 나머지 기능들

신은지·2024년 7월 10일

Data Structure

목록 보기
14/31
post-thumbnail

곱셈 연산

list를 곱셈 연산하면 아이템이 반복된다.


index(item) 함수

index(item) 함수를 이용해 item의 index를 알아낼 수 있다.


count() 함수

count() 함수를 이용해 특정 아이템의 개수를 알아낼 수 있다.


del 키워드

del 키워드를 이용해 특정 아이템을 삭제할 수 있다.


💡Python으로 list item 위치 찾기

# 1부터 10까지의 정수가 중복되지 않고 섞여 있을 때 행운의 숫자 7의 위치 찾기
import random
randomList = random.sample(range(1, 11), 10)
selectIdx = input('7의 위치: ')
it = randomList.index(7)

if it == selectIdx:
    print('정답~!!')
else:
    print('꽝~!!')

print(randomList)
print('7의 위치: {}'.format(it))

💡Python으로 list item 별 count 하기

# 하루 동안 헌혈을 진행한 후 혈액형 별 개수를 파악
import random

bloodType = ['A', 'B', 'O', 'AB']
visitor = []
typeCnt = []

for i in range(100) :
    type = bloodType[random.randrange(len(bloodType))]
    visitor.append(type)

print('visitor: {}'.format(visitor))
print('visitor length: {}'.format(len(visitor)))

for type in bloodType:
    print('{}형 \t : {}개'.format(type, visitor.count(type)))





* 이 글은 제로베이스 데이터 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.

profile
I believe there is no best, only better

0개의 댓글