Абстракция множества Python

Содержание
Введение
Синтаксис
Пример
Несколько источников/условий
Похожие статьи

Введение

Set Comprehension - Абстракция множеств.

Похоже на List Comprehension но создаёт не список а множество.

Базовый синтаксис

Вместо того, чтобы создавать пустое множество и добавлять каждый элемент в конец, вы просто определяете множество и его содержимое одновременно, следуя этому формату:

new_set = {expression for member in iterable}

{ expr(item) for item in iterable }

Banner image

Пример

>>> from math import factorial >>> s = {len(str(factorial(x))) for x in range(20)} >>> type(s)

<class 'set'>

>>> print(s)

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18}

Несколько источников/условий

# Multi-input Set Comprehension c = {x * y for x in range(5) for y in range(5)} print(c)

{0, 1, 2, 3, 4, 6, 8, 9, 12, 16}

Article author: Andrei Olegovich

Похожие статьи
Built-in Collections
Списки []
list comprehension: Абстракция списка
Python
if, elif, else
Циклы
Абстракция множеств и словарей

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: