python-list排序,set的使用

2017-01-01 10:46

list排序,和打印

l = [2, 4, 3, 5]
str = "bcdea"
def badIterable():
    i = 0
    iterable = ["a", "b", "c"]
    for item in iterable:
        print(i, item)
        i += 1
def goodIterable():
    iterable = ["a", "b", "c"]
    for i, item in enumerate(iterable):
        print(i, item)

#结果:[(0, 'a'), (1, 'b'), (2, 'c')]
def listIterable():
    le = list(enumerate('abc'))
    return le
#结果:[(1, 'a'), (2, 'b'), (3, 'c')]
def listIterable1():
    le = list(enumerate('abc', 1))
# 正序
def sortList(li):
    li.sort()
    return li
# 倒序
def reverseList(li):
    # li.reverse()
    temp = li[::-1]
    print(temp)
    return temp

set的使用

# 打印
def testSet():
    my_set = {i * 15 for i in range(100)}
    print(my_set)
# 交集
def intersection():
    x = set("span")
    y = set("pta")
    print(x&y)
# 合集
def collection():
    x = set("span")
    y = set("pta")
    print(x|y)
# 差集
def subtract():
    x = set("span")
    y = set("pta")
    print(x-y)

#去掉重复数据
def removeRepeat():
    a = [11,22,33,44,11,22]
    b = set(a)
    print(b)
    c = [i for i in b]
    print(c)
foo = "t1m3a3pp4mt"    
print("".join(list(set(foo)))[::-1])   # 去掉重复字符串,倒序排列
print("".join(list(set(foo)))[::1]) # 去掉重复自父亲,正序排列