Querying and totaling certian elements in a list

range sum query with updates
queries for number of distinct elements in a subarray
number of distinct elements in a range segment tree
mo's algorithm
range sum query problem
segment tree range maximum query
range update point query segment tree
fenwick tree

So I am very new to python and I have created a list of information that looks like this:

the format is Date, Store, Total income(in that day) and transactions(in that day)

data = [[datetime.datetime(2012, 3, 1, 0), 'PizzaHut', 2839.60, 25], [datetime.datetime(2012, 12, 4, 0), 'Dominoes', 2839.60, 25], [datetime.datetime(2012, 11, 8, 0), 'Sals', 2839.60, 25], [datetime.datetime(2012, 6, 4, 0), 'Sals', 2839.60, 25], [datetime.datetime(2012, 8, 12, 0), 'PizzaHut', 2839.60, 25]

So essentially what I am trying do is to query to total income of the a certain store. so the user should enter for example 'Sals' and it will total up the income of that store based on the data in the list. There is probably a really simple solution to the problem but I have been looking around on the net and decided it was time for me to ask you guys. if I can get some explanation on how it works that would really help me aswell as it helps me to grow my knowledge! Thank you all for your contributions!

Using sum()

Ex:

import datetime
data = [[datetime.datetime(2012, 3, 1, 0), 'PizzaHut', 2839.60, 25], [datetime.datetime(2012, 12, 4, 0), 'Dominoes', 2839.60, 25], [datetime.datetime(2012, 11, 8, 0), 'Sals', 2839.60, 25], [datetime.datetime(2012, 6, 4, 0), 'Sals', 2839.60, 25], [datetime.datetime(2012, 8, 12, 0), 'PizzaHut', 2839.60, 25]]
key = 'Sals'

print( sum(i[2] for i in data if i[1] == key) )

Output:

5679.2

Edit as per comment

import datetime
import collections
data = [[datetime.datetime(2012, 3, 1, 0), 'PizzaHut', 2839.60, 25], [datetime.datetime(2012, 12, 4, 0), 'Dominoes', 2839.60, 25], [datetime.datetime(2012, 11, 8, 0), 'Sals', 2839.60, 25], [datetime.datetime(2012, 6, 4, 0), 'Sals', 2839.60, 25], [datetime.datetime(2012, 8, 12, 0), 'PizzaHut', 2839.60, 25]]
key = 'Sals'

d = collections.defaultdict(list)
for i in data:
    d[i[1]].append(i)

print( "Sum: {0}".format(sum(i[2] for i in d[key])) )
print( "Average: {0}".format(sum(i[2] for i in d[key])/len(d[key])) )

Output:

Sum: 5679.2
Average: 2839.6

Group-By Date

key = datetime.datetime(2012, 12, 4, 0)

d = collections.defaultdict(list)
for i in data:
    d[i[0]].append(i)

python - Querying and totaling certian elements in a list, So essentially what I am trying do is to query to total income of the a certain store. so the user should enter for example 'Sals' and it will total up the income of that  Querying and totaling certian elements in a list. Ask Question Asked 1 year, 9 months ago. Active 1 year, 9 months ago. Viewed 38 times 0. So I am very new to python

I would build a dictionary if searches be frequent.

res = {}
def builddict(data):
  for ele in data:
    res.setdefault(ele[1], 0)
    res[ele[1]] += ele[2] * ele[3]

builddict(data)
print (res['Sals'])

Range sum queries without updates, Given an array arr of integers of size n. We need to compute sum of elements from index i to index j. The queries consisting of i and j index values will be  To query if the array field contains at least one element with the specified value, use the filter { <field> => <value> } where <value> is the element value. To query if the array field contains at least one element with the specified value, use the filter equal( <field>, <value> ) where <value> is the element value.

You can take advantage of namedtuple to make your code more readable:

import datetime

from collections import namedtuple

dailySales = namedtuple('Data', ['day', 'store', 'income', 'transactions'])

data = [[datetime.datetime(2012, 3, 1, 0), 'PizzaHut', 2839.60, 25],
        [datetime.datetime(2012, 12, 4, 0), 'Dominoes', 2839.60, 25],
        [datetime.datetime(2012, 11, 8, 0), 'Sals', 2839.60, 25],
        [datetime.datetime(2012, 6, 4, 0), 'Sals', 2839.60, 25],
        [datetime.datetime(2012, 8, 12, 0), 'PizzaHut', 2839.60, 25]]

namedData = [dailySales(*i) for i in data]

key = 'Sals'

print(sum(j.income for j in namedData if j.store==key))
#5679.2

Queries for number of distinct elements in a subarray, Queries for number of distinct elements in a subarray · Calculate the Sum of GCD over all subarrays Given a array 'a[]' of size n and number of queries q. The DbSet represents a collection of entities of a specific type - the type specified by the type parameter. Queries are specified using Language Integrated Query (LINQ), a component in the .NET Framework that provides query capability against collections in C# or VB. LINQ queries can be written using query syntax or method syntax. Query syntax shares a resemblance with SQL.

Sum data by using a query - Access - Office Support, Access provides several ways to add these sample tables to a and then select a spreadsheet program from the list. The SQL keyword SELECT forms the basis for all SQL querying. Although SELECT technically specifies columns to be used in a query, it is always the first SQL keyword or command in an SQL phrase that includes at least the SQL keyword FROM and often the SQL keyword WHERE.

Count data by using a query - Access - Office Support, Understand ways to count data, count data by using a Total row or by using a totals You can count the number of items in a field (a column of values) by using the by using a totals query instead of a Total row when you need to count some or The following table lists and describes the aggregate functions that Access  LINQ: Test-Driven Learning. This project is aimed to help the user further study LINQ with a test-driven approach. Each unit contains an annotated tutorial and a platform where you can test your understanding of the topic.

List functions, Docs · Power Query M formula language · Power Query M functions; List functions; List functions overview List.NonNullCount, Returns the number of items in a list excluding null values. List. List.InsertRange, Inserts items from values at the given index in the input list. Sum, Returns the sum from a list. The number of elements that match a certain condition. The element that has the greatest or least value. The first element that matches a condition, or the sum of particular values in a specified set of elements. For example, the following query returns the number of scores greater than 80 from the scores integer array:

Comments
  • idownvotedbecau.se/noattempt - being new to python - and even to programming - is not an excuse to not at least give it a try (and no, "looking around the net" is NOT "giving a try").
  • I never said I didn't give it a try? Of course I did I tried different solutions for a long time and couldn't find anything that did what I wanted it to. So I decided that it was time to ask the helpful community at Stackoverflow :)
  • I'm not not talking about "solutions" that you've "been looking around on the net". If you did try something by yourself then you should have posted your attempt and explained what problems you had with it, not asking for a ready-made solution.
  • Awesome! How would I calculate an average of the stores sales as well then? Cheers :)
  • @Jasotufy2 To calculate the average just do print(sum([i[2] for i in data if i[1] == 'Sals'])/len([i for i in data if i[1] == 'Sals']))
  • Okay so one more question... If I wanted to use the date instead of the branch name to query results how could I do that?
  • Just change the key to your required date ex: key = datetime.datetime(2012, 12, 4, 0) and this line d[i[1]].append(i) to d[i[0]].append(i)
  • Still not really getting it to work to well :( could you post it in the code for me please. Much appreciated!