Telegram Group & Telegram Channel
Что выведет этот код?

Могло показаться, что выведется словарь с буквами и их повторением в словаре.

dict.setdefault(key, default=None) - возвращает значение по ключу key, а если ключа нет в словаре, то добавляет пару key=default и возвращает значение default.

dict.get(key, default=None) - возвращает значение по ключу key, а если ключа нет, то возвращает default не добавляя пару в словарь.

То есть мы просто каждой букве в слове поставили значение counter.get(char, 1) + 1 = 2.

Чтобы посчитать реальный счетчик повторении есть несколько способов:


s = "hello"

# 1
counter = {}
for char in s:
counter[char] = counter.setdefault(char, 0) + 1

# 2
counter = {}
for char in s:
counter[char] = counter.get(char, 0) + 1

# 3
from collections import defaultdict
counter = defaultdict(int)
for char in s:
counter[char] += 1

# 4
from collections import Counter
counter = Counter(s)


#dict #counter



tg-me.com/pythrone/13
Create:
Last Update:

Что выведет этот код?

Могло показаться, что выведется словарь с буквами и их повторением в словаре.

dict.setdefault(key, default=None) - возвращает значение по ключу key, а если ключа нет в словаре, то добавляет пару key=default и возвращает значение default.

dict.get(key, default=None) - возвращает значение по ключу key, а если ключа нет, то возвращает default не добавляя пару в словарь.

То есть мы просто каждой букве в слове поставили значение counter.get(char, 1) + 1 = 2.

Чтобы посчитать реальный счетчик повторении есть несколько способов:


s = "hello"

# 1
counter = {}
for char in s:
counter[char] = counter.setdefault(char, 0) + 1

# 2
counter = {}
for char in s:
counter[char] = counter.get(char, 0) + 1

# 3
from collections import defaultdict
counter = defaultdict(int)
for char in s:
counter[char] += 1

# 4
from collections import Counter
counter = Counter(s)


#dict #counter

BY PyThrone




Share with your friend now:
tg-me.com/pythrone/13

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Telegram announces Anonymous Admins

The cloud-based messaging platform is also adding Anonymous Group Admins feature. As per Telegram, this feature is being introduced for safer protests. As per the Telegram blog post, users can “Toggle Remain Anonymous in Admin rights to enable Batman mode. The anonymized admin will be hidden in the list of group members, and their messages in the chat will be signed with the group name, similar to channel posts.”

Find Channels On Telegram?

Telegram is an aspiring new messaging app that’s taking the world by storm. The app is free, fast, and claims to be one of the safest messengers around. It allows people to connect easily, without any boundaries.You can use channels on Telegram, which are similar to Facebook pages. If you’re wondering how to find channels on Telegram, you’re in the right place. Keep reading and you’ll find out how. Also, you’ll learn more about channels, creating channels yourself, and the difference between private and public Telegram channels.

telegram from jp


Telegram PyThrone
FROM USA