반응형

 

 

 

 

For Loop + append

result = [i for i in range(10)]
print(result)

result = [i for i in range(10) if i % 2 == 0]
print(result)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 2, 4, 6, 8]

 

 

Nested For Loop

word1 = "ABC"
word2 = "CDF"
result = [i+j for i in word1 for j in word2]
print(result)

result = [i+j for i in word1 for j in word2 if not i==j]
print(result)

result = [[i+j for i in word1 ] for j in word2]
print(result)

['AC', 'AD', 'AF', 'BC', 'BD', 'BF', 'CC', 'CD', 'CF']
['AC', 'AD', 'AF', 'BC', 'BD', 'BF', 'CD', 'CF']
[['AC', 'BC', 'CC'], ['AD', 'BD', 'CD'], ['AF', 'BF', 'CF']]

 

 

 

이중 리스트

# 이중 리스트
words = 'apple banana computer doctor eraser fox grape'.split()
# print(words)

stuff = [[w.upper(), w.lower(), len(w)] for w in words]
for i in stuff:
    print(i)
['APPLE', 'apple', 5]
['BANANA', 'banana', 6]
['COMPUTER', 'computer', 8]
['DOCTOR', 'doctor', 6]
['ERASER', 'eraser', 6]
['FOX', 'fox', 3]
['GRAPE', 'grape', 5]

 

 

 

 

 

반응형

+ Recent posts