일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Python
- implicitData
- gluonnlp
- LatentFactorModel
- numpy.bool
- Colab
- MatrixFactorization
- wordembedding
- jsonl
- Convert
- session-basedRecommendation
- Cast
- VScodeNotResponding
- ExplicitData
- 지도시각화
- BloombergMarketConcepts
- iterrows
- sshtunnel
- github2FA
- 텐서플로자격증
- DIF_SR
- jsonlines
- pandas
- MySQL
- decimal error
- Visualization
- str.replace
- json
- vscode
- TensorflowDeveloperCertificate
- Today
- Total
garret
[Python] slice()함수 vs. islice() 함수 본문
islice()와 slice() 모두 슬라이싱하는데 사용되는 함수다.
islice()를 공부하다가 slice()의 차이점이 어떤건지 궁금해서 정리
slice() 정의
Return a slice object representing the set of indices specified by range(start, stop, step). The start and step arguments default to None. Slice objects have read-only data attributes start, stop, and step which merely return the argument values (or their default). They have no other explicit functionality; however, they are used by NumPy and other third-party packages. Slice objects are also generated when extended indexing syntax is used. For example: a[start:stop:step] or a[start:stop, i].
range로 구체화된 indices의 집합을 나타내는 slice 객체 반환.
- slice 객체를 [] 연산자를 사용해 시퀀스에 적용하면, 해당 범위의 원소들로 이루어진 새로운 시퀀스가 생성됨
slice(stop)
slice(start,stop [, step])
slice() 사용예시
my_list = [1, 2, 3, 4, 5]
my_slice = slice(1, 4, 2)
new_list = my_list[my_slice]
print(new_list)
islice() 정의
Make an iterator that returns selected elements from the iterable. If start is non-zero, then elements from the iterable are skipped until start is reached. Afterward, elements are returned consecutively unless step is set higher than one which results in items being skipped. If stop is None, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position.
즉, iterable에서 선택된 요소를 반환하는 iterator 생성.
- islice() 함수는 새로운 시퀀스를 생성하는 게 아니라, iterator를 반환하므로 메모리를 더 효율적으로 사용가능.
- 일반적으로 대용량 데이터를 다룰 때 사용되며, 새로운 시퀀스를 생성하지 않고 필요한 부분만 추출해서 처리 가능
itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step])
islice() 사용예시
from itertools import islice
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
my_slice = islice(my_list, 3)
print(list(my_slice))
+추가
iterable 이란?
An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() method or with a __getitem__() method that implements sequence semantics.
즉. 한번에 하나씩 반환가능한 객체이며 예시는 다음과 같다.
- sequence 타입 : list, string, tuple
- non-sequence 타입 : dictionary, file objects, objects
iterator이란?
An object representing a stream of data. Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again. Iterators are required to have an __iter__() method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to the iter() function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
즉, 데이터 흐름을 보여주는 객체
Reference
Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...
docs.python.org
Glossary
>>>, The default Python prompt of the interactive shell. Often seen for code examples which can be executed interactively in the interpreter.,,..., Can refer to:- The default Python prompt of the i...
docs.python.org
itertools — Functions creating iterators for efficient looping
This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set...
docs.python.org
'Programming > Python' 카테고리의 다른 글
[Python] pandas.DataFrame.select_dtypes 함수 (0) | 2023.06.13 |
---|---|
[Python] pandas.DataFrame.sample() 함수 (0) | 2023.06.09 |
[Python] split() 함수 vs. join() 함수 (0) | 2023.06.01 |
[Python] sys.stdout.write()와 print() 차이 (0) | 2023.05.23 |
[Python] sys.stdin.readline() (0) | 2023.05.17 |