Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
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
Archives
Today
Total
관리 메뉴

RUBY

리스트 곱셈연산, index(), count(), del 키워드(파이썬) 본문

프로그래밍 언어/Python

리스트 곱셈연산, index(), count(), del 키워드(파이썬)

ruby-jieun 2023. 1. 23. 00:41

 

 

리스트 곱셈연산


 

 

 

리스트를 곱셈 연산하면 아이템이 반복된다.

 

 

 

 

 

 

 

 

index() 함수


 

 

 

index(item) 함수를 item의 인덱스를 알아낼 수 있다.

 

 

1부터 10까지의 정수가 중복되지 않고 섞여 있을 때 행운의 숫자 7의 위치를 찾자!

import random
sampleList = random.sample(range(1, 11), 10)
selectIdx = int(input('숫자 7의 위치 입력: '))
searchIdx = sampleList.index(7)
if searchIdx == selectIdx:
    print('정답!')
else:
    print('틀렸어요ㅠㅠ')
print('sampleList: {}'.format(sampleList))
print('searchIdx: {}'.format(searchIdx))
숫자 7의 위치 입력: 3
정답!
sampleList: [9, 5, 1, 7, 2, 4, 10, 8, 3, 6]
searchIdx: 3
숫자 7의 위치 입력: 4
틀렸어요ㅠㅠ
sampleList: [1, 9, 6, 3, 8, 2, 4, 7, 10, 5]
searchIdx: 7

 

 

 

 

count() 함수


 

 

 

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

 

 

 

 

del 키워드


 

 

 

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

 

 

 

 

 

하루 동안 헌혈을 진행한 후 혈액형 별 개수를 파악하는 프로그램을 만들어보자

import random
types = ['A', 'B', 'AB', 'O']
todayData = []
typeCnt = []
for i in range(100):
    type = types[random.randrange(len(types))]
    todayData.append(type)
# print('todayData : {}'.format(todayData))
print('todayData length : {}'.format(len(todayData)))
for type in types:
    print('{}형 \t : {}개'.format(type, todayData.count(type)))
todayData length : 100
A형 	 : 31개
B형 	 : 21개
AB형 	 : 27개
O형 	 : 21개

'프로그래밍 언어 > Python' 카테고리의 다른 글

튜플 인덱스(파이썬)  (0) 2023.01.23
튜플(Tuple)(파이썬)  (0) 2023.01.23
리스트 슬라이싱[n:m](파이썬)  (0) 2023.01.23
리스트 reverse() 함수(파이썬)  (0) 2023.01.23
리스트 sort() 함수(파이썬)  (0) 2023.01.23
Comments