Python μ For Statement μ κ΄ν κΈμ
λλ€.
μ§λ¬Έ ¶
μ½κ°..λ³νμΌλ‘..
κ²°κ³Όλ μμκ²μ΄λ.μμ κ°μ..μ΄μ λ λΉμ°ν λͺ¨λ₯΄κ² μ΄μ...λκ°...;;;;
λ΅λ³ 1 ¶
μ΄λ»κ² κ°μ λμμ νλμ§ μ΄μ λ₯Ό λͺ¨λ₯΄κ² λ€κ³ νμ§.
μ΄μ λ hello worldλΌλ λ¬Έμμ΄ a μμ C++μ λ°°μ΄κ³Ό λΉμ·νκ² μΈ μ μκΈ° λλ¬Έμ΄μΌ. hλ
a[0]
, e λ a[1]
... μ΄λ°μμΌλ‘ λ§μ΄μΌ. νμ§λ§ λ°°μ΄ μμμ κ°μ λ°κΏ μλ μλ€λ μ μ΄ C++μ λ°°μ΄νκ³ λ€λ₯Έ μ μ΄μ§.
λΉμ·ν κ²μ μ°Ύμλ©΄ C++μ λ°°μ΄λ³΄λ€λ C++μ string ν΄λμ€, Javaμ String ν΄λμ€μ λΉμ·νλ€λ μ μ μ μ μμ΄. -- Leonardong
λ΅λ³ 2 ¶
pythonμ for statement λ¬Έλ²μ κ·Όμμ μΈ μ°¨μμΌλ‘ μ κ·Όν΄ λ΄
μλ€.
pythonμ λ€λ¦
λλ€.
~cpp for i in 'helloworld':print i, for i in [ 'h','e','l','l','o','w','o','r','l','d']:print i,μ§λ¬Έκ³Ό κ°μ λ§₯λ½μ μ΄ λλμμ΄ κ°μμ§ λ°μ Έ λ³ΌκΉμ?
μ°μ for λ¬Έμ μ μλ₯Ό μμ λ΄
μλ€.
μνμ€μ μ μλ λ€μκ³Ό κ°μ΅λλ€.
{{|
Sequences
These represent finite ordered sets indexed by non-negative numbers. The built-in function len() returns the number of items of a sequence. When the length of a sequence is n, the index set contains the numbers 0, 1, ..., n-1. Item i of sequence a is selected by ai.
...
~cpp for statement definition for_stmt ::= "for" target_list "in" expression_list ":" suite ["else" ":" suite] --- μμλ¨Ήκ² μ μ΄λ³΄μλ©΄, for νκ²κ°μ²΄λ¦¬μ€νΈ(target) in μνμ€ν(expression_list==sequence): μ(expression)in λ€μμ μμΌν λ¬Έμ μνμ€ν μ λλ€.
μνμ€μ μ μλ λ€μκ³Ό κ°μ΅λλ€.
{{|
Sequences
These represent finite ordered sets indexed by non-negative numbers. The built-in function len() returns the number of items of a sequence. When the length of a sequence is n, the index set contains the numbers 0, 1, ..., n-1. Item i of sequence a is selected by ai.
...
μΉμ ν ν΄μ .png)
.png)
μνμ€
μμκ° μλμλ‘ μ νλμ΄ μμΌλ©° μμκ° λ§€κ²¨μ§ μ§ν©. λ΄μ₯ν¨μμΈ len()λ μνμ€μ μμ΄ν κ°―μλ₯Ό λ°ννλ€. μνμ€μ κΈΈμ΄λΌ nμ΄λ©΄, μΈλ±μ€λ 0..n-1 κΉμ§μ λ²μλ₯Ό κ°μ§λ€. μνμ€μ μμ΄ν iλ aiλ‘ μ νλλ€.
μμκ° μλμλ‘ μ νλμ΄ μμΌλ©° μμκ° λ§€κ²¨μ§ μ§ν©. λ΄μ₯ν¨μμΈ len()λ μνμ€μ μμ΄ν κ°―μλ₯Ό λ°ννλ€. μνμ€μ κΈΈμ΄λΌ nμ΄λ©΄, μΈλ±μ€λ 0..n-1 κΉμ§μ λ²μλ₯Ό κ°μ§λ€. μνμ€μ μμ΄ν iλ aiλ‘ μ νλλ€.
... μ¬λΌμ΄μ± κ΄λ ¨ μ μ...νλ΅
|}}
λ΄μ₯νμ μνμ€λ 2.3 κΈ°μ€μΌλ‘ μ¬μ―κ°μ§κ° 미리 μ μ λμ΄ μμ΅λλ€.
{{|There are six sequence types: strings, Unicode strings, lists, tuples, buffers, and xrange objects|}}
λ΄μ₯νμΈ strings λ μνμ€ νμ΄λ©°,
|}}
λ΄μ₯νμ μνμ€λ 2.3 κΈ°μ€μΌλ‘ μ¬μ―κ°μ§κ° 미리 μ μ λμ΄ μμ΅λλ€.
{{|There are six sequence types: strings, Unicode strings, lists, tuples, buffers, and xrange objects|}}
λ΄μ₯νμΈ strings λ μνμ€ νμ΄λ©°,
a[i]
ννλ‘ μ κ·Όν μ μμ΅λλ€. λ°λΌμ, for i in 'helloworld': print i,
μμ iλ λ°λ³΅μ μΌλ‘ 'h','e'... μ λ°ννλ©°
μμ κΈ°μ λλλ‘ listν μμ μνμ€ νμ΄λ©°,
a[i]
ννλ‘ μ κ·Όν μ μμ΅λλ€.for i in [ 'h','e','l','l','o','w','o','r','l','d']:print i,λ μμ iκ° 'h','e'.. λ₯Ό λ°λ³΅νκ² λ©λλ€.
μ¬κΈ°κΉμ§ μμ 보μλ €λ©΄, Python Language Referenceμμ sequence, for statementλ‘ μ΄μ¬ν μ°Ύμ 보μλ©΄ λ©λλ€. μ΄νκ°μ νμ΄μ¬μλ μ λμμμ΅λλ€. κ·Έλ¦¬κ³ λ€μμ μ΄μΌκΈ°λ€μ λ€λ₯Έ μΈμ΄λ₯Ό μ’ μμλ©΄ μ΄ν΄κ°μ€ κ²λλ€.
C / Java 1.4 μ΄νλ²μ μ for μ μ΄λ¬Έμ κ°μ²΄μ νΉμ±μ λ°λΌ λμνμ§ μμ΅λλ€. μ΄ μΈμ΄λ€μμ forλ¬Έμ μ ν΄μ§ 쑰건문μ κ²μ¬νλ©΄μ, νμΆν©λλ€. μ¦, whileλ¬Έμ μ¬λμ΄ μ½κΈ° μ½κ² μ½κ° κ³ μ°¨μμΌλ‘ μΆμνλ ννμ λΆκ³Όν©λλ€.
C/Java1.4μ΄ν μ Pythonμ forλ¬Έμ λν κ΄μ μ΄ μ ν λ€λ¦
λλ€. κ·Έλ¦¬κ³ μ μ©νμ§μ. Cμ forλ¬Έκ³Ό ꡬλΆνκΈ° μνμ¬ pythonμ μ΄λ¬ν forλ¬Έμ λ³΄ν΅ for each λ¬Έμ΄λΌκ³ λΆλ¦
λλ€. μ΄κ² μ§μ§ forλ¬Έ μ΄λΌκ³ μ΄μΌκΈ°λ€ νμ§μ.
μ C++μ μλλλλ©΄, C++μ μ μ΄λ¬Έμ΄ Cλ¬Έλ²μ μ’
μλμ΄ μκ³ , Cμμλ λ°°μ΄κ³Ό κ°μ΄ μ£Όμλ₯Ό ν΅ν μΈλ±μ€λ‘ μ κ·Όνλ νλ€μ΄ μ’
λ£ μΈλ±μ€μ λν μ λ³΄κ° μμ΄μ ꡬνμ΄ λΆκ°λ₯ν©λλ€. μΆμν μμΌ C++μμλ STLμ for_each(..) λΌλ ν¨μλ‘ λΉμ·ν κ²μ΄ ꡬνλμ΄ μκΈ°λ ν©λλ€.
Java 1.5 μ advanced for statement λΌλ μ΄λ¦μΌλ‘ λΉμ·ν κ²μ΄ μΆκ°λμκ³ , C#μλ μΈμ΄κ° νμ λ λ λΆν° μμμ΅λλ€. Java 1.5μμλ μλ
κ° λ
Όμ λμ λμ
μ νλλ°, μΈμ΄μ λ
Ήμ΄κΈ° μν΄μλ Autoboxing/Unboxingκ³Ό νΈλ¦¬μ±μ μν΄ Templateκ³Ό κ°μ μ¬λ¬ νμλΆκ°κ²°νκ³ λ³΅μ‘λ€λ(?)ν κ°λ
μ΄ ν¨κ» μΆκ°λμμ΅λλ€.
ref : Python Lib ref, Python Language ref, μ΄κ°νμ΄μ¬
--NeoCoin
--NeoCoin