names = ['computer','book', 'abc']
letters = [len(n) for n in names]
κ°μ₯ κΈ΄ μ΄λ¦μ μ°Ύλ μ½λλ₯Ό μμ±νλ€λ©΄,
names = ['computer','book', 'abc']
letters = [len(n) for n in names]
longest_name = None
max_letters = 0
for i in range(len(names)):
count = letters[i]
if count > max_letters:
longest_name = names[i]
max_letters = count
print(longest_name)
> computer
λ‘ μ°Ύμ μ μμ΅λλ€.
νμ§λ§, μΈλ±μ€ iλ‘ λ£¨νμ μ κ·Όνλ κ³Όμ μ΄ λ λ² μΌμ΄λ©λλ€.
μμ μ½λλ₯Ό μ’ λ λͺ
λ£νκ² νκΈ° μν΄ λ΄μ₯ ν¨μ zipμ μ¬μ©ν μ μμ΅λλ€.
νμ΄μ¬μμ zipμ μ΄ν°λ μ΄ν° λκ° μ΄μμ κ°μλλ€. λν κ° μ΄ν°λ μ΄ν°λ‘λΆν° λ€μ κ°μ λ΄μ ννμ μ»μ΄μ΅λλ€.
zip ν¨μ μ¬μ©ν μ½λ
names = ['computer','book', 'abc']
letters = [len(n) for n in names]
longest_name = None
max_letters = 0
for name, count in zip(names, letters):
if count > max_letters:
longest_name = name
max_letters = count
π λ΄μ₯ν¨μ zipμ μ¬μ©ν λλ λ¬Έμ κ° μμ΅λλ€.
μ΄ν°λ μ΄ν°λ€μ κΈΈμ΄κ° λ€λ₯΄λ©΄ zipμ΄ μ λλ‘ μλνμ§ μλλ€λ κ²μ
λλ€.
λ§μ½, names 리μ€νΈμ μλ‘μ΄ λ¨μ΄λ₯Ό μΆκ°
νκ³ , letters μ μΉ΄μ΄ν°λ₯Ό μ
λ°μ΄νΈνμ§ λͺ»νλ€λ©΄
κ²°κ³Όκ° μ μμ μΌλ‘ λμ€μ§ μμ΅λλ€.
names = ['computer','book', 'abc']
letters = [len(n) for n in names]
longest_name = None
max_letters = 0
names.append('helloworld')
for name, count in zip(names, letters):
if count > max_letters:
longest_name = name
max_letters = count
print(longest_name)
> computer
helloworld λ₯Ό append νμ§λ§, lettersμ λ°μμ΄ λμ§ μμκΈ° λλ¬Έμ,
computerκ° μ μΌ κΈ΄ λ¨μ΄λ‘ νλ¦°νΈλ©λλ€.
λ§μ½ zipμΌλ‘ μ€νν 리μ€νΈμ κΈΈμ΄κ° κ°λ€κ³ νμ ν μ μλ€λ©΄, λ΄μ₯ λͺ¨λ itertoolsμ zip_longestλ₯Ό κ³ λ €νλ κ²μ΄ μ’μ΅λλ€.
μΆμ² : νμ΄μ¬ μ½λ©μ κΈ°μ λμ