list_input_a = ["12", "34", "56", "321", "hello" ,"256"]
list_number=[]
for item in list_input_a:
try:
float(item)
list_number.append(item)
except:
pass
finally:
print(f"end list file : {list_input_a[list_input_a.index(item):]}")
print(f"{list_input_a} 내부에 있는 숫자는 {list_number}입니다.")
This code snippet filters out numeric strings from a list and prints the remaining elements of the list after each iteration. Here's a breakdown of its functionality:
### Code Breakdown
1. **Initial Setup**:
- `list_input_a` is defined with a mix of numeric strings (`"12"`, `"34"`, etc.) and a non-numeric string (`"hello"`).
- `list_number` is initialized as an empty list to hold numeric strings found in `list_input_a`.
2. **Loop and Try-Except Block**:
- The code iterates over each item in `list_input_a`.
- Within the loop, a `try` block attempts to convert the current `item` to a float. This conversion will succeed for numeric strings and fail for non-numeric strings (like `"hello"`).
- If the conversion succeeds (meaning `item` is a numeric string), the item is appended to `list_number`.
- If the conversion fails, the `except` block catches the exception, and the `pass` statement means the code does nothing and moves on to the next iteration.
- The `finally` block executes after each iteration, regardless of whether the try block succeeded or failed. It prints the remaining elements of `list_input_a` starting from the current item to the end of the list. The use of `list_input_a.index(item)` finds the first occurrence of `item` in the list, which might not work as expected if the list contains duplicates.
3. **Final Print Statement**:
- After the loop completes, the code prints the original list `list_input_a` and the filtered `list_number` containing only numeric strings.
### Key Observations
- The `finally` block's usage here is somewhat unconventional, as it's used to print the remaining elements in the list during each iteration. Typically, `finally` is used for cleanup actions that must be executed under all circumstances.
- The `list_input_a.index(item)` approach in the `finally` block assumes each item is unique because `index()` returns the first index of the item found, which might not accurately reflect the loop's progress if there are duplicate items in the list.
### Output Explanation
The output consists of multiple lines showing the state of `list_input_a` from the current item to the end of the list for each iteration, followed by a summary statement showing the original list and the numeric strings identified within it.
Example Final Output (the exact wording in Korean translates to "The numbers inside list_input_a are list_number."):
```
[...] # Repeated lines showing the end list file from each iteration
["12", "34", "56", "321", "hello", "256"] 내부에 있는 숫자는 ["12", "34", "56", "321", "256"]입니다.
```
This output indicates that all elements except `"hello"` were recognized as numeric strings and collected in `list_number`.
'개념 > 혼자 공부하는 파이썬' 카테고리의 다른 글
python) custom exception class (2) | 2024.11.01 |
---|---|
python) Class Definitions and Inheritance (1) | 2024.10.31 |
python) Overriding (1) | 2024.10.29 |
python) size comparison function (2) | 2024.10.28 |
python) Declare a function inside a class (0) | 2024.10.27 |