Telegram Group & Telegram Channel
Forwarded from Python/ django
🐍 Python-задача: что выведет этот код с вложенными генераторами?


gen = (x for x in range(3))

def wrap(g):
return (x * 2 for x in g)

gen2 = wrap(gen)

print(list(gen))
print(list(gen2))


🔍 Варианты:
• a) [0, 1, 2], [0, 2, 4]
• b) [0, 1, 2], []
• c) [], [0, 2, 4]
• d) [0, 1, 2], Ошибка

💡 Разбор:

- `gen = (x for x in range(3))` — генератор 0, 1, 2
- `wrap(gen)` — создаёт **новый генератор**, который берёт значения из `gen` и умножает на 2

Но генераторы **исчерпаемы**: после первого полного прохода `list(gen)` → `gen` становится пустым

Значит:

- `list(gen)` → `[0, 1, 2]`
- `gen2 = wrap(gen)` теперь ссылается на **пустой** `gen`
- `list(gen2)` → `[]`

**Правильный ответ: b) `[0, 1, 2]`, `[]`**

🧠 **Вывод:** если оборачиваешь генератор — не "прожигай" его до передачи дальше. Генераторы нельзя перезапустить или "перемотать".

🛠️ Совет: если данные нужны повторно — сохрани их в список:

```
python
data = list(gen)
```

или используй itertools.tee для разветвления итератора.

@pythonl



tg-me.com/python_testit/1184
Create:
Last Update:

🐍 Python-задача: что выведет этот код с вложенными генераторами?


gen = (x for x in range(3))

def wrap(g):
return (x * 2 for x in g)

gen2 = wrap(gen)

print(list(gen))
print(list(gen2))


🔍 Варианты:
• a) [0, 1, 2], [0, 2, 4]
• b) [0, 1, 2], []
• c) [], [0, 2, 4]
• d) [0, 1, 2], Ошибка

💡 Разбор:

- `gen = (x for x in range(3))` — генератор 0, 1, 2
- `wrap(gen)` — создаёт **новый генератор**, который берёт значения из `gen` и умножает на 2

Но генераторы **исчерпаемы**: после первого полного прохода `list(gen)` → `gen` становится пустым

Значит:

- `list(gen)` → `[0, 1, 2]`
- `gen2 = wrap(gen)` теперь ссылается на **пустой** `gen`
- `list(gen2)` → `[]`

**Правильный ответ: b) `[0, 1, 2]`, `[]`**

🧠 **Вывод:** если оборачиваешь генератор — не "прожигай" его до передачи дальше. Генераторы нельзя перезапустить или "перемотать".

🛠️ Совет: если данные нужны повторно — сохрани их в список:

```
python
data = list(gen)
```

или используй itertools.tee для разветвления итератора.

@pythonl

BY Python tests


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/python_testit/1184

View MORE
Open in Telegram


Python tests Telegram | DID YOU KNOW?

Date: |

Telegram announces Search Filters

With the help of the Search Filters option, users can now filter search results by type. They can do that by using the new tabs: Media, Links, Files and others. Searches can be done based on the particular time period like by typing in the date or even “Yesterday”. If users type in the name of a person, group, channel or bot, an extra filter will be applied to the searches.

Spiking bond yields driving sharp losses in tech stocks

A spike in interest rates since the start of the year has accelerated a rotation out of high-growth technology stocks and into value stocks poised to benefit from a reopening of the economy. The Nasdaq has fallen more than 10% over the past month as the Dow has soared to record highs, with a spike in the 10-year US Treasury yield acting as the main catalyst. It recently surged to a cycle high of more than 1.60% after starting the year below 1%. But according to Jim Paulsen, the Leuthold Group's chief investment strategist, rising interest rates do not represent a long-term threat to the stock market. Paulsen expects the 10-year yield to cross 2% by the end of the year. A spike in interest rates and its impact on the stock market depends on the economic backdrop, according to Paulsen. Rising interest rates amid a strengthening economy "may prove no challenge at all for stocks," Paulsen said.

Python tests from us


Telegram Python tests
FROM USA