Python exercises----Find out which user has the highest ad cost

Source: Internet
Author: User

There are three files, the ad clike user, respectively, where:

AD represents which user was running which ad

Clike represents the daily cost of each ad.

User's corresponding relationship on behalf of users and user names

The contents of the file are as follows:

# Cat AD

Car user_01

Dacar user_01

Youxi user_02

Shouyou user_02

Yeyou user_02

Shuiguo user_03


# CAT Click

Car 20160113 102

Dacar 20160113 109

Youxi 20160113 103

Shouyou 20160113 106

Yeyou 20160113 116

Shuiguo 20160113 111

Shouyou 20160113 123

Yeyou 20160113 121

Shuiguo 20160113 105

Dacar 20160113 108

Youxi 20160113 112

Shouyou 20160113 121

Shuiguo 20160113 109

Shouyou 20160113 112

Yeyou 20160113 101

Shuiguo 20160113 100

Car 20160113 102

Dacar 20160113 109

Youxi 20160113 103

Shouyou 20160113 106

Yeyou 20160113 116

Shuiguo 20160114 111

Shouyou 20160114 123

Yeyou 20160114 121

Shuiguo 20160114 105

Dacar 20160114 108

Youxi 20160114 112

Shouyou 20160114 121

Shuiguo 20160114 109

Shouyou 20160114 112

Yeyou 20160114 101

Shuiguo 20160114 100


# Cat User

user_01Hailei

User_02 Cuikai

User_03 Duanhuan


Question: Which user spends the highest advertising costs?

Ad file, the user name is duplicated and needs to be aggregated by the user.

Code implementation, there is nothing special, read 3 files, load the content into the Python data structure, and then sum, Sort, print.

In order to highlight features, the code does not have validation code.

# -*- coding: utf-8 -*-from collections import defaultdictclick = The  defaultdict (list) ads = defaultdict (list) users = {}#  stores 3 files in the 3 dictionaries above with  Open ("click",  "R")  as f:    for i in f:         v = i.strip (). Split ()          Click[v[0]].append (int (v[2])) With open ("User",  "R")  as f:    for  I in f:        v = i.strip (). Split ()    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;USERS[V[0]]&NBSP;=&NBSP;V[1]#&NBSP;ADS data structure is: defaultdict (<type  ' List ' >, {' user_03 ':  [' Shuiguo '],  ' user_02 ':  [' youxi ',  ' shouyou ',  ' yeyou '],   ' user_01 ':  [' car ',  ' Dacar ']}) With open ("Ad",  "R")  as f:     for i in  F:        v = i.strip (). Split ()          ads[v[1]].append (V[0]) #  will be divided into a user's advertising fee and added to a dictionary that can be username, the value of the list moneys =  Defaultdict (list) for ad in ads:    for g in click:         if g in ads[ad]:             moneys[ad].append (SUM (click[g])) #  the user's advertising costs summed, in reverse order, the first one is the user um =  []for m in moneys:    um.append ((M, sum (moneys[m))) Sorted_um  = sorted (um, key=lambda x: x[1], reverse=true) [0]print sorted_um[0],  USERS[SORTED_UM[0]],&NBSP;SORTED_UM[1]


Answer:

user_02 Cuikai 2030

This article is from the "Candle Shadow Red" blog, be sure to keep this source http://gccmx.blog.51cto.com/479381/1736229

Python exercises----Find out which user has the highest ad cost

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.