1. 빈 리스트를 만들고 리스트에 값 추가
myList=[] myList.append(100) myList.append(200) myList.append(300) print(myList)
< 결과 >
[100, 200, 300]
2. 빈 리스트를 만들고 1부터 100까지 추가
myList=[] for i in range(100): myList.append(i+1) print(myList)
< 결과 >
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
3. 숫자를 4개 입력받아 리스트에 추가한 후 4번째, 3번째 숫자를 차례대로 출력
myList=[] myList.append(int(input("1st number: "))) myList.append(int(input("2nd number: "))) myList.append(int(input("3th number: "))) myList.append(int(input("4th number: "))) print("4th number is %d" % myList[-1]) print("3th number is %d" % myList[-2])
< 결과 >
1st number: 3 2nd number: 5 3th number: 7 4th number: 9 4th number is 9 3th number is 7
4. 두 리스트 더하기
aList=[10,30,50] bList=[20,40,60] print(aList+bList)
< 결과 >
[10, 30, 50, 20, 40, 60]
5. 4번의 합친 리스트에서 가장 마지막 숫자를 하나씩 꺼내기
aList=[10,30,50] bList=[20,40,60] cList=aList+bList print(cList) for i in range(len(cList)): print("%s from cList" % cList.pop())
< 결과 >
[10, 30, 50, 20, 40, 60] 60 from cList 40 from cList 20 from cList 50 from cList 30 from cList 10 from cList
6. 2개의 리스트 더하고, 리스트 내 값을 역순으로 만들기
myList=[100,200,300] print(myList) myList.extend([400,300,200]) print(myList) myList.reverse() print(myList)
< 결과 >
[100, 200, 300] [100, 200, 300, 400, 300, 200] [200, 300, 400, 300, 200, 100]
7. 리스트 중간에 값 넣고 값 위치 알아보기
myList=[1,5,10] print(myList) myList.insert(2,3) print(myList) for i in myList: print(myList.index(i))
< 결과 >
[1, 5, 10] [1, 5, 3, 10] 0 1 2 3
8. 리스트 내 값 제거하기
myList=[1,2,3,4,5,6,7,8,9,10] for i in myList: if i % 2 == 0: myList.remove(i) print(myList)
< 결과 >
[1, 3, 5, 7, 9]
9. 중첩 리스트 처리하기
myList1=[100,200,300,400,500] print(myList1) myList2=[i for i in myList1] print(myList2) myList3=[j*j for j in myList2] print(myList3)
< 결과 >
[100, 200, 300, 400, 500] [100, 200, 300, 400, 500] [10000, 40000, 90000, 160000, 250000]
10. 2차원 리스트 생성하고 처리하기
myList=[[0 for i in range(5)] for j in range(5)] print(myList) for x in range(5): for y in range(5): myList[x][y]=x*y print(myList)
< 결과 >
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8], [0, 3, 6, 9, 12], [0, 4, 8, 12, 16]]
11. 리스트 값 소트하기
mList=['원빈','장동건','이종석','서태지'] fList=['수영','소진','수지','안영미'] lastNameList=[] for str in mList: lastNameList.append(str[0]) for str in fList: lastNameList.append(str[0]) lastNameList.sort() print(lastNameList)
< 결과 >
['서', '소', '수', '수', '안', '원', '이', '장']
12. 2차원 배열 처리하기
myList=['수영','소진','수지','안영미'] encodeList=[[],[]] for str in myList: encodeList[0].append(str.encode('utf-8')) print(encodeList) for str in encodeList[0]: encodeList[1].append(str.decode('utf-8')) print(encodeList)
< 결과 >
[[b'\xec\x88\x98\xec\x98\x81', b'\xec\x86\x8c\xec\xa7\x84', b'\xec\x88\x98\xec\xa7\x80', b'\xec\x95\x88\xec\x98\x81\xeb\xaf\xb8'], []] [[b'\xec\x88\x98\xec\x98\x81', b'\xec\x86\x8c\xec\xa7\x84', b'\xec\x88\x98\xec\xa7\x80', b'\xec\x95\x88\xec\x98\x81\xeb\xaf\xb8'], ['수영', '소진', ' 수지', '안영미']]
13. 리스트 더하고 곱하기
myList1=[11,22,33] myList2=[44,55,66] print(myList1+myList2) print(myList1*2) print(myList2*2)
< 결과 >
[11, 22, 33, 44, 55, 66] [11, 22, 33, 11, 22, 33] [44, 55, 66, 44, 55, 66]