s[2] = 'a' s = s[:2] + 'a' + s[3:] c c if len(c) == 1 and c.lower() in 'aeiou' : # c contains a vowel ... if len(c) == 1 and 'a' <= c.lower() <= 'z' : # c contains an English alphabet ... s c=0 for e in s : if '0' <= e <= '9' : c += 1 for i in range(len(s)): c = s[i] print(i,c) for i,c in enumerate(s): print(i,c) for e in s[::-1] : ... for k in range(-1,-(len(s)+1),-1): e = s[k] ... c=0 s for k in range(len(s)-1) : # if '0' <= s[k] <= '9' and '0' <= s[k+1] <= '9' : c += 1 44 04 : String
t c=0 # 'aaaaaa' 'aaa' s k = s.find(t) while k >= 0 : c=0 while t in s : c += 1 k = s.find(t,k+1) c += 1 c=0 t in s ts for i in range(len(s)-len(t)+1) : True if s[i:i+len(t)] == t : c += 1 result = \"\" for n in range(2,30): for k in range(2, n) : if n % k == 0 : break else: result += str(n) + \", \" print(result[:-2]) 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 \"<title>\" \"</title>\" <title> </title> t = input().strip() a = t.find(\"<title>\") #a jb if a >= 0 : #..<title>.....</title> j = a+len(\"<title>\") b = t.find(\"</title>\",j) print( t[j:b] ) else : print( 'No title' ) m months = \"JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC\" m = int(input()) if 1 <= m <= 12 : print( months[(m-1)*3:m*3] ) else : print( 'invalid month number' ) c = input().strip().lower() # country code # USA, Thailand, Japan, China, Singapore countries = \"usthjpcnsg\" currency_codes = \"USDTHBJPYCNYSGD\" k = countries.find(c) if k >= 0: k //= 2 print( currency_codes[3*k:3*(k+1)] ) else : print( 'Not found' ) 04 : String 45
\\ s = \"String strip s = String' s = \"string' index s = \"You shouted \"Hey!\" very loudly yesterday\" index ok enter s0 'ok' len(s)-1 s = input() -1 -len(s) if s == 'ok' : index print('OK') \\ \\\\ s = input().strip() s = input().strip() # len(s) < 10 t = \"\" for i in range(10): t = s[i] + t print( s[n/2] ) # n/2 t = input().strip() # t[0],t[-1] = t[-1],t[0] t = t[-1] + t[1:-1] + t[0] s = \"c:\\temp\\data\\input.txt\" # s = \"c:\\\\temp\\\\data\\\\input.txt\" print(\"average = \" + avg) # print(\"average = \" + str(avg)) print(\"average =\", avg) 46 04 : String
Problem Code 'pypy' 'ppyyppyy' 'pythonnnaa' 'ppyytthhoonnnaa' YN dn dn d n 0n dn d 04 : String 47
Problem Code 0, 1,..., 9, A, B, C, D, E, F 0, 1,..., 9,10,11,12,13,14,15 01 04 : String int \"01101\" = 0×24 + 1×23 + 1×22 + 0×21 + 1×20 = 13 48
...102...89..3.. ...102...89..2.. 2 True False ..125.9.0. False ..125.9.2. True t = '' 2 s = s[:2]+s[3:] for e in input().strip() : if '0' <= e <= '9' : t += e print(t) 12a34bce5 t t t 12345 t t = '' for e in t b for e in input().strip() : b b if '0' <= e <= '9' : e in b break t += e b = '' for e in t : if e in b : break b += e 04 : String 49
t = '' break for e in input().strip() : break if '0' <= e <= '9' : t += e True b = '' False print(True) break for e in t : break if e in b : print(True) print(False) else for break ..125.9.0. False b += e ..125.9.2. True else: kk print(False) t = '' t for e in input().strip() : b if t[k] in t[:k] if '0' <= e <= '9' : t += e for e in t for k in range(len(t)) for k in range(len(t)) : ..125.9.0. False if t[k] in t[:k] : print(True) ..125.9.2. True break for else: for for print(False) for t = input().strip() ..125.9.0. False for k in range(len(t)) : .125.9.2. True if '0'<=t[k]<= '9' and \\ for k,e in enumerate(t): e) t[k] in t[:k] : for k print(True) break ..125.9.0. False ..125.9.2. True else: print(False) t = input().strip() for k,e in enumerate(t) : if '0' <= e <= '9' and \\ e in t[:k] : print(True) break else: print(False) 50 04 : String
dd MMM yyyy mm/dd/yyyy mm/dd/yyyy) dd MMM yyyy) 08/27/2014 27 AUG 2014 02/01/2018 01 FEB 2018 04 : String 51
HelLo WoRLd 5 PYTHON123 6 yes! 0 52 04 : String
aeiou beautiful rhythm eau i u 3 vowel 2 group 0 beautiful 3 vowel 2 group 1 rhythm 0 04 : String 53
Intania 84 Wongnai Computer Programming Programming Computer Computer Programming
05 : File readline infile = open(filename, 'r') infile = open('c:/temp/data.txt', 'r') n=0 first_line = infile.readline() s=0 for line in infile : second_line = infile.readline() sid, sc = line.strip().split() for line in infile : s += float(sc) n += 1 # for infile infile.close() print('average =', (sc/n)) # line infile = open('c:/temp/data.txt', 'r') ... n = int(infile.readline()) # s=0 infile.close() # for k in range(n) : t sid, sc = infile.readline().strip().split() s += float(sc) t = '\\n' len(t) 1 infile.close() print('average =', (sc/n)) t = '' len(t) 0 f = open('data.txt', 'r') for line in infile : t = f.readline() ... if len(t)>0 and t[-1]=='\\n' : line = infile.readline() t = t[:-1] while len(line) > 0 : for line in f : ... if line[-1] == '\\n' : line = infile.readline() line = line[:-1] ... \\n ( \\n ) \\n strip() 05 : File 55
outfile = open('c:/temp/out.txt','w') m outfile.write(any_string) 5 outfile.write(any_string + '\\n') ... m = int(input()) outfile.close() outfile = open('D:/primes.txt','w') c=0 write '\\n' result = \"\" for n in range(2,m): for k in range(2,n): if n % k == 0: break else: result += str(n) + \", \" c += 1 if c % 5 == 0: outfile.write(result[:-2] + '\\n') result = \"\" if len(result) > 0: outfile.write(result[:-2] + '\\n') outfile.close() t = input().strip() \\n f = open('names.txt', 'r') \\ for line in f : 56 if line == t : # line \\n print(t, ': found in names.txt') break else : print(t, ': not found') of = open('c:\\temp\\data.txt', 'w') \\t of = open('c:\\\\temp\\\\data.txt', 'w') // \\/ of = open('c:/temp/data.txt', 'w') infile = open('h:\\\\file.data') h:/file.data fn = input() infile = open(fn) fn blank strip infile = open( input().strip() ) 05 : File
readline readlines infile = open('c:/temp/data.txt') first_line = infile.readlines() # readlines ( second_line = infile.readline() # list of strings ) Problem Code Input Process Output line1 line3 line2 line2 line3 line1 Input blank Process Output reverse.txt Input <headline> Process <headline> </headline> </headline> Output 05 : File 57
Problem Code Input Process True Output False c:\\t\\answers.txt A, B, C D A,B,C,D c:\\t\\results.txt AABBCCCBBCCDDDABABDDCCDDCC 5630120421,25 5630120421 AABBCCCBBCCDDDABABDDCCDDCA 5631010121,14 5631010121 A BBCD BDBAAABA ABDBCCCDCC 563102121,16 563102121 ABABCCNNAADDDABAB CCDDAC 5630121821,24 5630121821 AABBCCCBBCCDDDABABDDCCDD 58 05 : File
fin = open('c:\\t\\answers.txt', 'r') readline soln = fin.readline().strip() strip for line in fin: for sid,ans = line.strip().split() split point = 0 for for k in range(len(soln)): for k if ans[k] == soln[k]: answers.txt point += 1 OSError: [Errno 22] Invalid print(sid, point) argument: 'c:\\t\\x07nswers.txt' fin.close() fin = open('c:\\\\t\\\\answers.txt','r') \\ \\\\ soln = fin.readline().strip() \\ for line in fin: 5630120421 25 4 sid,ans = line.strip().split() point = 0 ValueError: too many values to for k in range(len(soln)): unpack (expected 2) if ans[k] == soln[k]: split point += 1 print(sid, point) fin.close() 5631010121 A BBCD BDBAAABA ABDBCCCDCC split 05 : File 59
fin = open('c:\\\\t\\\\answers.txt','r') sid = line[:10] soln = fin.readline().strip() ans = line[10:] for line in fin: find strip line = line.strip() line j = line.find(' ') j if j > 0 : sid = line[:j] sid = line[:j] ans = line[j:] ans = line[j:] point = 0 for k in range(len(soln)): if ans[k] == soln[k] : point += 1 print(sid, point) fin.close() 5630120421 12 5631010121 7 563102121 6 if ans[k] == soln[k] IndexError: string index out of range fin = open('c:\\\\t\\\\answers.txt','r') print(ans) print(soln) soln = fin.readline().strip() for line in fin: AABBCCCBBCCDDDABABDDCCDDCA AABBCCCBBCCDDDABABDDCCDDCC line = line.strip() j = line.find(' ') ans if j > 0 : ans = line[j:].strip() sid = line[:j] ans = line[j:].strip() 5630120421 25 point = 0 5631010121 14 for k in range(len(soln)): if ans[k] == soln[k] if ans[k] == soln[k] : IndexError: string index out of range point += 1 print(sid, point) fin.close() 563102121 ABABCCNNAADDDABAB CCDDAC 60 05 : File
fin = open('c:\\\\t\\\\answers.txt','r') line[j:].strip() soln = fin.readline().strip() for line in fin: ans[k] k line[j:].strip() line = line.strip() line[j+1:] j = line.find(' ') if j > 0 : 5630120421 25 5631010121 14 sid = line[:j] 563102121 16 ans = line[j+1:] point = 0 for k in range(len(soln)): if ans[k] == soln[k] : point += 1 print(sid, point) fin.close() if ans[k] == soln[k] IndexError: string index out of range fin = open('c:\\\\t\\\\answers.txt','r') index out of range soln = fin.readline().strip() for line in fin: ans[k] == soln[k] ans[k] k line = line.strip() soln[k] soln[k] j = line.find(' ') if j > 0 : range(len(soln)) ans[k] sid = line[:j] ans soln ans = line[j+1:] if len(ans) < len(soln) : strip ans += ' '*(len(soln)-len(ans)) len(ans) < len(soln) point = 0 ans for k in range(len(soln)): 5630120421 25 if ans[k] == soln[k] : 5631010121 14 point += 1 563102121 16 5630121821 24 print(sid, point) fin.close() 05 : File 61
data.txt id:name:section:score 5913842721:Somsak Rakrian:1:56.6 5913845921:Somsri Deeying:2:78.0 5913856821:Rakchard Yingcheep:2:89.0 5913861321:Thumdee Tong Daidee:2:99 591387721:Somrak Rakrian:10:84.25 Not Found data.txt 56.6 88.66666666666667 1 Not Found 2 3 62 05 : File
Find Student's Grade (0-4) Not Found score.txt Not Found score.txt 12345 1111 3 9999 2 12345 2 1234 4 9999 4 Not Found 89431 0 76238 1 05 : File 63
data.txt agAbggggDf ffgFFFaaD DaADDFF FFDFFF aAbDfF g abf afb FAf FfA data.txt Afb a FfA b f data.txt F A f 64 05 : File
06.1 : List o x = [] x = list() len(x) o x = [0]*10 x = [0,0,0,0,0,0,0,0,0,0] o x = list(a) a for e in a x = list('abcde') x = ['a', 'b', 'c', 'd', 'e'] x = list(range(1,10,2)) x = [1,3,5,7,9] o a = [1,2,3]; b = [1,2,3]; c = b; d = list(b) c=b cb + list * + x = [1,2,3,3,4,5,1,2,3,3,4,5] x = 2*([1,2,3] + [3,4,5]) x.append(e) e x x.insert(i, e) e ix x.pop(i) ix x.sort() x sorted(x) x x sum(x) x max(x) x min(x) x x.count(e) ex if e in x ex x.index(e) ex if e in x : k = x.index(e) ... else : ... find split join o t.split() t o t.split(s) ts o s.join(x) xs 06.1 : List 65
x = [11,12,13,14,15] for e in x : x e for x[0] x[-len(x)] 11 x[-1] x[len(x)-1] 15 index x o 0 len(x)-1 o -1 -len(x) x[k] x[-6] x[5] x[a:b] x[a:b] a b o x[2:50000] [13,14,15], x[4999:50000] [] o x[-500:-2] [11,12,13], x[-3:-500:-1] [13,12,11], x[-500:-300] [] x x[:] x[0:] x[:len(s)] x[::] x[::1] x[::2] x[1::2] x[::-1] x[-1::-1] x[-1:-(len(x)+1):-1] [15,14,13,12,11] x[k] = e x[k:k+1] = [e] x[a:b:c] = y y x[a:b:c] |c|>1 len(y) len(x[a:b:c]) x = [1,2,3,4,5]; x[::2] = [0,0,0] x [0,2,0,4,0] x[len(x):] = [e] x.append(e) x[i:i] = [e] x.insert(i,e) x[i:i+1] = [] x.pop(i) x += [1,2] x = x + [1,2] x 12 o x += [1,2] 12 x x.append(1) x.append(2) o x = x + [1,2] x x [1,2] o x = [3]; y = x xy x = x + [9] xy o x = [3]; y = x x.append(9) x y x = y, x = y[:], x[:] = y, x[:] = y[:] xy o x=y x y xy o x = y[:] x y x = list(y) o x[:] = y x x yx y o x[:] = y[:] x[:] = y 66 06.1 : List
n n = int(input()) data = [] for k in range(n): data.append( float(input()) ) x e c=0 for e in x for d in x : for e in x[a:b:c] for i in range(len(x)) : if d == e : c += 1 x[i] = ... c = x.count(e) 06.1 : List x s=0 for d in sorted(x)[1:-1] : s += d s = sum( sorted(x)[1:-1] ) 30 x 10% for i in range(len(x)) : if x[i] < 30 : x[i] += 0.1*x[i] e for e in x : if e < 30 : e += 0.1*e for i in range(len(t)) : c = t[i] print(i,c) for i,c in enumerate(t): print(i,c) for e in x[::-1] : ... for k in range(-1,-(len(x)+1),-1) : # e = x[k] ... 67
for e in x : : if ... break ... else : ... # for e in sorted(x) : ... for e in sorted(x)[::-1] : ... s for k in range(len(s)-1) : if s[k] > s[k+1] : print(\"False\") break else: print(\"True\") c=0 for i in range(len(x)) : for j in range(i+1,len(x)) : if x[i] > x[j] : c += 1 print(c) #d for k in range(len(d)-1) : for i in range(len(d)-1) : if d[i] > d[i+1] : d[i],d[i+1] = d[i+1],d[i] x x = [2,3,5,7,11,13,17,19,23] s=0 0 len(x)-1 for i in range(len(x),0,-1): -1 -len(x) s += i*x[i] # i len(x) print( x[n/2] ) # n/2 68 06.1 : List
x x+=y x = [1,2,3,4]; x.append( [5] ) # [1,2,3,4,[5]] x.append(y) x = [1,2,3,4]; x += [5] # [1,2,3,4,5] x = [1,2,3,4]; x.append( 5 ) # [1,2,3,4,5] x = [1,2,3,4]; x += 5 # y x x = [1,2,3] #y x y=x y=x y=x # y[2] 0 y = list(x) x[2] = 0 x = list(str) str x = list('abc') x = ['abc'] join x = ['a','b','c'] join x = [1,2,3] 1,2,3 s = ','.join(x) # t t = [] for e in x : t.append( str(e) ) s = ','.join(t) # x.sort() sorted(x) x = [9,2,0,4] x.sort() x x = x.sort() # x None x.sort() e # x.sort() sorted(x) x for e in x.sort() : x x ... # x x.sort() x = sorted(x) for e in sorted(x) : sorted(x) ... # x = [1,2,3,3,2,1] for e in x : if e%2 == 1 : x.pop(x.index(e)) for i in range(len(x)) : if x[i]%2 == 1 : x.pop(i) t = [] x=t t for e in x : x if e%2 != 1 : t.append(e) x[:] = t x[:] = t xt 06.1 : List 69
Problem Code v1 v2 v1 v2 Error v1 v2 nn n n , 70 06.1 : List
Problem Code </headline> <headline> </headline> <headline> 06.1 : List 71
n nn d 0 n-1 n 2n-1 n d 1 2 1 3 3 4 6 6 13 14 06.1 : List 72
n = int(input()) n for d for d in range(1,2*n): 1 2n-1 Bn d Gn q = ['G']*n + ['B']*n 'G' 'B' for m = 2*n # solve Josephus problem m = 2*n d if len(q)==1 and q[0]='G': break for print(d) d for break else: md print('Not found') m = int(input()) 0 m-1 list(range(m)) d = int(input()) q = list(range(m)) while q k=0 while len(q) > 1 : k index 0 k += d if k >= len(q) : kd kd k k -= len(q) q.pop(k) k print(q[0]) q 7 k = 5, d = 3 m = int(input()) d = int(input()) k += d k=8 q k -= len(q) k q = list(range(m)) k=0 1 53 1 while len(q) > 1 : (5 -> 6 -> 0 -> 1) k = (k + d) % len(q) q.pop(k) k q.pop(k) print(q[0]) while q[0] 6, d 2 0 m 4, d 3 m IndexError: pop index out of range q.pop(k) k index q print(k, len(q)) 33 3 3 02 k = (k + d) % len(q) k d 4 35 (3+5) % 4 = 0 m 6d 2 0 m 4d 3 1 0,1,2,3 -> 0,1,2 -> 1,2 -> 1 06.1 : List 73
n = int(input()) 11 for d in range(1,2*n) : 32 63 q = ['G']*n + ['B']*n 13 4 k=0 14 6 while len(q) > 1 : k = (k + d) % len(q) q.pop(k) if q[0] == 'G' : print(d) break else: print('Not found') 74 06.1 : List
06.2 : Nested List x x[k] x[k][j] j x[k] 2, len(x[4]) 0 x = [] x.append( 1 ) # [1] # [1,[2,3]] x.append( [2,3] ) # [1,[2,3],[4,5,6]] # [1,[2,3],[4,5,6],[[7,8]]] x.append( [4,5,6] ) # [1,[2,3],[4,5,6],[[7,8]],[]] x.append( [[7,8]] ) 3, x[2][1] 5, x[3][0][1] 8, len(x[3][0]) x.append( [] ) len(x) 5, x[1][1] x, y [[0.0, 0.0, 10.0], [1.0, 5.0, 3.0]] circles = [] file1 = open('c:/temp/circles.txt') for line in file1 : x,y,r = line.split() circles.append( [float(x),float(y),float(r)] ) free = [] for i in range(len(circles)) : for j in range(i+1,len(circles)) : dx = circles[i][0] - circles[j][0] dy = circles[i][1] - circles[j][1] sumr = circles[i][2] + circles[j][2] if dx**2 + dy**2 <= sumr**2 : break else : free.append(circles[i]) for c in free : print(c) 06.2 : Nested List 75
m×n AB A len(A) A = [ [1,2], [1,1] ] m B = [ [0,1], [4,3] ] len(A[0]) = len(A[1]) C = [] = ... = len(A[m-1]) = n for i in range(len(A)) : 0 C.append([0]*len(A[i])) A = [] for j in range(len(A[i])) : for k in range(5): C[i][j] = A[i][j] + B[i][j] A.append( [0]*3 ) username 0 1 usernames f = [ ['noon',['pat','koi']], ['wii',['noon','koi']], ['pat',['koi','noon','wii']], ['koi',[]] ] username maxindex = 0 for k in range(1,len(f)) : if len(f[k][1]) > len(f[maxindex][1]) : maxindex = k print(f[maxindex][0],'has max.# of followers :', \\ ', '.join(f[maxindex][1])) max_followers = [] max_username = '' for [username,followers] in f : if len(followers) > len(max_followers) : max_username = username max_followers = followers print(max_username,'has max.# of followers :', \\ ', '.join(max_followers)) 76 06.2 : Nested List
sort f usernames c = [] sort for [username,followers] in f : sort c.append([len(followers),username]) c.sort() for [x,username] in c : print(username) x=[[3,2],[3,1],[9],[2,5],[3]] f = [ ['noon',['pat','koi']], ['wii',['noon','koi']], ['pat',['koi','noon','wii']], ['koi',[]] ] x.sort() x c = [[2,'noon'],[2,'wii'],[3,'pat'],[0,'koi']] c.sort() [[0,'koi'],[2,'noon'],[2,'wii'],[3,'pat']] [[2,5],[3],[3,1],[3,2],[9]] sort username [2,5] < [3] < [3,1] < ... sort() username c = [] for [username,followers] in f : c.append([-len(followers),username]) c.sort() f = [ ['noon',['pat','koi']], ['wii',['noon','koi']], ['pat',['koi','noon','wii']], ['koi',[]] ] c = [[-2,'noon'],[-2,'wii'],[-3,'pat'],[0,'koi']] sort [[-3,'pat'],[-2,'noon'],[-2,'wii'],[0,'koi']] a x x = [[2,3],[3,4]] x.append(a) append [2,3] [3,4] x x = [] x=x+a x += a x.append( [2,3] ) x.append( [3,4] ) x = [] # [2,3,3,4] x = x + [2,3] + [3,4] 06.2 : Nested List 77
* 0 a [a]*n x = [ [0,0,0] ] * 3 print(x) [[0,0,0],[0,0,0],[0,0,0]] x[0][1] = 1 x [[0,1,0],[0,1,0],[0,1,0]] x = [[0]*3]*3 x = [[0,0,0],[0,0,0],[0,0,0]] n=3 a x = [] for i in range(n) : x.append( [0]*n ) [a]*n Problem rc Code c r [[]] c username username usernames username noon pat koi wii noon pat koi noon wii koi f = [['noon',['pat','koi']], ['wii', ['noon']], ['pat', ['koi','noon','wii']], ['koi', []] ] 78 06.2 : Nested List
Problem Code f nn 'xyz','xy','abc' 'xy','abc','xyz' nm n m n m 32 B 9.0 A 3.0 2.0 A 5.0 B 4.0 5.0 C 5.0 C 2.0 3.0 Invalid data: 33 AA AA 4.0 AC B 9.0 8.0 7.0 AC 8.0 5.0 79 06.2 : Nested List
0 1m [ [ , , ..., ], [ , , ..., ], ... ] 1. d0 = [['AA','4.0'],['B','9.0','8.0','7.0'],['AC','8.0','5.0']] 2. d0 err = ['AA','AC'] err 0 Invalid data err 4. err 0 d0 = [['A','3.0','2.0'], ['B','4.0','5.0'], ['C','2.0','3.0']] err = [] 4.1. d0 d1 = [[5.0,'A'], [9.0,'B'], [5.0,'C']] 4.2. d1 d1 = [[9.0,'B'], [5.0,'A'], [5.0,'C']] d1 n,m = [int(e) for e in input().split()] nm d0 = list() for n for k in range(n) : split d0.append(input().split()) d0 err = list() print(d0) for x in d0 : [['AA','4.0'], [B','9.0','8.0','7.0'], if len(x[1:]) != m : ['AC','8.0','5.0']] err.append( x[0] ) x d0 if len(err) > 0 : print('Invalid data: ') x[1:] m for sid in err : print(sid) x[0] else : err err 0 d1 = list() for x in d0 : Invalid data: s=0 AA for e in x[1:] : AC s += float(e) d1.append( [s, x[0]] ) x d0 x[1:] float x[0] d1 print(d1) [[5.0,'A'], [9.0,'B'], [5.0,'C']] 80 06.2 : Nested List
for k in range(len(d1)-1) : d1.sort() for i in range(len(d1)-1) : d1.sort(reverse=True) if d1[i][0] < d1[i+1][0] \\ or (d1[i][0]==d1[i+1][0] \\ and d1[i][1]>d1[i+1][1]) : d1[i],d1[i+1] = \\ d1[i+1],d1[i] for [total,sid] in d1 : print( sid, total ) d1 B 9.0 A 5.0 C 5.0 else : d1 , ] d1.sort() d1 = list() [ , ] for x in d0 : [- s=0 for e in x[1:] : s += float(e) d1.append( [-s, x[0]] ) d1.sort() for sumsc,sid in d1 : print( sid, -sumsc ) [ [ ..., ], [ ..., ], ... ] [ [ [ ..., ] ], [ [ ..., ] ], ... ] 06.2 : Nested List 81
Intania 87
06.3 : List Comprehension map: map: b a b = [] b = [ e%10 for e in a ] for e in a : b.append( e%10 ) filter: filter: ba 5 b = [] b = [e for e in a if len(e)>5] for e in a : if len(e) > 5 : b.append( e ) map & filter: a map & filter: b = [2.54*e for e in a if e>10] 10 b b = [] for e in a : if e > 10 : b.append( 2.54*e ) 0 20 c = [[a,b] for a in range(0,21) \\ for b in range(a,21) \\ c = [] if a+b == a*b ] for a in range(0,21) : for b in range(a,21) : if a+b == a*b : c.append( [a,b] ) x = [e for e in a] x = list(a) ax a x a x=a x x xa a 06.3 : List Comprehension 83
x = [int(e) for e in input().split()] split() input() x t = ','.join( [str(e) for e in x] ) x join c = sum( [ 1 for e in x if e%2 == 0 ] ) x sum x1 b = [ (1 if x[i] >= 0 else -1) for i in range(len(x)) ] (1 if x[i] >= 0 else -1) 1 x[i] >= 0 -1 b b[i] = 1 x[i] >= 0 b[i] = -1 x = [ [e for e in range(3)] for k in range(4) ] x = [ list(range(3)) for k in range(4) ] x = [ [0,1,2], [0,1,2], [0,1,2], [0,1,2] ] x = [ [0,1,2] for k in range(4) ] x = [ [0,1,2] ] * 4 x[0], x[1], x[2] x[3] x[0][0] = 9 x [[9,1,2],[9,1,2],[9,1,2]] x = [ [0,1,2] ] * 4 a = [ [x**2+y**2, x, y] for [x,y] in data ] a.sort() d = [ [x,y] for [s,x,y] in a ] data xy a xy a sort x, y d 84 06.3 : List Comprehension
[print(k) for k in range(5)] 01234 [t.append(e) for e in x if e not in t] x tt [None, None, ...] for e in x : if e not in t : t.append(e) for x = [e for e in range(10)] print(e) #e x = [] for e in range(10) : x.append(e) print(e) # 9 e = 99 x = [e for e in range(10)] print(e) # 99 Input x c Process d c x x = ['abba','babana','ann']; c = 'a' d = [2,3,1] Input x x Process 06.3 : List Comprehension 85
Input: x x Process Input Process Input Process Input y x x[i]+y[i] Process z z[i] [1,2,3,4,5,6] Input m Process m m = [[1,2,3],[4,5,6]] Input x x x Process 1. 2. 3. x x Input n n-1 n Process c n-1 x n-1 x ... x x 4,6,8,10,... x 6,9,12,15,... 8,12,16,20,... n-1 2k,3k,4k,5k,... n-1 k = 2,3,4,...,N//2-1 cx 86 06.3 : List Comprehension
Input n Process n 1. n 2. 2 n-1 1 n,m = [int(e) for e in \\ n,m = [int(e) for e in \\ input().split()] input().split()] d0 = list() d0 = [input().split() for k in range(n)] for k in range(n) : err = [x[0] for x in d0 if len(x[1:]) != m] d0.append(input().split()) err = list() if len(err) > 0 : for x in d0 : print('Invalid data: ') print('\\n'.join(err)) if len(x[1:]) != m : err.append(x[0]) else : # list comprehension if len(err) > 0 : # list comprehension print('Invalid data: ') d1 = [[-sum([float(e) for e in x[1:]]), \\ for sid in err : x[0]] for x in d0] print(sid) d1.sort() for ntotal,scode in d1 : else : print( scode, -ntotal ) d1 = list() for x in d0 : s=0 for e in x[1:] : s += float(e) d1.append( [-s, x[0]] ) d1.sort() for ntotal,scode in d1 : print( scode, -ntotal ) 06.3 : List Comprehension 87
1 -4 10 5 2 -3 -5 100 99 0 -1 -1 30 -3 88 06.3 : List Comprehension
5931111121 87.25 Not Found 5932222221 77.00 4 5933333321 82.50 5 5934444421 69.75 Not Found 5935555521 66.00 5934444421 111 100 13 96 1234 96 555 99 2121 96 99 99 1234 429801 78 359124 89 902316 91.25 773842 45.75 264336 06.3 : List Comprehension 89
Not found Not found 5 4 5 5 Not found 5 10 -1 4 5 5 -1 10 5 5 3 5 -1 2 4 2 4 2 4 -1 90 06.3 : List Comprehension
07 : Tuple, Dictionary and Set Tuple list my_list = [1, 2.5, 3, 'A'] list tuple tuple not_a_tuple = 1 my_tuple = (1, 2.5, 3, 'A') ('a','b','c') (1,2,3) my_tuple_2 = (1,) tuple not_a_tuple = (1) t1 = tuple('abc') t2 = tuple([1, 2, 3]) list my_tuple[0] 1 my_tuple[1:3] (2.5, 3) + tuple my_tuple[-2:-1] (3,) * tuple a = (2, 3, 5) b = (7,) p=a+b (2, 3, 5, 7) q = p*2 (2, 3, 5, 7, 2, 3, 5, 7) a += (2,) a = a + (2,) a (2,) tuple (2, 3, 5, 2) tuple a tuple my_tuple[3] = 'B' tuple t = 1,2,3 my_tuple = my_tuple[:3] + ('B',) + my_tuple[4:] print(t) (1, 2, 3) a,b,c = (1,2,3) a = 1, b = 2, c = 3 list tuple x,y = 'A','Z' x = 'A', y = 'Z' a,x = x,a ax L = [('A','B'), (1,20,300), (9.9,)] list tuple L[1] (1,20,300) L[1][2] 300 07 : Tuple, Dictionary and Set 91
tuple list T = (['A','B'], [1,20,300], [9.9]) tuple list tuple tuple T[1] [1,20,300] T[1][2] 300 T tuple T[1] = 9 T[1] T[1][2] = 9 T = ((1,2), (3,4)) tuple tuple T[1][0] 3 tuple1 += (0,) list1 += [0] import time n = 100000 tuple1 += (0,) t0 = time.time() tuple1 = tuple1 + (0,) list1 = [] for k in range(n): tuple list1 += [0] list1 += [0] print(time.time()-t0) list1 = list1 + [0] t0 = time.time() list1.append(0) tuple1 = tuple() for k in range(n): 0 tuple1 += (0,) print(time.time()-t0) Dictionary (key, value) key key value value list, tuple, ... key value key dict dict1 = {} dict1 = dict() key dict dict2 = {'Name':'Tom', 'Age':39} dict ('Name','Tom') ('Age',39) value D = {'Name':'Sarah'} print(D['Name']) D['Age'] = 20 Sarah D['Name'] = 'Somsri' ('Age',20) ('Name','Somsri') 92 07 : Tuple, Dictionary and Set
key dict my_dict = {} = 8.00 my_dict[1] tuple my_dict[3.75] = [3, 7, 5] list, dict set my_dict['Hello'] = 'World' my_dict[(2,99)] = 999 my_dict[[1,2,3]] = '123' list key value key keys(), values(), d = {1:7, 2:8, 3:9} items() for...in for e in d : key d e for e in d.keys() key d e for e in d.values() value d e for e in d.items() d tuple e e[0] key, e[1] value for k,v in d.items() key, value d k, v keys(), values() d = {'aka':'as known as', 'so':'significant other'} x = d.keys() items() list print(x[1]) x list for...in key index x = list( d.keys() ) x x[1] print(x[1]) 'aka' 'so' d.update(d1) A = {1:'one', 2:'two'} dict d1 B = {1:'nueng', 3:'sarm'} d key d1 d A.update(B) A {1:'nueng', 2:'two', 3:'sarm'} value B pop(key) C = {1: 'one', 2: 'two', 3: 'three'} key key x = C.pop(1) x = 'one', C = {2:'two', 3:'three'} error y = C.pop(4) error in key D = {1: 'one', 2: 'two', 3: 'three'} value (key,value) dict x = 1 in D x = True value y = 4 in D.keys() y = False z = 'two' in D.values() z = True w = (2,'two') in D.items() w = True keys items values 07 : Tuple, Dictionary and Set 93
Search
Read the Text Version
- 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
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170