Python 기본--목록

高洛峰
풀어 주다: 2017-02-17 11:41:12
원래의
1204명이 탐색했습니다.

목록

목록은 가장 일반적으로 사용되는 데이터 유형 중 하나입니다. 목록은 데이터에 대한 가장 편리한 저장, 수정 및 기타 작업을 수행할 수 있습니다

1. 목록 정의

fruits = ['apple','banana','orange']
로그인 후 복사

2. 첨자를 통해 목록의 요소에 액세스하고 첨자는 0

>>> fruits[0]
'apple'
>>> fruits[2]
'orange'
>>> fruits[-1]
'orange'
>>> fruits[-2]
'banana'
로그인 후 복사

부터 계산되기 시작합니다. 3. Slice

>>> fruits = ['apple','banana','orange','peal','grape']
>>> fruits[1:4]    #取下标1到下标4之间的数,包括1但不包括4
['banana', 'orange', 'peal']
>>> fruits[1:-1]   #取下标1至-1之间的数,不包括-1
['banana', 'orange', 'peal']
>>> fruits[0:3]    #从头开始取,不包括3
['apple', 'banana', 'orange']
>>> fruits[:3]     #和上句一样
['apple', 'banana', 'orange']
>>> fruits[3:]     #从下标3到最后,到末尾只能这样取
['peal', 'grape']
>>> fruits[0::2]   #从头开始,步长为2,即隔一个取一个
['apple', 'orange', 'grape']
>>> fruits[::2]    #和上句一iy
['apple', 'orange', 'grape']
로그인 후 복사

4. >5 .요소 삽입, insert()

아래 첨자 1에 수박 삽입

>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape']
>>> fruits.append('newpeach')
>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
로그인 후 복사

6. 목록의 요소 수정

바나나를 체리로 변경

['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.insert(1,'watermelon')
>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
로그인 후 복사

7. 삭제

pop()은 기본적으로 마지막 요소를 삭제한 후 요소를 반환합니다

>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits[2]='cherry'
>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
로그인 후 복사

8. Extend Extension()

>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
>>> del fruits[2]    #删除第二个元素
>>> fruits
['apple', 'watermelon', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.remove('orange')     #删除指定的元素
>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'newpeach']
>>> fruits.pop()    #删除最后一个元素
'newpeach'
>>> fruits
['apple', 'watermelon', 'peal', 'grape']
로그인 후 복사

9. 복사 ()

 >>> fruits
['apple', 'watermelon', 'peal', 'grape']
>>> vegetable = ['radish','cabbage','cucumber']
>>> fruits
['apple', 'watermelon', 'peal', 'grape']
>>> vegetable
['radish', 'cabbage', 'cucumber']
>>> fruits.extend(vegetable)
>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
로그인 후 복사

10. 통계 count()

['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits2 = fruits.copy()
>>> fruits2
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
로그인 후 복사

11. sort()를 정렬하고 reverse()

 >>> fruits.count('apple')
 1
로그인 후 복사

를 가져옵니다. 12. >

>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits.sort()
>>> fruits
['apple', 'cabbage', 'cucumber', 'grape', 'peal', 'radish', 'watermelon']
>>> fruits.reverse()
>>> fruits
['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
로그인 후 복사

더 많은 Python 기본 목록 관련 기사를 보려면 PHP 중국어 웹사이트를 주목하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!