8、练习题2 P94
# 1. 写函数,计算一个列表中有多少个数字,打印: 列表中有%s个数字。
# 提示:type('x') == int 判断是否是数字。
"""
# 方式一:
def get_list_counter1(data_list):
count = 0
for item in data_list:
if type(item) == int:
count += 1
msg = "列表中有%s个数字" %(count,)
print(msg)
get_list_counter1([1,22,3,'alex',8])
# 方式二:
def get_list_counter2(data_list):
count = 0
for item in data_list:
if type(item) == int:
count += 1
return count
v = get_list_counter1([1,22,3,'alex',8])
msg = "列表中有%s个数字" %(v,)
print(msg)
"""
# 2. 写函数,计算一个列表中偶数索引位置的数据构造成另外一个列表,并返回。 P96
"""
# 方式一:
def get_data_list1(arg):
v = arg[::2]
return v
data = get_data_list1([11,22,33,44,55,66])
# 方式二:
def get_data_list2(arg):
v = []
for i in range(0,len(arg)):
if i % 2 == 0:
v.append(arg[i])
return v
data = get_data_list2([11,22,33,44,55,66])
"""
# 3. 读取文件,将文件的内容构造成指定格式的数据,并返回。
"""
a.log文件
alex|123|18
eric|uiuf|19
...
目标结构:
a. ["alex|123|18","eric|uiuf|19"] 并返回。
def create_list(file):
with open("a.log",mode='r',encoding='utf-8') as f:
content = f.read()
lists = []
data =content.split("\n")
return data
b. [['alex','123','18'],['eric','uiuf','19']]
c. [
{'name':'alex','pwd':'123','age':'18'},
{'name':'eric','pwd':'uiuf','age':'19'},
]
"""
标签:count,alex,07,get,练习,list,arg,data,函数
From: https://www.cnblogs.com/jhchena/p/18432415