python leecode

段落文字选出偶数行

a = open("gg.txt")
bb = []
lines = a.readlines()
for line in lines:
    line = line.strip('\n')
    bb.append(line)
for i in bb[1::2]:
    print(i)   

1.去掉回车符号
2.获取偶数行

一段话中的字符数

a = input("str=")
a = a.split()
dict = {}
for i in a :
    num = 1 
    if i not in dict.keys():
        dict[i] = num
    else :
        num = dict[i]
        num = num + 1
        dict[i] = num
for key in dict.keys():
    print("{} {}".format(key,dict[key]))

你可能感兴趣的:(python leecode)