ad_items_dict_copy = ad_items_dict.copy()
ad_items_dict = {'1': {
'1.1': {
'url_dict': {
'1.1': 123
}
}
}}
for kitem, vitem in ad_items_dict.items():
for value in vitem.values():
value['url_dict'] = {k.replace('.', '_'): v for k, v in value['url_dict'].items()}
ad_items_dict[kitem] = {k.replace('.', '_'): v for k, v in vitem.items()}
# {'1': {'1_1': {'url_dict': {'1_1': 123}}}}
>>> from dateutil.relativedelta import relativedelta
>>> from dateutil.parser import parse
>>> input_iso8601 = '2017-04-22T13:56:43.914375+03:00'
>>> t = parse(input_iso8601)
>>> t
datetime.datetime(2017, 4, 22, 13, 56, 43, 914375, tzinfo=tzoffset(None, 10800))
>>> t2 = t + relativedelta(hour=0, minute=0, second=0, microsecond=0)
>>> t2
datetime.datetime(2017, 4, 22, 0, 0, tzinfo=tzoffset(None, 10800))
>>> t2.isoformat()
'2017-04-22T00:00:00+03:00'
>>> from pathlib import Path
>>> d = Path('/bin')
>>> q = d / 'cat'
>>> q.exists()
Out[6]:
True
>>> q.is_dir()
Out[7]:
False
>>> Path('/bin/cat').exists()
Out[8]:
True
>>> class Foo:
def __getattr__(self, item):
return lambda: item
>>> foo = Foo()
>>> foo.a
Out[8]: <function __main__.Foo.__getattr__.<locals>.<lambda>>
>>> foo.a()
Out[9]: 'a'
>>> table = {
"а": "a",
"б": "b",
"в": "v",
"г": "g",
"д": "d",
"е": "e",
"ё": "e",
"ж": "zh",
"з": "z",
"и": "i",
"й": "i",
"к": "k",
"л": "l",
"м": "m",
"н": "n",
"о": "o",
"п": "p",
"р": "r",
"с": "s",
"т": "t",
"у": "u",
"ф": "f",
"х": "h",
"ц": "c",
"ч": "ch",
"ш": "sh",
"щ": "sh",
"ъ": "",
"ы": "y",
"ь": "",
"э": "e",
"ю": "yu",
"я": "ya",
}
table = {ord(key): value for key, value in table.items()}
>>> "Жёлудь".translate(table)
Out[16]: 'Жelud'
product('ABCD', repeat=2)
permutations('ABCD', 2)
combinations('ABCD', 2)
combinations_with_replacement('ABCD', 2)
random_product
random_permutation
random_combination
random_combination_with_replacement
if len(NABOR) != len(set(NABOR))
if CARD == SLOGNOST[0]:
elif CARD == SLOGNOST[1]:
...
str(random.choice(SLOGNOST))
print('{0:#b} | {0:b}'.format(11)) # 0b1011 | 1011
>> from decimal import Decimal
>>> quantize = Decimal('.01')
>>> print(Decimal(123.456).quantize(quantize).normalize())
123.46
>>> print(Decimal(123.00).quantize(quantize).normalize())
123
>>> Decimal(0.0005)
Out[23]: Decimal('0.0005000000000000000104083408558608425664715468883514404296875')
>>> Decimal('0.0005')
Out[24]: Decimal('0.0005')
>>> round(Decimal('0.0005'), 3)
Out[25]: Decimal('0.000')
>>> round(Decimal(0.0005), 3)
Out[26]: Decimal('0.001')
>>> round(Decimal(0.0006), 3)
Out[27]: Decimal('0.001')
>>> round(Decimal(0.0004), 3)
Out[28]: Decimal('0.000')
pattern= re.compile(r'\#\w+', re.U)
>>> compound_transpositions = [[2, 3], [3, 4], [4, 5], [5, 6]]
>>> coefficients = [17, 69, 84, 3, 46, 97, 12, 68, 70, 10]
>>> compound_transpositions.sort(key=lambda x: coefficients[x[0]]-coefficients[x[1]])
>>> compound_transpositions
[[4, 5], [3, 4], [2, 3], [5, 6]]
>>> class Test:
a = 5
>>> globals()['Test']()
<Test object at 0x7f73aade9cc0>
a = b[:]
# или
a = b.copy()
b = [1, 2]
>>> b.append(b)
>>> b
[1, 2, [...]]
>>> id(b)
139920848119752
>>> id(b[2])
139920848119752
>>> a = b[:]
>>> a
[1, 2, [1, 2, [...]]]
>>> id(a)
139920848120456
>>> id(a[2])
139920848119752
from copy import deepcopy
>>> a = deepcopy(b)
>>> a
[1, 2, [...]]
>>> id(a)
139920847744840
>>> id(a[2])
139920847744840
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
def previous_and_next(some_iterable):
prevs, items, nexts = tee(some_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return izip(prevs, items, nexts)