Develop/Algorithm

๋ฐฑ์ค€ 1152) ๋‹จ์–ด์˜๊ฐœ์ˆ˜ [Python]

proggg 2024. 9. 4. 23:24

 

์™œ ํ‹€๋ ธ๋Š”์ง€ ์ •๋ง ๋ชจ๋ฅด๊ฒ ๋Š” . .  ๋ฌธ์ œ์˜€๋‹ค. Python ์„ ์ฃผ ์–ธ์–ด๋กœ ํ•˜๊ณ  ์žˆ์ง€๋งŒ, ๋‚˜๋Š” ์—ญ์‹œ ์ œ๋Œ€๋กœ ์•Œ๊ณ  ์žˆ๋Š”๊ฒŒ ์•„๋‹Œ๊ฐ€๋ณด๋‹ค.

 

์ฒซ๋ฒˆ์งธ ์‹œ๋„. < ์‹คํŒจ > 

print(len(input().split(' ')))

 

 

๋‘๋ฒˆ์งธ ์‹œ๋„. < ์‹คํŒจ >

string = list(input().split(' '))
if '' in string:
    string.remove('')
print(len(string))

 

 

์„ธ๋ฒˆ์งธ ์‹œ๋„. <์‹คํŒจ>

string_list = map(str,input().split(' '))
string_list=list(string_list)
for string in string_list:
    if string == '' or string == ' ':
       # print("this is blank : ",string)
        string_list.remove(string)
print(len(string_list))

 

๊ฐˆ์ˆ˜๋ก ๊ธธ์–ด์ง€๋Š”, ์ฝ”๋“œ๋“ค๊ณผ ์™œ ํ‹€๋ ธ๋Š”์ง€๋ฅผ ์•Œ์•„๋ณด๋ ค๊ณ  ํ–ˆ๋˜ ํ”์ ๋“ค์ด ๋ณด์ธ๋‹ค. 

๊ณ ๋ฏผ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€. ๋ฉ”์†Œ๋“œ๋“ค์„ ๋‹ค ํŒŒํ—ค์ณ๋ณด๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. 

 

๊ทธ ์ค‘, List ์˜ Remove ํ•จ์ˆ˜์˜ ์น˜๋ช…์ ์ธ ์•ฝ์ (?) ๊ณผ ๋‚˜์˜ ๋Œ€์ฑ…์—†์Œ์ด ๋“œ๋Ÿฌ๋‚ฌ๋‹ค.

 

https://docs.python.org/3/tutorial/datastructures.html

 

5. Data Structures

This chapter describes some things you’ve learned about already in more detail, and adds some new things as well. More on Lists: The list data type has some more methods. Here are all of the method...

docs.python.org

 

ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ, List remove ๋Š” Remove the first item from the list -

๊ทธ๋Ÿฌ๋‹ˆ๊นŒ remove ๋ฅผ ์š”์ฒญํ•œ ์•„์ดํ…œ์ค‘์—์„œ ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ์‚ญ์ œํ•˜๋Š”๊ฒƒ์ด๋‹ค. 

์™œ remove ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์•„์ดํ…œ์„ ์‚ญ์ œํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์„๊นŒ... ์ฐธ ๋ฐ”๋ณด๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ ๋‹ค.

 


 

๊ทธ๋ž˜์„œ ๊ฒฐ๋ก ์€ ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๋ชจ๋“  ๊ณต๋ฐฑ๊ณผ null ์„ ์—†์• ์ฃผ๋Š” ์ผ์„ ํ•ด์•ผํ•œ๋‹ค. 

 

list comprehension ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

 

string = input()
word = [word for word in string.split(' ') if word]
print(len(word))