woneuy01 / python2

0 stars 0 forks source link

List comprehensions ZIP #8

Open woneuy01 opened 4 years ago

woneuy01 commented 4 years ago

[ for in if ]

things = [2, 5, 9]

yourlist = [value * 2 for value in things]

print(yourlist)


[4, 10, 18]

woneuy01 commented 4 years ago

def keep_evens(nums): new_list = [num for num in nums if num % 2 == 0] return new_list

print(keep_evens([3, 4, 6, 7, 0, 1]))


[4, 6, 0]

woneuy01 commented 4 years ago

things = [3, 4, 6, 7, 0, 1]

chaining together filter and map:

first, filter to keep only the even numbers

double each of them

print(map(lambda x: x*2, filter(lambda y: y % 2 == 0, things)))

equivalent version using list comprehension

print([x*2 for x in things if x % 2 == 0])


[8, 12, 0] [8, 12, 0]

woneuy01 commented 4 years ago

L = [12, 34, 21, 4, 6, 9, 42] lst = [] for x in L: if x > 10: lst.append(x) print(lst)

lst2 = [li for li in L if li>10] // 위의 코드를 한줄로 만들면 이렇게 됨


[12, 34, 21, 42]

woneuy01 commented 4 years ago

Write code to assign to the variable compri all the values of the key name in any of the sub-dictionaries in the dictionary tester. Do this using a list comprehension.

tester = {'info': [{"name": "Lauren", 'class standing': 'Junior', 'major': "Information Science"},{'name': 'Ayo', 'class standing': "Bachelor's", 'major': 'Information Science'}, {'name': 'Kathryn', 'class standing': 'Senior', 'major': 'Sociology'}, {'name': 'Nick', 'class standing': 'Junior', 'major': 'Computer Science'}, {'name': 'Gladys', 'class standing': 'Sophomore', 'major': 'History'}, {'name': 'Adam', 'major': 'Violin Performance', 'class standing': 'Senior'}]}

compri=[ li['name'] for li in tester['info']]


['Lauren', 'Ayo', 'Kathryn', 'Nick', 'Gladys', 'Adam']

woneuy01 commented 4 years ago

L1 = [3, 4, 5] L2 = [1, 2, 3] L3 = []

for i in range(len(L1)): L3.append(L1[i] + L2[i])

print(L3)


[4, 6, 8]

woneuy01 commented 4 years ago

L1 = [3, 4, 5] L2 = [1, 2, 3] L4 = list(zip(L1, L2)) print(L4)


[(3, 1), (4, 2), (5, 3)]

woneuy01 commented 4 years ago

L1 = [3, 4, 5] L2 = [1, 2, 3] L3 = [] L4 = list(zip(L1, L2))

for (x1, x2) in L4: L3.append(x1+x2)

print(L3)


[4, 6, 8]

woneuy01 commented 4 years ago

L1 = [3, 4, 5] L2 = [1, 2, 3] L3 = [x1 + x2 for (x1, x2) in list(zip(L1, L2))] // 가장 간단하게 표현하면 이렇다. print(L3)


[4, 6, 8]

woneuy01 commented 4 years ago

L1 = [3, 4, 5] L2 = [1, 2, 3] L3 = map(lambda x: x[0] + x[1], zip(L1, L2)) print(L3)


[4, 6, 8]

woneuy01 commented 4 years ago

def possible(word, blanked, guessesmade): if len(word) != len(blanked): return False for i in range(len(word)): bc = blanked[i] wc = word[i] if bc == '' and wc in guessesmade: return False elif bc != '' and bc != wc: return False return True

print(possible("wonderwall", "_onrll", "otnqurl")) print(possible("wonderwall", "_onrll", "wotnqurl"))


True False

woneuy01 commented 4 years ago

def possible(word, blanked, guessesmade): if len(word) != len(blanked): return False for (bc, wc) in zip(blanked, word): if bc == '' and wc in guessesmade: return False elif bc != '' and bc != wc: return False return True

print(possible("wonderwall", "_onrll", "otnqurl")) print(possible("wonderwall", "_onrll", "wotnqurl"))


True False

woneuy01 commented 4 years ago

Using zip and list comprehension, create a new list, L3, that sums the two numbers if the number from L1 is greater than 10 and the number from L2 is less than 5. This can be accomplished in one line of code.

L1 = [1, 5, 2, 16, 32, 3, 54, 8, 100] L2 = [1, 3, 10, 2, 42, 2, 3, 4, 3]

L3 =[ a+b for a,b in zip(L1,L2) if a>10 and b<5] print(L3)


[18, 57, 103]