1、删除list中重复的元素(同时保持list中的元素的位置不修改):
思路:运用python中set集合的特性(set集合中元素是无序不能够重复的集合)1.将list中通过set删除元素中重复的元素(此时元素是无序),必须采用sort进行元素的排序:sort:可以按照自己定义的方式进行排序。
a=['b','a','b','d','f'] list1=list(set(1)) list1.sort(key=a.index)
将list转换成string:
s=(" ").join(list)
2.kata上一个autocorrect:自动修改的小程序:
题目:https://www.codewars.com/kata/evil-autocorrect-prank/train/python
def autocorrect(input):
text=["u","you","Youuuuu"]
text1=["!","?",".",","]
s1=input.strip().split()
s2=[]
cout=False
for i in s1:
flag=False
word=""
if i[-1] in text1:
flag=True
word=i[-1]
i=i[:-1]
if i in text:
a=["your","sister"]
s2.extend(a)
cout=True
else:
if i not in s2:
s2.append(i)
s2[s2.index(i)]=s2[s2.index(i)]+word
#print s2
s3=(' ').join(s2)
return s3
print (autocorrect("I miss your sister!"))
print (autocorrect("I miss you sister!"))
print (autocorrect("Today i want to go home, I miss you sister! "))