# 03. 로그파일 재정렬(leetcode 937)

{% hint style="info" %}
[**파이썬 리스트 조건에 맞게 정렬하기 = filter**](https://coding-groot.tistory.com/21)

[**Filter 공식페이지 docs**](https://docs.python.org/3/library/functions.html#filter)

[**lambda 공식페이지**](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions)

[**정규표현식**](https://wikidocs.net/4309)
{% endhint %}

### **풀이**

**`logs` 리스트에는 digit 로그와 letter 로그 두 종류가 있다. 따라서 먼저 이 두 종류를 구분하자.**

**리스트에서 조건에 맞는 요소를 가져와서 새로운 리스트를 만드는 방법은 filter** 메서드를 사용하면 하자.

**`filter(function, iterable)` 처럼 사용하면 된다. 첫 번째 인자로 들어가는 function 에 일반적인 함수를 콜백함수처럼 넣을 수도 있지만, lambda 를 이용해서 함수를 즉시 만들어서 넣을 수도 있다.**

**이후에는 letters 를 정렬해야한다. 정렬도 filter 와 마찬가지로 lambda 를 사용하면 깔끔하게 코드르 짤 수 있다.**

**아래는 최종 코드**

```
class Solution:
    def reorderLogFiles(self, logs: List[str]) -> List[str]:
        numbers = re.compile('[0-9]')
        alphabets = re.compile('[A-Za-z]')
        digits = list(filter(lambda x: numbers.search(x.split()[1]), logs))
        letters = list(filter(lambda x: alphabets.search(x.split()[1]), logs))
        letters.sort(key=lambda x: (x.split()[1:], x.split()[0]))
        return letters + digits
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://simian114.gitbook.io/blog/undefined/undefined-1/03.-leetcode-937.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
