코드스테이츠(컴퓨터 공학 기본)(Computer Science) Session 5 / Sprint 1(컬렉션 자료형의 다양한 활용)

컬렉션 자료형의 내장메소드

  • 컬렉션 자료형을 활용할 때 사용할 수 있는 내장메소드에 대해 알아보자.
list=[]
for i in range(1000, 2200):
    if (i%7==0) and (i%5!=0):
        list.append(str(i))

print(','.join(list))

#1001,1008,1022,1029,1036,1043,1057,1064,1071,1078,1092,1099,1106,1113,1127,1134,1141,1148,1162,1169,1176,1183,1197,1204,1211,1218,1232,1239,1246,1253,1267,1274,1281,1288,1302,1309,1316,1323,1337,1344,1351,1358,1372,1379,1386,1393,1407,1414,1421,1428,1442,1449,1456,1463,1477,1484,1491,1498,1512,1519,1526,1533,1547,1554,1561,1568,1582,1589,1596,1603,1617,1624,1631,1638,1652,1659,1666,1673,1687,1694,1701,1708,1722,1729,1736,1743,1757,1764,1771,1778,1792,1799,1806,1813,1827,1834,1841,1848,1862,1869,1876,1883,1897,1904,1911,1918,1932,1939,1946,1953,1967,1974,1981,1988,2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198
list=[]
for i in range(1000, 2200):
    if (i%7==0) and (i%5!=0):
        list.insert(len(list), str(i))

print(','.join(list))

# a.insert(len(a), x) 는 a.append(x) 와 동등합니다.
#1001,1008,1022,1029,1036,1043,1057,1064,1071,1078,1092,1099,1106,1113,1127,1134,1141,1148,1162,1169,1176,1183,1197,1204,1211,1218,1232,1239,1246,1253,1267,1274,1281,1288,1302,1309,1316,1323,1337,1344,1351,1358,1372,1379,1386,1393,1407,1414,1421,1428,1442,1449,1456,1463,1477,1484,1491,1498,1512,1519,1526,1533,1547,1554,1561,1568,1582,1589,1596,1603,1617,1624,1631,1638,1652,1659,1666,1673,1687,1694,1701,1708,1722,1729,1736,1743,1757,1764,1771,1778,1792,1799,1806,1813,1827,1834,1841,1848,1862,1869,1876,1883,1897,1904,1911,1918,1932,1939,1946,1953,1967,1974,1981,1988,2002,2009,2016,2023,2037,2044,2051,2058,2072,2079,2086,2093,2107,2114,2121,2128,2142,2149,2156,2163,2177,2184,2191,2198
words = []
while True:
    char = input()
    if char:
        words.append(char.upper())
    else:
        break;

for word in words:
    print(word)
    
# 입력하는 문자마다 출력.
values = []
for i in range(100, 300):
    char = str(i)

    if (int(char[0])%2==0) and (int(char[2])%2==0):
        values.append(char)

print(",".join(values))

#200,202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236,238,240,242,244,246,248,250,252,254,256,258,260,262,264,266,268,270,272,274,276,278,280,282,284,286,288,290,292,294,296,298
list_1 = ['bread', 'meat']
list_2 = ['Lettuce',2 ,5]
list_1.extend(list_2)
print('list1: {}, list2: {}'.format(list_1, list_2))

# list1: ['bread', 'meat', 'Lettuce', 2, 5], list2: ['Lettuce', 2, 5]
list1 = [11, 12, 43, 4, 6]
for i in list1:
    if not i % 2:
        list1.remove(i)
print(list1)

# [11, 43, 6]
my_list = [1, 2, 3, 4, 5]
my_list[0] = 99
print(my_list) # [99, 2, 3, 4, 5]

del my_list[0]
print(my_list) # [2, 3, 4, 5]
my_list = [1, 2, 3, 4, 5]
my_list[0] = 99
print(my_list) # [99, 2, 3, 4, 5]

my_list.pop()
print(my_list) # [99, 2, 3, 4]
list = ['xyz', 'XYZ' 'abc', 'ABC']
print("Index for xyz : ",  list.index( 'xyz' ))
print("Index for ABC : ",  list.index( 'ABC' ))
list = ['xyz', 'XYZ' 'abc', 'ABC']
print("Count for xyz : ",  list.count( 'xyz' )) # Index for xyz :  0
print("Count for ABC : ",  list.count( 'ABC' )) # Index for ABC :  2
import math
def bin_search(li, element):
    bottom = 0
    top = len(li)-1
    index = -1
    while top >= bottom and index == -1:
        mid = int(math.floor((top+bottom) / 2.0))
        if li[mid] == element:
            index = mid
        elif li[mid] > element:
            top = mid-1
        else:
            bottom = mid+1

    return index

li=[2,5,7,9,11,17,222]
print(bin_search(li,11)) # 4
print(bin_search(li,102)) # -1
li = [12,24,35,70,88,120]
for (i,x) in enumerate(li):
   if i not in (0,3,5):
     li = x
print(li) # 88
def list_update(list_test):
    new_li=[]
    new_set = set()
    for item in list_test:
        if item not in new_set:
            new_set.add(item)
            new_li.append(item)

    return new_li

list_test=[120,120,10,20,30,20]
print(list_update(list_test)) # [120, 10, 20, 30]
from operator import itemgetter
l = []
while True:
    s = input()
    if not s:
        break
    l.append(tuple(s.split(",")))

print(sorted(l, key=itemgetter(0,1,2))) # [("('dave'", " 'B'", ' 10)')]

# 입력 테스트 시, ('dave', 'B', 10) 처럼 아이템인덱스 채워주기
dic = {}
s=input()
for s in s:
    dic[s] = dic.get(s,0)+1
print('\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]))

# ('dave', 'B', 10)
(,1
',4
d,1
a,1
v,1
e,1
,,2
 ,2
B,1
1,1
0,1
),1

좋은 웹페이지 즐겨찾기