简体   繁体   中英

How to store multiple values together in python?

I have written a python code, to count the number of queries, and the number of advertisements from log.

For example (google, 16, 35) where google means query, 16 means query number, 35 means advertisement number.

I thought about defining two dictionaries, one dict is store query->query_number , another query->advertisement , then join these 2 dicts.

But it seems too complex, is it possible to store query, query_num, advertisement_num in a single dictionary?

if match[0].strip():
     if not dict.has_key(match[0]):
        dict[match[0]] = 1
     else:
        dict[match[0]] +=1

this code is used to caculate the queryNum,but I have to still store the adver_count.How can I do?

I have used class to store the query_num and adver_num.Here is my code.How can I do descend sort by adver_num according?Who can help me?Thanks

import re
dict={}
class log:
    def __init__(self,query_num, adver_num):
        self.query_num = query_num
        self.adver_num = adver_num
f = open('result.txt','w')

def sort_by_value(d):
   return sorted(d.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)

for line in open("test.log"):
   count_result = 0
   query_num = 0
   match=re.search('.*qry=(.*?)qid0.*rc=(.*?)discount',line).groups()
   counts=match[1].split('|')
   for count in counts:
      count_result += int(count)
   if match[0].strip():
     if not dict.has_key(match[0]):
        dict[match[0]] = log(1,count_result)
     else:
        query_num = dict[match[0]].query_num+1;
        count_result = dict[match[0]].adver_num+count_result;
        dict[match[0]] = log(query_num,count_result)
     #f.write("%s\t%s\n"%(match[0],count_result))

sort_by_value(dict)

for i in dict.keys():
    f.write("%s\t%s\t%s\n"%(i,dict[i].query_num,dict[i].adver_num))

You could use only one dict, query as key, and a tuple (query_num, advertisement_num) as value.

Code example:

when calculating the queryNum,

if match[0].strip():
    if not dict.has_key(match[0]):
        dict[match[0]] = (1,0)
    else:
        qnum, adnum = dict[match[0]]
        dict[match[0]] = (qnum + 1, adnum)

You can create classes for yourself:

class YourClass:
    def __init__(self, query, query_num, adver_num):
        self.query = query
        self.query_num = query_num
        self.adver_num = adver_num

Thank you can handle it like:

your_dictionary['google'] = YourClass('google', 16, 35)

And access values like:

print your_dictionary['google'].query, your_dictionary['google'].query_num, your_dictionary['google'].adver_num

Yes, you can. Using query as the key you can simply use a tuple as the value for the dict:

d = {"google": (16, 35)}

You can even use a dictionary as the value:

d = {"google": {"query number": 16, "advertisement number": 35}}

You can save the query sets into one and the same list with itertools chain as following;

#Import itertools
from itertools import chain
# use itertools chain to store your querysets into a list/variable like "store" 
store = list(chain(query_num, advertisement_num))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM