3-1-2-29. Sets

There aren’t 785 countries in the world,

which means that there are duplicate entries in the country’s list.

Slicing the list to see the first few elements confirms this.

It will be useful to remove the duplicates to

produce a list of all the countries that users browse from.

Well, a set in Python does exactly that.

Sets our containers of unique elements without any particular ordering.

We can create a set from a list like this.

Set removes the duplicates and the print function prints

the unique values of which there are 196 countries.

Sets support the in operator the same way lists do.

India is in this set.

You can add elements to sets where you

don’t use the append method like you do with lists,

instead, sets have the add method.

Here, Italy is added.

Sets also have a pop method just like lists.

When you pop an element from a set,

a random element is removed.

Remember that sets, unlike lists,

are unordered, so there is no last element.

Other operations you can perform with sets,

include those of mathematical sets.

Methods like union, intersection,

and difference are easy to perform with sets and

are much faster than such operators with other containers.

인기 있는 검색 엔진을 운영하고 있다고 가정해 보겠습니다.

사용자가 어디에서 탐색하는지 알아보기 위해 설문조사를 진행했습니다.

785개의 응답을 수집하여 국가 목록으로 모았습니다.

전 세계에 785개 국가가 없습니다.

이는 국가 목록에 중복 항목이 있음을 의미합니다.

처음 몇 개의 요소를 보기 위해 목록을 슬라이싱하면 이를 확인할 수 있습니다.

중복을 제거하는 것이 유용합니다.

사용자가 탐색하는 모든 국가의 목록을 생성합니다.

글쎄요, 파이썬의 집합은 정확히 그 일을 합니다.

특별한 순서 없이 고유한 요소의 컨테이너를 설정합니다.

다음과 같이 목록에서 집합을 만들 수 있습니다.

세트는 중복을 제거하고 인쇄 기능은 인쇄합니다.

196개국의 고유한 가치.

집합은 목록과 동일한 방식으로 in 연산자를 지원합니다.

인도는 이 세트에 있습니다.

다음 위치에 집합에 요소를 추가할 수 있습니다.

목록과 같이 추가 방법을 사용하지 마십시오.

대신 세트에는 add 메소드가 있습니다.

여기에 이탈리아가 추가됩니다.

집합에는 목록과 마찬가지로 팝 메서드도 있습니다.

세트에서 요소를 팝할 때,

임의의 요소가 제거됩니다.

목록과 달리 집합은

순서가 지정되지 않았으므로 마지막 요소가 없습니다.

세트로 수행할 수 있는 기타 작업,

수학적 집합을 포함합니다.

합집합, 교집합,

그리고 차이점은 세트로 수행하기 쉽고

다른 컨테이너를 사용하는 이러한 연산자보다 훨씬 빠릅니다.

Sets

set is a data type for mutable unordered collections of unique elements. One application of a set is to quickly remove duplicates from a list.

numbers = [1, 2, 6, 3, 1, 1, 6]
unique_nums = set(numbers)
print(unique_nums)

This would output:

{1, 2, 3, 6}

Sets support the in operator the same as lists do. You can add elements to sets using the add method, and remove elements using the pop method, similar to lists. Although, when you pop an element from a set, a random element is removed. Remember that sets, unlike lists, are unordered so there is no “last element”.

fruit = {"apple", "banana", "orange", "grapefruit"}  # define a set

print("watermelon" in fruit)  # check for element

fruit.add("watermelon")  # add an element
print(fruit)

print(fruit.pop())  # remove a random element
print(fruit)

This outputs:

False
{'grapefruit', 'orange', 'watermelon', 'banana', 'apple'}
grapefruit
{'orange', 'watermelon', 'banana', 'apple'}

Other operations you can perform with sets include those of mathematical sets. Methods like union, intersection, and difference are easy to perform with sets, and are much faster than such operators with other containers.

%d