๐Ÿ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ๊ตฌ๋ฌธ ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • ํŠœํ”Œ(tuple)์™€ ๋ฆฌ์ŠคํŠธ(list) ๋น„๊ต
  • ํŠœํ”Œ
  • ํŠœํ”Œ์˜ ์‚ญ์ œ์™€ ๋ณ€๊ฒฝ : error ๋ฐœ์ƒ
  • ํŠœํ”Œ ๋‹ค๋ฃจ๊ธฐ

Was this helpful?

  1. python

tuple

ํŠœํ”Œ(tuple)์™€ ๋ฆฌ์ŠคํŠธ(list) ๋น„๊ต

: ๋ช‡ ๊ฐ€์ง€ ์ ์„ ์ œ์™ธํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ์™€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๋‹ค

  • ๋ฆฌ์ŠคํŠธ(list)๋Š” [ ]์œผ๋กœ ๋‘˜๋Ÿฌ์‹ธ์ง€๋งŒ ํŠœํ”Œ(tuple)์€ ( )์œผ๋กœ ๋‘˜๋Ÿฌ์‹ผ๋‹ค.

  • ๋ฆฌ์ŠคํŠธ(list)๋Š” ๊ทธ ๊ฐ’์˜ ์ƒ์„ฑ, ์‚ญ์ œ, ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ํŠœํ”Œ(tuple)์€ ๊ทธ ๊ฐ’์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†๋‹ค.

  • ์ด์ฒ˜๋Ÿผ ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ๊ทธ ๊ฐ’์ด ํ•ญ์ƒ ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ๋ฅผ ๋ฐ”๋ž€๋‹ค๋ฉด ํŠœํ”Œ(tuple)์„ ์‚ฌ์šฉํ•œ๋‹ค

ํŠœํ”Œ

t1 = () # ๊ธฐ๋ณธ 
t2 = (1,) # ๋ฆฌ์ŠคํŠธ์™€ ๋‹ค๋ฅด๊ฒŒ 1๊ฐœ์˜ ์š”์†Œ๋งŒ ๊ฐ€์งˆ๋•Œ๋Š” ์ฝค๋งˆ(,)๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค
t3 = (1, 2, 3)
t4 = 1, 2, 3 # ๊ด„ํ˜ธ()๋ฅผ ์ƒ๋žตํ•ด๋„ ๋ฌด๋ฐฉํ•˜๋‹ค 
t5 = ('a', 'b', ('ab', 'cd'))

ํŠœํ”Œ์˜ ์‚ญ์ œ์™€ ๋ณ€๊ฒฝ : error ๋ฐœ์ƒ

  • ํŠœํ”Œ(tuple)์€ ์š”์†Ÿ๊ฐ’์„ ์‚ญ์ œ ๋ฐ ๋ณ€๊ฒฝํ•˜๋ฉด error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค

    ```python

    t1 = (1, 2, 'a', 'b')

    del t1[0]

Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object doesn't support item deletion

```python
t1 = (1, 2, 'a', 'b')
t1[0] = 'c'

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

ํŠœํ”Œ ๋‹ค๋ฃจ๊ธฐ

  • ์ธ๋ฑ์‹ฑ

    t1 = (1, 2, 'a', 'b')
    print(t1[0]) # 1
  • ์Šฌ๋ผ์ด์‹ฑ

    t1 = (1, 2, 'a', 'b')
    print(t1[1:]) # (2, 'a', 'b')
  • ๋”ํ•˜๊ธฐ

    t1 = (1, 2, 'a', 'b')
    t2 = (3, 4)
    print(t1 + t2) # (1, 2, 'a', 'b', 3, 4)
  • ๊ณฑํ•˜๊ธฐ

    t2 = (3, 4)
    print(t2 * 3) # (3, 4, 3, 4, 3, 4)
  • ๊ธธ์ด ๊ตฌํ•˜๊ธฐ

    t1 = (1, 2, 'a', 'b')
    print(len(t1)) # 4
PreviousBooleanNextpackage

Last updated 4 years ago

Was this helpful?