日常练习(10题)

2019-04-16  本文已影响0人  wangcc_sd

1.有一个jsonline格式的文件file.txt大小约为10K

def get_lines():
    with open('file.txt','rb') as f:
        return f.readlines()

if __name__ == '__main__':
    for e in get_lines():
        process(e) # 处理每一行数据
'''

解 使用readlines

# bufsize = 65536
# with open(path) as  infile:
#     while True
#         lines=infile.readlines(bufsize)
#         if not lines:
#             pass
#         for line in lines:
#             process(line)

解法2

# import  mmap
# with open(path) as infile:
#     m = mmap.mmap(infile, 0, access=mmap.ACCESS_READ)
'''

3.输入日期, 判断这一天是这一年的第几天?

'''
# import datetime
# # def dayofyear():
# #     year = input("yesr")
# #     month = input("month")
# #     day = input("day")
# #     date1 = datetime.date(year=int(year), month=int(month), day=int(day))
# #     date2 = datetime.date(year=int(year), month=1, day=1)
# #     return (date1 - date2).days+1
# #
# # print(dayofyear())

'''

4.打乱一个排好序的list对象alist?

'''
# import random
# alist=[1,2,3,4,5,6]
# random.shuffle(alist)
# print(alist)
'''

5.现有字典 d= {'a':24,'g':52,'i':12,'k':33}请按value值进行排序?

'''
# d = {'a': 24, 'g': 52, 'i': 12, 'k': 33}
# a=sorted(d.items(),key=lambda x:x[1])
# print(a)

'''

6.字典推导式

d = {key:value for (key,value) in iterable}

7.请反转字符串 "aStr"?

'''
#print('aStr'[::-1])
'''

8.将字符串 "k:1 |k1:2|k2:3|k3:4",处理成字典 {k:1,k1:2,...}

'''
# str1 = "k:1|k1:2|k2:3|k3:4"
# def strdict(str1):
#     dict1={}
#     for items in str1.split('|'):
#         key,value=items.split(":")
#         dict1[key]=value
#     return dict1
# a=strdict(str1)
# print(a)
'''

9.请按alist中元素的age由大到小排序

'''
# alist = [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}]
# # def sort_by_age(list1):
# #     return sorted(alist,key=lambda x:x['age'],reverse=True)
# # print(sort_by_age(alist))
'''

10.下面代码的输出结果将是什么?

'''
list = ['a','b','c','d','e']
print(list[10:])
#返回空列表
上一篇下一篇

猜你喜欢

热点阅读