반응형

File 읽어서 객체에 담기

# test 객체


class test:
	name: str
    age: int

def test():
    file_path = "test.txt"
    # 파일 읽기
    with open(file_path, 'r', encoding='UTF8') as file:
        file_content = file.read()

    # JSON 파싱
    data = json.loads(file_content)
    x = data["data"]

    # Syncer 인스턴스 생성
    test_data = test(**x)
반응형

'Python' 카테고리의 다른 글

파이썬 lambda 사용시  (0) 2023.06.01
print 하면 <map object at 0x10446d880> 나올 때  (0) 2023.06.01
[python] list 사용 방법  (0) 2023.05.29
zip()  (0) 2023.05.03
combinations()  (0) 2023.05.03
반응형

파이썬 lambda 사용시

파이썬 lambda 사용할 때 격은 일에 대한 내용입니다.

 

 

 

1. map(lambda x: x> 1, nums)

nums 안에 있는 숫자 값을 조건에 따라 걸러 낼려고 했는데 map()을 사용하면 True, False로 return 하게 됩니다.

숫자로 받을려면 filter() 함수를 사용하면됩니다.

반응형

'Python' 카테고리의 다른 글

File 읽어서 객체에 담기  (0) 2024.09.04
print 하면 <map object at 0x10446d880> 나올 때  (0) 2023.06.01
[python] list 사용 방법  (0) 2023.05.29
zip()  (0) 2023.05.03
combinations()  (0) 2023.05.03
반응형
반응형

문제

vscode 환경에서 파이썬을 가지고 map에 있는 값을 읽을려고 print() 실행 했을 때 <map object at 0x10446d880>이라고 나왔다.

읽을 수 있게 바꿔야 합니다.

 

코드

def twoSum(nums, target):
	# arr map
    arr = map(lambda x: x < target, nums)

nums = [2,7,11,15]
target = 9
twoSum(nums, target)

 

해결

1. for문

for num in arr:
	print(num)

 

2. map을 list로 담기

arr = list(map(lambda x: x < target, nums))
print(arr)

 

 

반응형

'Python' 카테고리의 다른 글

File 읽어서 객체에 담기  (0) 2024.09.04
파이썬 lambda 사용시  (0) 2023.06.01
[python] list 사용 방법  (0) 2023.05.29
zip()  (0) 2023.05.03
combinations()  (0) 2023.05.03
반응형
반응형

list 사용 방법

  • 리스트는 [] 기호를 사용하여 표현
  • L = [i for i in L if i % 3 == 0] 도 가능
  • del(L[2]) - 리스트 특정 인덱스 내용을 삭제
  • del(L) - 리스트 자체를 삭제
  • t = (1, 2, 3, 4, 5)
  • L = list(t) - 튜플(tuple), 집합(set)과 같은 다른 자료형을 리스트로 바꾸기 위해서는 list()를 사용
  • append(값) - 리스트에 값을 하나 추가
  • insert(인덱스, 값) - 인덱스 위치에 값을 하나 추가
  • extend(iterable) - iterable 인자를 넘겨 리스트에 추가
  • copy() - 리스트 복사 (리턴받아 사용)
  • remove(값) - 전달한 값을 삭제 (중복된 경우 처음 나오는 값을 삭제)
  • pop(인덱스) - 인덱스에 위치한 값을 리턴하면서 삭제 (인자가 없으면 맨 뒤 값을 pop)
  • clear() - 리스트 값 모두 삭제 (빈 리스트 생성)
  • count(값) - 인자로 전달한 값의 개수를 확인
  • len() - 리스트의 길이를 확인하기 위해서는  함수를 사용
  • index(인덱스) - 인덱스에 위치한 값을 확인
  • reverse() - 리스트에 들어있는 값을 역순(거꾸로)으로 변경
  • sort() - 리스트 내용을 정렬 (오름차순)(reverse=True를 인자로 전달하면 내림차순 정렬이 가능)
반응형

'Python' 카테고리의 다른 글

파이썬 lambda 사용시  (0) 2023.06.01
print 하면 <map object at 0x10446d880> 나올 때  (0) 2023.06.01
zip()  (0) 2023.05.03
combinations()  (0) 2023.05.03
[panda]info()  (0) 2023.04.26
반응형
반응형

zip(*iterables, strict=False)

  • 동일한 개수로 이루어진 iterable한 객체들(iterables)을 인수로 받아 묶어서 iterator로 반환합니다.

예시 코드)

for itemin zip([1, 2, 3], ['sugar', 'spice', 'everything nice']):
	print(item)
    
(1, 'sugar')
(2, 'spice')
(3, 'everything nice')
반응형

'Python' 카테고리의 다른 글

print 하면 <map object at 0x10446d880> 나올 때  (0) 2023.06.01
[python] list 사용 방법  (0) 2023.05.29
combinations()  (0) 2023.05.03
[panda]info()  (0) 2023.04.26
cannot import name 'fl_score' from 'sklearn.metrics'  (0) 2023.04.26
반응형
반응형

 

itertools.combinations(iterable, r)

  • 파이썬의 combinations은 itertools 라이브러리를 호출해서 사용할 수 있습니다.
  • iterable은 member를 하나씩 반환할 수 있는 object를 말하며, 예로는 sequence type인 list, str, tuple이 있습니다.

 

예시 코드)

combinations('ABCD', 2) --> AB AC AD BC BD CD
combinations(range(4), 3) --> 012 013 023 123
반응형

'Python' 카테고리의 다른 글

[python] list 사용 방법  (0) 2023.05.29
zip()  (0) 2023.05.03
[panda]info()  (0) 2023.04.26
cannot import name 'fl_score' from 'sklearn.metrics'  (0) 2023.04.26
IndentationError: unindent does not match any outer indentation level  (0) 2023.04.25
반응형
반응형

panda.info()를 사용하면 데이터 프레임에 있는 데이터를 확인할 수 있습니다

 

반응형
반응형
반응형

문제

sklearn에서 f1._score import 할 때 에러가 발생했습니다.

 

 

해결

1과 l이 jupyter notebook에서는 같은 모양이라 잘못 입력했던거였습니다.

숫자 1로 바꾸니 제대로 import 되었습니다.

 

 

* conda install -c anaconda scikit-learn 으로 설치 후 conda update scikit-learn 으로 업데이트 하면됩니다.

반응형

'Python' 카테고리의 다른 글

zip()  (0) 2023.05.03
combinations()  (0) 2023.05.03
[panda]info()  (0) 2023.04.26
IndentationError: unindent does not match any outer indentation level  (0) 2023.04.25
ValueError: min() / max() arg is an empty sequence  (0) 2023.04.19

+ Recent posts