Попытки убрать блок else, либо заменить "Нет вхождения" на pass или None - выдают ошибку.
a if q else b
- это тернарный оператор. У него три аргумента (a, b, q) и результатом будет a или b в зависимости от истинности q.from collections import Counter
a = [{'id': '1', 'qty': 6}, {'id': '2', 'qty': 1}, {'id': '1', 'qty': 1}, {'id': '2', 'qty': 1}, {'id': '3', 'qty': 10}, {'id': '1', 'qty': 1}]
c = sum((Counter({d['id']: d['qty']}) for d in a), Counter())
result = [dict(id=k, qty=v) for k, v in c.items()]
import os
os.path.expanduser('~/my/path/from/home')
from pathlib import Path
p = Path('~/my/folder').expanduser() # PosixPath('/home/svp/my/folder')
from pathlib import Path
p = Path('~/Documents/Rockstar Games/GTA V/User Music/gta.mp3').expanduser()
class Filterer(object):
"""
A base class for loggers and handlers which allows them to share
common code.
"""
def __init__(self):
"""
Initialize the list of filters to be an empty list.
"""
self.filters = []
def addFilter(self, filter):
"""
Add the specified filter to this handler.
"""
if not (filter in self.filters):
self.filters.append(filter)
# ...
def filter(self, record):
"""
Determine if a record is loggable by consulting all the filters.
The default is to allow the record to be logged; any filter can veto
this and the record is then dropped. Returns a zero value if a record
is to be dropped, else non-zero.
.. versionchanged:: 3.2
Allow filters to be just callables.
"""
rv = True
for f in self.filters:
if hasattr(f, 'filter'):
result = f.filter(record)
else:
result = f(record) # assume callable - will raise if not
if not result:
rv = False
break
return rv
from itertools import repeat
d = {}
for x in [('a', 'd', 'z', 'x'), ('b', 'e ', 'k', 'l'), ('b', 'e', 'm', 'n'), ('c', 'f', 'g', 'h'), ('c', 'f', 'y', 'w')]:
d.setdefault(x[:2], {}).update(zip(x, repeat(None)))
print([list(v.keys()) for v in d.values()])
x[:2]
на tuple(map(str.strip, x[:2]))
print_friends_count(count)
test = (1,2,3)
len(test) > 2
test = (1,2,3)
second = test[2:3]
second = second[0] if second else None