1번 리스트 - extend() - 2번 리스트 -> 1번 리스트가 확장되는 개념
group1 = ['홍길동', '박찬호', '이용규']
group2 = ['강호동', '박승철', '김지은']
print('group1 : {}'.format(group1))
print('group2 : {}'.format(group2))
group1.extend(group2)
print('group1 : {}'.format(group1)) #확장
print('group2 : {}'.format(group2)) #그대로
-->
group1 : ['홍길동', '박찬호', '이용규']
group2 : ['강호동', '박승철', '김지은']
group1 : ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
group2 : ['강호동', '박승철', '김지은']
a + b -> c가 생김
group1 = ['홍길동', '박찬호', '이용규']
group2 = ['강호동', '박승철', '김지은']
print('group1 : {}'.format(group1))
print('group2 : {}'.format(group2))
result = group1 + group2
print('result : {}'.format(result))
print('group1 : {}'.format(group1))
print('group2 : {}'.format(group2))
-->
group1 : ['홍길동', '박찬호', '이용규']
group2 : ['강호동', '박승철', '김지은']
result : ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
group1 : ['홍길동', '박찬호', '이용규']
group2 : ['강호동', '박승철', '김지은']
myFavoriteNumber = [1, 3, 5, 6, 7]
friendFavoriteNumber = [2, 3, 5, 8, 10]
print('myFavoriteNumber: {}'.format(myFavoriteNumber))
print('friendFavoriteNumber: {}'.format(friendFavoriteNumber))
addList = myFavoriteNumber + friendFavoriteNumber
#extend 는 뒤에 붙이는 것, +는 새로운 리스트를 만드는 것
print('addList: {}'.format(addList)) #겹침 o
result = [] #새로운 리스트 선언
for number in addList:
if number not in result:
result.append(number) #중복없이 더하고자
print('result: {}'.format(result)) #겹침 x
-->
myFavoriteNumber: [1, 3, 5, 6, 7]
friendFavoriteNumber: [2, 3, 5, 8, 10]
addList: [1, 3, 5, 6, 7, 2, 3, 5, 8, 10]
result: [1, 3, 5, 6, 7, 2, 8, 10]
- 문자
students = ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
print('students: {}'.format(students))
students.sort()
print('students: {}'.format(students))
students.sort(reverse=True) #내림차순
print('students: {}'.format(students))
-->
students: ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
students: ['강호동', '김지은', '박승철', '박찬호', '이용규', '홍길동']
students: ['홍길동', '이용규', '박찬호', '박승철', '김지은', '강호동']
----------------------------
- 숫자
numbers = [2, 50, 0.12, 1, 9, 7, 17, 35, 100, 3.14]
print('numbers: {}'.format(numbers))
numbers.sort()
print('numbers: {}'.format(numbers))
numbers.sort(reverse=True)
print('numbers: {}'.format(numbers))
-->
numbers: [2, 50, 0.12, 1, 9, 7, 17, 35, 100, 3.14]
numbers: [0.12, 1, 2, 3.14, 7, 9, 17, 35, 50, 100]
numbers: [100, 50, 35, 17, 9, 7, 3.14, 2, 1, 0.12]
playerScore = [9.5, 8.9, 9.2, 9.8, 8.8, 9.0]
print('playerScore: {}'.format(playerScore))
playerScore.sort()
print('playerScore: {}'.format(playerScore))
playerScore.pop(0)
playerScore.pop(len(playerScore) - 1) #인덱스 값
print('playerScore: {}'.format(playerScore))
sum = 0
avg = 0
for score in playerScore:
sum += score
avg = sum / len(playerScore)
print('sum: %.2f' % sum)
print('avg: %.2f' % avg)
-->
playerScore: [9.5, 8.9, 9.2, 9.8, 8.8, 9.0]
playerScore: [8.8, 8.9, 9.0, 9.2, 9.5, 9.8]
playerScore: [8.9, 9.0, 9.2, 9.5]
sum: 36.60
avg: 9.15
secret = '27156231'
secretList = []
solvedList = '' #빈 문자로 처리
#문자를 숫자로 바꿔서 넣어야 함
for cha in secret:
secretList.append(int(cha))
print('secretList: {}'.format(secretList))
secretList.reverse()
print('secretList: {}'.format(secretList))
val = secretList[0] * secretList[1]
secretList.insert(2, val) #2자리에, value값으로 들어가라
val = secretList[3] * secretList[4]
secretList.insert(5, val) #5자리에, value값으로 들어가라
val = secretList[6] * secretList[7]
secretList.insert(8, val)
val = secretList[9] * secretList[10]
secretList.insert(11, val)
print('secretList: {}'.format(secretList)) #암호가 해독됨
-->
secretList: [2, 7, 1, 5, 6, 2, 3, 1]
secretList: [1, 3, 2, 6, 5, 1, 7, 2]
secretList: [1, 3, 3, 2, 6, 12, 5, 1, 5, 7, 2, 14]
[2:4] == (2 <= n < 4)
students = ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
print('students: {}'.format(students))
print('students: {}'.format(students[2:4]))#2부터 4 앞 까지 = 2, 3
print('students: {}'.format(students[:4]))#처음부터 4 앞 까지
print('students: {}'.format(students[2:]))#2부터 끝까지
print('students: {}'.format(students[2:-2]))#2부터 -2(뒤에서부터계산) 앞 까지
print('students: {}'.format(students[-5:-2]))
-->
students: ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
students: ['이용규', '강호동']
students: ['홍길동', '박찬호', '이용규', '강호동']
students: ['이용규', '강호동', '박승철', '김지은']
students: ['이용규', '강호동']
students: ['박찬호', '이용규', '강호동']
리스트를 슬라이싱 한 결과도 리스트
문자열을 슬라이싱하면 결과도 문자열
str = 'abcdefghijklmnopqrstuvwxyz'
print('str length: {}'.format(len(str)))
print('str: {}'.format(str[2:4]))
print('str: {}'.format(str[:4]))
print('str: {}'.format(str[2:]))
-->
str length: 26
str: cd
str: abcd
str: cdefghijklmnopqrstuvwxyz
[2:-2, 2] : 2부터 시작해서 -2앞 까지, 두칸씩 슬라이싱
[::2] : 처음부터 끝까지, 2칸씩 기준으로 슬라이싱
변수에 새로운 값을 "할당"해서 변경
print('students: {}'.format(students))
students[1:4] = ['park chanho', 'lee yonggyu', 'gang hodong']
print('students: {}'.format(students))
-->
students: ['홍길동', '박찬호', '이용규', '강호동', '박승철', '김지은']
students: ['홍길동', 'park chanho', 'lee yonggyu', 'gang hodong', '박승철', '김지은']
print('students: {}'.format(students[slice(2, 4)]))
= [2:4]
print('students:
{}'.format(students[slice(4)]))
= [:4]
print('students: {}'.format(students[slice(2, len(students))]))
= [2:6]
-->
students: ['이용규', '강호동']
students: ['홍길동', '박찬호', '이용규', '강호동']
students: ['이용규', '강호동', '박승철', '김지은']