-
已知一个数字列表,统计偶数的个数
[10, 34, 5, 65, 66, 89, 82]
结果: 4
list1 = [10,34,5,65,66,89,82] new_list = [x for x in list1 if x %2 ==0] print('偶数个数为:',len(new_list))
-
已知一个数字列表,计算所有元素的平均值
[23, 34, 56, 9, 80]
list1=[23, 34, 56, 9, 80] sum = 0 for x in list1: sum += x print('平均值为:',sum/len(list1))
-
已知一个列表,统计列表中整数的个数
[‘abc’, True, 12.5, 20, ‘hello’, 10]
结果:2
list1=['abc', True, 12.5, 20, 'hello', 10] int1 = [x for x in list1 if type(x) == int] print(len(int1))
-
已知一个列表,统计列表中非整数的个数
[‘abc’, True, 12.5, 20, ‘hello’, 10]
结果:4
list1=['abc', True, 12.5, 20, 'hello', 10] value1 = [x for x in list1 if not( type(x) == int)] print(len(value1))
-
已知一个列表,计算列表中所有数字的和
[‘abc’, True, 12.5, 20, ‘hello’, 10]
结果:
list1=['abc', True, 12.5, 20, 'hello', 10] sum1 =[x for x in list1 if type(x)==int or type(x)==float] print(sum(sum1))
-
已知一个列表,计算所有大于100的数的和
[‘abc’, 23, 101, 12,5, ‘hello’, 120.8]
结果:221.8
list1=['abc', 23, 101, 12,5, 'hello', 120.8] sum1=[x for x in list1 if (type(x)==int or type(x)==float) and x>100] print(sum(sum1))
-
已知一个列表,提取列表中所有字符串元素的最后一个字符。
[‘hello’, ‘how’, 10, ‘are’, 12.5, True]
结果: [‘0’, ‘w’, ‘e’]
list1=['hello', 'how', 10, 'are', 12.5, True] value1 = [x[-1] for x in list1 if type(x)==str] print(value1)
-
根据要求完成以下功能:
已知变量orders中保存一个店家的所有订单数据,每个订单包含:订单ID、用户ID、订单金额、支付金额、订单来源渠道、是否退货
orders = [ {'orderID': '254118088', 'userID': '157213', 'goodsID': 'PR000064', 'orderAmount': '272.51', 'payment': '272.51 ', 'chanelID': None, 'chargeback': False}, {'orderID': '263312190', 'userID': '191121', 'goodsID': 'PR000583', 'orderAmount': '337.93', 'payment': '337.93 ', 'chanelID': '渠道-0765', 'chargeback': True}, {'orderID': '188208169', 'userID': '211918', 'goodsID': 'PR000082', 'orderAmount': '905.68', 'payment': '891.23 ', 'chanelID': '渠道-0530', 'chargeback': False}, {'orderID': '203314910', 'userID': '201322', 'goodsID': 'PR000302', 'orderAmount': '786.27', 'payment': '688.88 ', 'chanelID': None, 'chargeback': False}, {'orderID': '283989279', 'userID': '120872', 'goodsID': 'PR000290', 'orderAmount': '550.77', 'payment': '542.51 ', 'chanelID': '渠道-9527', 'chargeback': False}, {'orderID': '279103297', 'userID': '146548', 'goodsID': 'PR000564', 'orderAmount': '425.2', 'payment': '425.20 ', 'chanelID': '渠道-0765', 'chargeback': False}, {'orderID': '316686066', 'userID': '148994', 'goodsID': 'PR000709', 'orderAmount': '1764.37', 'payment': '1707.04 ', 'chanelID': '渠道-0396', 'chargeback': False}, {'orderID': '306447069', 'userID': '104863', 'goodsID': 'PR000499', 'orderAmount': '499.41', 'payment': '480.42 ', 'chanelID': '渠道-0007', 'chargeback': False}, {'orderID': '290267674', 'userID': '206155', 'goodsID': 'PR000253', 'orderAmount': '1103', 'payment': '1050.95 ', 'chanelID': '渠道-0330', 'chargeback': False}, {'orderID': '337079027', 'userID': '211918', 'goodsID': 'PR000768', 'orderAmount': '465.41', 'payment': '465.41 ', 'chanelID': '渠道-9527', 'chargeback': False}, {'orderID': '417411381', 'userID': '148994', 'goodsID': 'PR000483', 'orderAmount': '279.53', 'payment': '279.53 ', 'chanelID': '渠道-0007', 'chargeback': False}, {'orderID': '254286596', 'userID': '174586', 'goodsID': 'PR000322', 'orderAmount': '622.7', 'payment': '622.70 ', 'chanelID': '渠道-0283', 'chargeback': False}, {'orderID': '303647260', 'userID': '178023', 'goodsID': 'PR000685', 'orderAmount': '969.61', 'payment': '913.58 ', 'chanelID': '渠道-0765', 'chargeback': False}, {'orderID': '347419495', 'userID': '209896', 'goodsID': 'PR000483', 'orderAmount': '279.18', 'payment': '225.15 ', 'chanelID': '渠道-0396', 'chargeback': False}, {'orderID': '384544993', 'userID': '148994', 'goodsID': 'PR000004', 'orderAmount': '3424.78', 'payment': '3424.78 ', 'chanelID': '渠道-0530', 'chargeback': False}, {'orderID': '322802617', 'userID': '125220', 'goodsID': 'PR000812', 'orderAmount': '430.69', 'payment': '4223.48 ', 'chanelID': '渠道-0530', 'chargeback': False}, {'orderID': '399101394', 'userID': '183645', 'goodsID': 'PR000025', 'orderAmount': '703.39', 'payment': '553.37 ', 'chanelID': '渠道-9527', 'chargeback': False}, {'orderID': '274413321', 'userID': '162256', 'goodsID': 'PR000813', 'orderAmount': '227.42', 'payment': '200.37 ', 'chanelID': '渠道-0530', 'chargeback': False}, {'orderID': '362677803', 'userID': '217238', 'goodsID': 'PR000400', 'orderAmount': '169.04', 'payment': '106.99 ', 'chanelID': '渠道-0789', 'chargeback': True} ]
-
(数据清洗)删除orders中支付金额大于订单金额的订单
for order in orders: order['orderAmount'] = float(order['orderAmount']) order['payment'] = float(order['payment']) #方法一:(容易删不干净所以要加一个[:]) for x in orders[:]: if x['payment']>x['orderAmount']: orders.remove(x) print(orders)
-
计算这个店家的总订单金额和总支付金额以及实际销售额(店家真正拿到手中钱)
sum1 = sum2 = sum3 =0 for order in orders: sum1 += order['orderAmount'] sum2 += order['payment'] if not order['chargeback']: sum3 += order['payment'] print('总订单金额',sum1) print('总支付金额',sum2) print('实际销售额',sum3)
-
统计这个店家的订单的订单来源渠道的数量
chanels1 = set([order['chanelID'] for order in orders if order['orderlID']]) print('源渠道的数量',len(chanels1))
-
计算订单退货率
chargeback_count = total = 0 for order in orders: total += 1 if order['chargeback']: chargeback_count +=1 print('退货率:',chargeback_count/total)
-
获取最优广告渠道
chanels_count = [] for chanel in chanels2: count = 0 for order in orders: if order['chanelID'] ==chanel: count +=1 chanels_count.append(count) print(chanels2) print(chanels_count) max_count = max(chanels_count) #获取数量和最大值相等的渠道 best= [] for index in range(len(chanels_count)): if chanels_count[index] == max_count: best.append(chanels2[index]) print('最优渠道:',best)
-
将订单渠道为None的订单的渠道值修改成最优广告渠道
for order in order: if not order['chanelID']: order['chanelID'] = best[0] print(order)
-
在每个订单数据中添加一个键值对保存订单的折扣值
for order in order: order['discount'] = order['payment']/order['orderAmount'] print(orders)
-
day8-字典和集合作业
最新推荐文章于 2022-11-30 00:27:40 发布