Python 의 원조 소개
원본 그룹(tuple):원본 그룹 자체 가 변 할 수 없 는 데이터 형식 입 니 다.추가 삭제 하지 않 았 습 니 다.
원본 그룹 에 임의의 데이터 형식 을 저장 할 수 있 습 니 다.
t = (1,2.3,True,'star') ## , ,
print(t)
print(type(t))

원본 그룹 에는 가 변 데이터 형식 이 포함 되 어 있 으 며,원본 그룹의 내용 을 간접 적 으로 수정 할 수 있 습 니 다.
t1 = ([1,2,3],4) ## ,
t1[0].append(4)
print(t1)

원본 그룹 이 하나의 요소 만 있 을 때 뒤에 쉼표 를 붙 여야 합 니 다.그렇지 않 으 면 데이터 형식 이 확실 하지 않 습 니 다.
t2 = ('hello',)
t3 = (1,)
print(type(t2))
print(type(t3))

2.원 그룹의 특성
다음은 예 를 들 어 서브 용 원 조 입 니 다.
allowUsers = ('root','westos','redhat')
allowPasswd = ('123','456','789')
1)색인 과 절편
print(allowUsers[0])
print(allowUsers[-1])
print(allowUsers[1:])
print(allowUsers[2:])
print(allowUsers[:-1])
print(allowUsers[::-1])

2)반복
print(allowUsers * 3)
3)연결
print(allowUsers + ('linux','python'))

4)구성원 조작 부호
print('westos' in allowUsers)
print('westos' not in allowUsers)

5)순환 을 위 한
for user in allowUsers:
print(user)

for index,user in enumerate(allowUsers):
print(' %d : %s' %(index+1,user))

6)zip:두 원조 간 의 원소 가 서로 대응한다

3.원조 의 상용 방법
t = (1,2.3,True,'westos','westos')
print(t.count('westos'))
print(t.index(2.3))

4.원조 의 응용 장면
1)변수 교환 수치
현재 변수 에 값 을 부여 합 니 다.a=1,b=2.어떻게 원 그룹 을 사용 하여 a 와 b 의 값 을 빠르게 바 꿉 니까?
#1. (a,b) (1,2)
#2.b,a=a,b ---> b,a=(1,2)
b = (1,2)[0]
a = (1,2)[1]
print(a)
print(b)
이렇게 하면 a,b 의 값 을 교환 합 니 다.2)인쇄 변수의 값
name = 'westos'
age = 11
t = (name,age)
print('name:%s , age:%d' %(name,age))
print('name:%s , age:%d' %t)

3)원 그룹의 할당,요소 가 있 는 만큼 변 수 를 사용 합 니 다.
t = ('westos',11,100)
name,age,score = t
print(name,age,score)

4)메타 그룹의 할당 을 정렬 합 니 다.
score = (100,89,45,78,65)
# scoreLi = list(score)
# scoreLi.sort()
# print(scoreLi)
scores = sorted(score)
# print(scores)
minscore,*middlescore,maxscore = scores
print(minscore)
print(middlescore)
print(maxscore)
print(' : %.2f' %(sum(middlescore) / len(middlescore)))

총결산
이상 은 이 글 의 모든 내용 입 니 다.본 고의 내용 이 여러분 의 학습 이나 업무 에 어느 정도 참고 학습 가 치 를 가지 기 를 바 랍 니 다.여러분 의 저희 에 대한 지지 에 감 사 드 립 니 다.더 많은 내용 을 알 고 싶다 면 아래 링크 를 보 세 요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.