color_list = ["Red", "Blue", "Green", "Black"]
print(color_list) ## ==> ['Red', 'Blue', 'Green', 'Black']
color_list.append("Yellow")
print(color_list) ## ==> ['Red', 'Blue', 'Green', 'Black', 'Yellow']
+
사용list1 = list1 + list2
+를 사용하여 추가할 때는 대괄호를 사용하여 리스트의 형태로 만든 후 추가한다.
list와 list를 합치는 것 .
append와 +랑 다르게 원하는 위치에 element 삽입 가능하다.
list(원하는 위치,element)
cities.insert(1,"의왕시")
cities = [
"서울특별시",
"부산광역시",
"인천광역시",
"대구광역시",
"대전광역시",
]
cities[2] = "경기도 성남시"
를 하면 인천광역시가 경기도 성남시로 바뀐다.
리스트 문제 ! 진짜 어렵다 진짜로 진짜 진짜진짜임...
값을 추출하고 저장해주고 다시 넣기! 이거하나 생각하는데 2시간이 걸렸다..ㅎㅎ
처음보는 개념 (set)
:list의 친척
리스트와 다른점
set1 = {1,2,3}
set2 = set([1, 2, 3])
add 사용
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
> {1, 2, 3, 4}
remove 사용
my_set = {1, 2, 3}
my_set.remove(3)
print(my_set)
> {1, 2}
in 키워드 사용
my_set = {1, 2, 3}
if 1 in my_set:
print("1 is in the set")
> 1 is in the set
if 4 not in my_set:
print("4 is not in the set")
> 4 is not in the set
set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
print(set1 & set2)
> {4, 5, 6}
print(set1.intersection(set2))
> {4, 5, 6}
|
키워드 혹은 union 함수사용print(set1 | set2)
> {1, 2, 3, 4, 5, 6, 7, 8, 9}
print(set1.union(set2))
> {1, 2, 3, 4, 5, 6, 7, 8, 9}
1.Set 과 Dictionary 의 차이
set은 대괄호 []를 사용할 수 없다.
set은 값만 가지고있다.
dictionary는 value와 key를 갖고 있다.
set은 중복된 값을 가질 수 없다.
dictionary의 key는 중복된 값을 가질 수 없지만 value값은 중복된 값을 가질 수 있다.
2. List 와 Tuple 의 차이
리스트는 변경 가능
튜플은 변경 불가능하다.