#交换变量值
a, b = 5, 10
print(a, b)
a, b = b, a
print(a, b)
a = ['Python ','is',' awesome']
b =''.join(a)
print(b)
'''most frequent element in a list'''
a = [1,2,3,1,2,3,2,2,4,5,1]
print(max(set(a),key = a.count))
'''using Counter from collections'''
from collections import Counter
cnt = Counter(a)
print(cnt.most_common(3))
from collections import Counter
str1 = "abcdef"
str2 = "afbecd"
print(Counter(str1) == Counter(str2))
'''reversinig string with special case of slice step param'''
a = 'abcdefghigklmnopqrstuvwxyz'
print(a[::-1])
'''itering over string contens in reverse efficiently'''
for char in reversed(a):
print(char)
'''reversing an integer through type conversion and slicing'''
num = 123456789
print(int(str(num)[::-1]))
#反转列表
'''reversinig list with special case of slice step param'''
a = [5,4,3,2,1]
print(a[::-1])
'''itering over list contens in reverse efficiently'''
for ele in reversed(a):
print(ele)
'''reversinig list with special case of slice step param'''
a = [5,4,3,2,1]
print(a[::-1])
'''itering over list contens in reverse efficiently'''
for ele in reversed(a):
print(ele)
original = [['a','b'],['c','d'],['e','f']]
transposed = zip(*original)
print(list(transposed))
b = 6
print(4 < b < 7)
print(1 == b <20)
def product(a,b):
return a * b
def add(a,b):
return a + b
b= True
print((product if b else add)(5,7))
d = {'a':1,'b':2}
print(d)
print(d.get('a'))
print(d.get(1))
#通过【键】排序字典元素
d = {'apple':18,'orange':20,'banana':5,'rotten tomato':1}
print(sorted(d.items(),key=lambda x:x[1]))
from operator import itemgetter
print(sorted(d.items(),key=itemgetter(1)))
print(sorted(d, key=d.get))
a = [1,2,3,4,5]
for el in a:
if el == 0:
break
else:
print('did not break out of for loop')
items = ['foo','bar','xyz']
print(','.join(items))
numbers = [2,3,5,10]
print(','.join(map(str,numbers)))
data = [2,'hello',3,3.4]
print(','.join(map(str,data)))
d1 = {'a':1}
d2 = {'b':2}
print({**d1,**d2})
print(dict(d1.items() | d2.items()))
d1.update(d2)
print(d1)
lst = [40,10,20,30]
def minIndex(lst):
return min(range(len(lst)),key = lst.__getitem__)
def maxIndex(lst):
return max(range(len(lst)),key = lst.__getitem__)
print(minIndex(lst))
print(maxIndex(lst))
items = [2,2,3,3,1]
newitems2 = list(set(items))
print(newitems2)
from collections import OrderedDict
items = ["foo","bar","bar","foo"]
print(list(OrderedDict.fromkeys(items).keys()))
a = [1,2,3,4,5]
print(list(a))
a = [1,2,3,4,5]
print(a.copy())
a = [1,2,3,4,5]
b = a
b[0] = 10
print(a)
print(b)
a = [1,2,3,4,5]
b = a[:]
b[0] = 10
print(a)
print(b)
from copy import deepcopy
l = [[1,2],[3,4]]
l2 = deepcopy(l)
print(l2)