\# Reversing a string using slicing
my\_string \="ABCDE"reversed\_string \= my\_string\[::\-1\]print(reversed\_string)
\# Output
\# EDCBA
2.每个单词的第一个字母大写
使用title函数方法:
my\_string \="my name is chaitanya baweja"
\# using the title() function of string classnew\_string \= my\_string.title()print(new\_string)
\# Output
\# My Name Is Chaitanya Baweja
3.字符串查找唯一元素
使用集合的概念查找字符串的唯一元素:
my\_string \="aavvccccddddeee"
\# converting the string to a set
temp\_set \=set(my\_string)
\# stitching set into a string using join
new\_string \=''.join(temp\_set)print(new\_string)
\# output
\# cdvae
4.重复打印字符串和列表n次
你可以使用乘法符号(*)打印字符串或列表多次:
n \=3 \# number of repetitions
my\_string \="abcd"
my\_list \= \[1,2,3\]print(my\_string\*n)
\# abcdabcdabcd
print(my\_list\*n)
\# \[1,2,3,1,2,3,1,2,3\]
5.列表生成
\# Multiplying each element in a list by 2
original\_list \= \[1,2,3,4\]
new\_list \= \[2\*x for x in original\_list\]print(new\_list)
\# \[2,4,6,8\]
6.变量交换
a \=1
b \=2
a, b \= b, a
print(a) \# 2print(b) \# 1
list\_of\_strings \= \['My','name','is','Chaitanya','Baweja'\]
\# Using join with the comma separator
print(','.join(list\_of\_strings))
\# Output
\# My,name,is,Chaitanya,Baweja
\# finding frequency of each element in a listfrom collections import Counter
my\_list \= \['a','a','b','b','b','c','d','d','d','d','d'\]
count \= Counter(my\_list) \# defining a counter objectprint(count) \# Of all elements
\# Counter({'d':5,'b':3,'a':2,'c':1})print(count\['b'\]) \# of individual element
\# 3print(count.most\_common(1)) \# most frequent element
\# \[('d',5)\]
from collections import Counter
str\_1,str\_2,str\_3 \="acbde","abced","abcda"
cnt\_1, cnt\_2, cnt\_3 \= Counter(str\_1), Counter(str\_2), Counter(str\_3)if cnt\_1 \== cnt\_2:print('1 and 2 anagram')if cnt\_1 \== cnt\_3:print('1 and 3 anagram')
\# output
\# 1and2 anagram
12.使用try-except-else-block模块
except获取异常处理:
a, b \=1,0try:print(a/b)
\# exception raised when b is0
exceptZeroDivisionError:print("division by zero")else:print("no exceptions raised")finally:print("Run this always")
\# output
\# division by zero
\# Run this always
13.使用枚举函数得到key/value对
my\_list \= \['a','b','c','d','e'\]for index, value inenumerate(my\_list):print('{0}: {1}'.format(index, value))
\# 0: a
\# 1: b
\# 2: c
\# 3: d
\# 4: e
14.检查对象的内存使用情况
import sys
num \=21print(sys.getsizeof(num))
\# In Python 2,24
\# In Python 3,28
import time
start\_time \= time.time()
\# Code to check follows
for i inrange(10\*\*5):
a, b \=1,2
c \= a+ b
\# Code to check ends
end\_time \= time.time()
time\_taken\_in\_micro \=(end\_time\- start\_time)\*(10\*\*6)print(time\_taken\_in\_micro)
\# output
\# 18770.217895507812
17.列表展开
from iteration\_utilitiesimport deepflatten
\# if you only have one depth nested\_list, use this
defflatten(l):return \[item for sublist in l for item in sublist\]
l \= \[\[1,2,3\],\[3\]\]print(flatten(l))
\# \[1,2,3,3\]
\# if you don't know how deep the listis nested
l \= \[\[1,2,3\],\[4,\[5\],\[6,7\]\],\[8,\[9,\[10\]\]\]\]print(list(deepflatten(l, depth\=3)))
\# \[1,2,3,4,5,6,7,8,9,10\]
18.列表采样
import random
my\_list \= \['a','b','c','d','e'\]
num\_samples \=2
samples \= random.sample(my\_list,num\_samples)print(samples)
\# \['a','e'\] this will have any2 random values
19.数字化
将整数转化成数字列表:
num \=123456
\# using maplist\_of\_digits \=list(map(int,str(num)))print(list\_of\_digits)
\# \[1,2,3,4,5,6\]
\# using list comprehension
list\_of\_digits \= \[int(x)for x instr(num)\]print(list\_of\_digits)
\# \[1,2,3,4,5,6\]
20.检查列表元素的唯一性
检查列表中每个元素是否为唯一的:
defunique(l):iflen(l)\==len(set(l)):print("All elements are unique")else:print("List has duplicates")
unique(\[1,2,3,4\])
\# All elements are unique
unique(\[1,1,2,3\])
\# List has duplicates