Programming Language/Python2022. 3. 10. 10:28
728x90

map(function, iterable, ...)

map은 새로운 리스트를 반환한다.

 

# 1. range를 리스트로 반환한다.
a = list(range(10))
print(a)
# 결과
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


# 2. range를 문자 리스트로 반환한다.
a = list(map(str,range(10)))
print(a)
# 결과
# ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# 3. float 리스트를 int 리스트로 변환한다.
b = [1.1, 2.6, 3.3]
a = list(map(int, b))
print(a)
# [1, 2, 3]

# 4. 숫자에 달러 기호를 붙여, 문자 리스트를 반환한다.
def converDollar(money):
  return '$' + str(money)

a = list(map(converDollar,range(10)))
print(a)
# ['$0', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9']

# 5. 문자리스트 생성후 구분자 ";"로 Join 처리한다.
a = ";".join(map(str,range(10)))
print(a)
# 결과
# 0;1;2;3;4;5;6;7;8;9

# 6. 리스트를 변수에 할당
a, b = [10, 20]
print(a)
print(b)
# 결과
# 10
# 20

 

728x90
Posted by 하루y
728x90

 

SettingWithCopyWarning 경고가 계속 발생할 때 해결방안

 

1) Dataframe의 slice를 복사 후 사용한다.

  # df = df[(df.A > 10) & (df.B < 5)] 아래 구분으로 변경한다.
  df = df[(df.A > 10) & (df.B < 5)].copy()
  df['1/PER'] = 1 / df['PER']

2) 경고를 끈다

import pandas as pd

# 경고를 끈다 (기본값: 'warn')
pd.set_option('mode.chained_assignment',  None)

# 중략...
df = df[(df.A > 10) & (df.B < 5)]
df['1/PER'] = 1 / df['PER']

 

end.

728x90
Posted by 하루y
728x90

SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)'))

 

인증서 오류 시 --trusted-host 옵션을 사용한다.

 

$ pip install requests bs4 lxml openpyxl numpy
There was a problem confirming the ssl certificate

$ pip --trusted-host pypi.python.org --trusted-host files.pythonhosted.org --trusted-host pypi.org install requests bs4 lxml openpyxl numpy

 

 

Usage:   
  pip <command> [options]

Commands:
  install               Install packages.
  download          Download packages.
  uninstall            Uninstall packages.
  freeze               Output installed packages in requirements format.
  list                   List installed packages.
  show                Show information about installed packages.
  check               Verify installed packages have compatible dependencies.
  config              Manage local and global configuration.
  search              Search PyPI for packages.
  cache               Inspect and manage pip's wheel cache.
  index               Inspect information available from package indexes.
  wheel              Build wheels from your requirements.
  hash               Compute hashes of package archives.
  completion      A helper command used for command completion.
  debug            Show information useful for debugging.
  help               Show help for commands.

 

General Options:
  -h, --help        Show help.
  --isolated        Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose    Give more output. Option is additive, and can be used up to 3 times.     
  -V, --version    Show version and exit.

  --trusted-host <hostname>

   Mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS.

  ... 중략 ...

 

end.

728x90
Posted by 하루y
Programming Language/Python2022. 2. 20. 00:02
728x90

파이썬 코딩 스타일 가이드

https://www.python.org/dev/peps/pep-0008/#code-lay-out


1. 코드 레이아웃
 1) 들여 쓰기: 공백 4개
 - Python은 들여쓰기를 위해 탭과 공백을 혼합하는 것을 허용하지 않는다.
 2) 최대 라인 길이: 79자
  - 독/주석은 72자 제한
 3) 단락 내 수식에서 이진 연산자 앞에 줄 바꿈한다.
 4) 빈 줄
  - 최상위 함수 및 클래스 정의를 두 개의 빈줄로 묶는다.
  - 클래스내 메서드 정의는 단일 빈줄로 둘러싼다.
  - 관련 기능 그룹을 구분하기 위해 여분의 빈 줄을 사용.
  ps. 파이썬은 라인 피드 문자를 공백으로 받아 들임.
 5) 소스 파일 인코딩: 배포판 코드는 항상 UTF-8 로 사용.
 6) import
  - 하나씩 각 줄로 정의함.
  - from xxx import a, b, c 처럼 from 이후는 여러개 정의 가능.
 7) 모듈 수준 Dunder Names
  - Dunder Names: __all__, __author__, __version__
  - Duner Names는 __future__ 를 제외한 모든 import 문 앞에 위치해야 함.


2. 문자열 따옴표
 1) 작은 따옴표와 큰따옴표 문자열은 동일함.


3. 표현식 및 명령문의 공백
 1) 이항 연산자를 한 공백 사용
 2) 붎필요한 공백 사용하지 않음
  - 괄호, 대괄호, 중괄호에서 파라 뒤 공백 1개, ','뒤에 파라 없으면 공백 제거.
  - 배열범위 표시시 ':'뒤는 붙임.
  - 변수 정의 할당 시 불필요한 공백 두지 않음.
# Wrong:
x             = 1
y             = 2
long_variable = 3

 

4. 코멘트
 1) Block Comment: '#' 와 공백 하나로 시작
 2) InLine Comment: 코드 뒤 공백 2개 후에, '# ' 로 시작
 3) Documentation String: """ 시작 ~ 마지막라인은 """로 끝나야 함.

 

5. 명명 규칙
 1) 피해야 할 이름
  - 단일 문자 변수 중에서 소문자 l, 대문자 O, 대문자 I 는 금지.
 2) 패키지 및 모듈 이름
  - 모두 소문자로된 짧은 이름으로
  - 가독성을 위해서, 모듈 이름에 밑줄(_) 사용.
 3) 클래스 이름
  - Class와 Exceptoin에는 CapWords(CamelCase)표기법을 사용하고,
    함수, 변수 명은 소문자와 '_'를 사용한다.
    예외로 상수의 경우는 대문자와 '_'를 사용한다.

 

end.

728x90
Posted by 하루y