๊ด€๋ฆฌ ๋ฉ”๋‰ด

DOing

[์ž๋ฃŒ๊ตฌ์กฐ] ๋ฐฐ์—ด with Python ๋ณธ๋ฌธ

์•Œ๊ณ ๋ฆฌ์ฆ˜

[์ž๋ฃŒ๊ตฌ์กฐ] ๋ฐฐ์—ด with Python

mangdo 2021. 5. 18. 11:02

๐Ÿ’ก ๋ฐฐ์—ด(Array)

๋ฐฐ์—ด์€ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด๋‹ค.

 

[ ๋ฐฐ์—ด์˜ ์žฅ์  ]

: ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•œ ๋น ๋ฅธ ์ ‘๊ทผ ๊ฐ€๋Šฅ

 

[ ๋ฐฐ์—ด์˜ ๋‹จ์  ]

: ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€/์‚ญ์ œ๊ฐ€ ์‰ฝ์ง€์•Š๋‹ค.

: ๋ฏธ๋ฆฌ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.

 

ํŒŒ์ด์ฌ์€ ๋ฐฐ์—ด๋ณด๋‹ค ์ถ”๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋‹จ์ ์ด ์™€๋‹ฟ์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

country='US';
country=country+'a'

ํ•˜์ง€๋งŒ ๋ณธ๋ž˜์˜ C์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๋ฐฐ์—ด์˜ ๋‹จ์ ์ด ์™€๋‹ฟ๋Š”๋‹ค.

#include<stdio.h>
int main(int argc, char * argv[]){
    char country[3] ='US'; # ๋ฏธ๋ฆฌ ์ตœ๋Œ€๊ธธ์ด๋ฅผ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค, ์ด ๊ธธ์ด๋ฅผ ๋„˜์„ ์ˆ˜ ์—†๋‹ค.
    printf("%c%c\n", country[0], country[1]);
    retrun 0;
}

 

ํŒŒ์ด์ฌ ๋ฐฐ์—ด?

ํŒŒ์ด์ฌ์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์ด ๋ฐฐ์—ด๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

ํŒŒ์ด์ฌ์˜ ๋ฆฌ์ŠคํŠธ๋Š” ์›๋ž˜ ๋ฐฐ์—ด๋ณด๋‹ค๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ๋‹ค.

# 1์ฐจ์› ๋ฐฐ์—ด
data = [1,2,3,4,5]

# 2์ฐจ์› ๋ฐฐ์—ด
data = [[1,2,3], [4,5,6], [7,8,9]]

print(data[0]) #[1,2,3]
print(data[0][1]) #2

 


 

์—ฐ์Šต๋ฌธ์ œ 1. ๋‹ค์Œ dataset์—์„œ ์ „์ฒด ์ด๋ฆ„์•ˆ์— M์ด ๋ช‡๋ฒˆ ๋‚˜์™”๋Š”๊ฐ€?

dataset = [MarMia, Media]
m_count = 0
for data in dataset:
	for index in range(len(data)):
    	if data[index] == 'M':
        	m_count += 1