def plus_on(arr):
return arr+1
a=[1,2,3]
#정의한 함수 사용
print(list(map(plus_on,a)))
출력 : [2,3,4]
#함수 없이 람다 사용
print(list(map(lambda x: x + 2, a)))
출력 : [3,4,5]
#간단한 람다 표현식
plus_two = lamnda x: x+2
print(plus_two(1))
출력 : 3
#1x3 행렬 만들기
a=[0] * 3
print(a)
#출력 : [0,0,0]
#3x3 행렬 만들기
b = [[0] * 3 for _ in range(3)]]
print(b)
#출력 : [[0,0,0],[0,0,0],[0,0,0]]
#3x3 행렬 특정 위치 값 수정 하기
b[1][0] = 2
print(b)
#출력 : [[0,0,0],[2,0,0],[0,0,0]]
#3x3 행렬 출력하기
for x in b:
print(x)
#출력
[0,0,0]
[2,0,0]
[0,0,0]
for x in b:
for value in x:
print(value, end=' ')
print()
#출력
0 0 0
2 0 0
0 0 0
a = list(range(0,10)) # list생성 [0,1,2,3,4,5,6,7,8,9]
print(a[:3]) # list 0 ~ 2 인덱스 출력
print(a[1:3]) # list 1 ~ 2 인덱스 출력
2. 반복문 활용 방법
a = list(range(0,10)) # list생성 [0,1,2,3,4,5,6,7,8,9]
for i in range(len(a)):# i는 0~9까지 순환
print(a[i])
for i in a: # i에게 값 타입 반환 value
print(i)
for i in enumerate(a): # i에게 튜플 타입 반환 (index, value)
print(i)
for index, value in enumerate(a): # index에게 a의 index 반환, value에게 a의 value값 반환
print(index, value)
isOver = all(11>x for x in a) # list a의 값이 모두 11을 넘기지 못하므로 True 반환
isUnder = all(11<x for x in a) # list a의 값이 모두 11을 넘기지 못하므로 False 반환
a = list() # list 생성
b = [] # list 생성
c = [1,2,3] # 1~10까지의 list 생성
d = list(range(1,11)) # 1~10까지의 list 생성
2. 인덱스 접근
a = [1,2] #list 1~2 생성
print(a[1]) # 2 출력
3. list 끼리 더하기
a = [1,3] #list 1,3 생성
b = [3,5] #list 3,5 생성
c = a + b #list 1,3,3,5 생성
4. list 값 추가 및 제거
a = [] #list 생성
a.append(6) # list에 추가 6 저장, list 상태 : 6
a.append(7) # list에 추가 7 저장, list 상태 : 6, 7
a.append(8) # list에 추가 8 저장, list 상태 : 6, 7, 8
a.append(9) # list에 추가 9 저장, list 상태 : 6, 7, 8, 9
a.insert(1,2) # list 1번 인덱스에 2추가 저장, list 상태 : 6, 2, 7, 8, 9
a.pop() # list 가장 마지막 인덱스 삭제, list 상태 : 6, 2, 7, 8
a.pop(2) # list 가장 2번째 인덱스 삭제, list 상태 : 6, 2, 8
a.remove(6) # list에서 데이터 6 삭제, list 상태 : 2, 8
locateValue = a.index(8) # list에서 데이터가 8인 인덱스 출력, locateValue에 1 저장
a.clear() #모든 데이터 삭제
5. list 값 통계
a = list(range(1,11)) #list 생성, 1~10 저장
sumvalue = sum(a) #list a에 저장되어있는 1~10까지의 합 저장
maxvalue = max(a) #list a에 저장되어있는 1~10까지의 최대값 저장
minvalue = min(a) #list a에 저장되어있는 1~10까지의 최소값 저장
6. list 셔플, 정렬
import random as r #랜덤함수 필요
a = list(range(1,11)) # list 생성, 1~10 저장
r.shuffle(a) # list a 셔플
a.sort() # list a 오름차순 정렬
a.sort(reverse=True) # list a 내림차순 정렬
"""
반복문을 이용한 문제풀이
1) 1부터 N까지 홀수출력하기
2) 1부터 N까지 합 구하기
3) N의 약수출력하기
"""
n = int(input("-->"))
listofn = []
sum = 0
//1번, 2번문제 동시에 수행
for i in range(1,n+1):
sum += i
if i%2!=0:
print(i)
print("합계 -> " + str(sum))
decreaseN = n
devider = 2
//3번 문제 수행
//감소하는 decreaseN이 1이 아닐때까지 수행 또는 devider가 입력 변수 n의 1/2값 보다 크면 종료
//devider를 1씩 증가하면서 입력 변수 n과 나누고 몪이 0이면 소인수 추가
//devider를 1씩 증가하면서 입력 변수 n과 나누고 몪이 0이 아니면 devider 1 증가
while decreaseN!=1:
if devider > n/2:
break
if decreaseN % devider == 0:
decreaseN /= devider
listofn.append(devider)
else:
devider+=1
print(listofn)
//1. 0~9까지 데이터를 포함한 list 생성
//2. i에 0값 대입 후 for문 print 수행
//3. 수행 후 i에 2 증가
//4. i가 9를 초과하면 else문 수행
for i in range(0,10,2):
print(i)
else:
print("모든 데이터 출력")
출력
0
2
4
6
8
모든 데이터 출력
코드
//1. 0~9까지 데이터를 포함한 list 생성
//2. i에 0값 대입 후 for문 print 수행
//3. 수행 후 i에 2 증가
//4. i가 9를 초과하거나 i가 3보다 크면 for문 탈출
//5. break로 수행되었기 때문에 else문 무시
for i in range(0,10,2):
print(i)
if i >= 3:
break
else:
print("모든 데이터 출력")
출력
0
2
4
간단한 for문 및 for문을 모두 완료시 수행하는 else문을 설명했다.
지금까지 했던 다른언어 C++, C#, Java에서도 이런 코드가 있던가?
아무튼 for~else문은 현재 for문을 모두 돌았는지 확인하는? 그런 용도로 쓰일것 같다.
//1. 사용자 입력을 받는다.
//2. 문자열 입력 데이터를 ',' 기준으로 자른후 문자열 리스트를 반환한다.
//3. 문자열 리스트를 float으로 모두 변환 시킨다.
//4. float으로 변환된 float 리스트를 a, b에 저장한다.
//5. 저장된 a, b를 출력한다.
a, b = map(float, input("-->").split(','))
print(a, b)
입력
--> 1,2
출력
1 2
알고리즘 문제 풀다보면 사용자 입력을 받아야 하는 문제가 존재한다.
이전 C++로 풀었다면 생각보다 귀찮은 코드가 추가되어야 하지만 python은 이처럼 간단하게 처리가 가능하다.
using OpenCvSharp;
using System.Windows;
using System.Windows.Controls;
using WpfApp1.utility;
namespace WpfApp1.usercontrol
{
/// <summary>
/// ImageViewer.xaml에 대한 상호 작용 논리
/// </summary>
public partial class ImageViewer : UserControl
{
public ImageViewer()
{
InitializeComponent();
}
public Mat MatImage
{
get { return (Mat)GetValue(MyProperty); }
set { SetValue(MyProperty, value); }
}
public static readonly DependencyProperty MyProperty =
DependencyProperty.Register(
"MatImage",
typeof(Mat),
typeof(ImageViewer),
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ImageViewer viewer = d as ImageViewer;
viewer.image.Source = ImageHandler.IplImageToInteropBitmap((e.NewValue as Mat));
}
}
}
주요코드
1. MatImage의 GetValue,SetValue
2. MyProperty의 등록 과정
DependecyProperty.Register(
"MatImage", // 실제 xaml에 노출 시킬 변수 명 추 후 Binding 시킨다.
typeof(Mat), // 위의 MatImage의 타입을 정의한다.
typeof(ImageViewer), // MatImage 변수를 가지고 있는 객체 타입을 정의한다.
new FrameworkPropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));
//OnMyPropertyChanged 함수로 콜백을 등록한다.
3. OnMyPropertyChanged 함수
DependecyObject - MatImage 인스턴스를 가지고 있는 오너 인스턴스 타입으로 사용.
DependencyPropertyChangedEventArgs - 외부 Binding을 통해 전달된 Mat Type의 인스턴스.
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
ImageViewer viewer = d as ImageViewer; // 오너 인스턴스로 변환
//e.NewValue as Mat으로 외부에서 바인딩한 데이터 변환
viewer.image.Source = ImageHandler.IplImageToInteropBitmap((e.NewValue as Mat));
}
ViewModel 폴더
MainViewModel.cs
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using OpenCvSharp;
using WpfApp1.utility;
namespace WpfApp1.ViewModel
{
/// <summary>
/// This class contains properties that the main View can data bind to.
/// <para>
/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
/// </para>
/// <para>
/// You can also use Blend to data bind with the tool's support.
/// </para>
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
public class MainViewModel : ViewModelBase
{
Mat _matImage;
public Mat matImage
{
get
{
return _matImage;
}
set
{
_matImage = value;
RaisePropertyChanged(() => matImage);
}
}
public RelayCommand OpenImage { get; set; }
public MainViewModel()
{
OpenImage = new RelayCommand(OpenImageAction);
}
void OpenImageAction()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.RestoreDirectory = true;
openFileDialog.DefaultExt = "jpg";
openFileDialog.Filter = "Images Files(*.jpg; *.bmp; *.png)|*.jpg;*.bmp;*.png";
if (openFileDialog.ShowDialog() == true)
{
//Get the path of specified file
string strImagePath = openFileDialog.FileName;
matImage = ImageHandler.OpenImage(strImagePath);
}
}
}
}
주요 코드
1. matImage 프로퍼티를 이용한 바인딩 코드
Mat _matImage;
public Mat matImage
{
get
{
return _matImage;
}
set
{
_matImage = value;
RaisePropertyChanged(() => matImage);
}
}