상세 컨텐츠

본문 제목

리스트 조회

python

by bumychoi 2024. 7. 4. 19:23

본문

 

x=["grapes","mango","orange","peach","kiwi","apple","lime","cherry","tomato","blueberry","watermelon"]
a=[]
for i in x:
    if i == "apple" or  i=="kiwi":
        a.append(i.upper())

print(a)
# 추가 방법
# 방법1
ex1=[]
for i in range(len(x)):
    if x[i] == "apple" or x[i] =="kiwi":
        ex1.append(x[i].upper())
print(f'ex1 결과:{ex1}')

# 방법2
ex2=map(lambda b:b.upper(),(filter(lambda a:a=="apple" or a=="kiwi",x)))
ex2=list(ex2)
print(f"ex2결과:{ex2}")

#방법3

ex3 = [a.upper() for a in x if a=="apple" or a=="kiwi"]

print(f'ex3결과:{ex3}')

'python' 카테고리의 다른 글

range 심화  (0) 2024.07.04
range 함수  (0) 2024.07.04
파이썬 기초(슬라이씽)  (0) 2024.07.04
파이썬 기초문법 3  (0) 2024.07.03
파이썬 기초문법 2  (0) 2024.07.03

관련글 더보기