Home > Software design >  Add only number from two string of data
Add only number from two string of data

Time:12-31

I want to make a list from the following data:

Ramesh CUB 10000
Rajesh IndianBank 20000
Ramesh CUB 20000
Arya ICICI 200000
Ramesh ICICI 20000
Rajesh KVB 30000
Vimala SBI 3000
Vimala YesBank 200000
Rajesh IndianBank 2000

such that Money from strings with same name and bank is added to get: Expected Output

Ramesh  CUB 40000
Ramesh ICICI 20000
Rajesh  IndianBank  22000
Rajesh KVB 30000
Arya ICICI 202000
Vimala SBI 33000
Vimala YesBank 200000

CodePudding user response:

Disclaimer: There are many possible ways to solve your problem ! Providing more context on your data and what you want to achieve, what you already tried could provide a more suitable response.

Proposition with pandas

If your data is contained in a file or a string one can see it as a pandas.DataFrame and use it's power :

import pandas as pd
from io import StringIO

pd.read_csv(StringIO('''Ramesh CUB 10000
Rajesh IndianBank 20000
Ramesh CUB 20000
Arya ICICI 200000
Ramesh ICICI 20000
Rajesh KVB 30000
Vimala SBI 3000
Vimala YesBank 200000
Rajesh IndianBank 2000
'''), sep=' ', header=None).groupby([0,1]).sum().reset_index()

Explanations :

  1. I transform the string in StringIO to make it readable by pd.read_csv
  2. I read the data
  3. I groupby the first two columns doing a sum

And the result is :

0   Arya    ICICI   200000
1   Rajesh  IndianBank  22000
2   Rajesh  KVB     30000
3   Ramesh  CUB     30000
4   Ramesh  ICICI   20000
5   Vimala  SBI     3000
6   Vimala  YesBank     200000

CodePudding user response:

Given a string like this:

>>> txt
Ramesh CUB 10000
Rajesh IndianBank 20000
Ramesh CUB 20000
Arya ICICI 200000
Ramesh ICICI 20000
Rajesh KVB 30000
Vimala SBI 3000
Vimala YesBank 200000
Rajesh IndianBank 2000

You can create a dict as a counter and add the entries of splitting the lines into key, values:

di={}
for s in txt.splitlines():
    b,n=s.rsplit(maxsplit=1)
    di[b]=di.get(b,0) int(n)

>>> di
{'Ramesh CUB': 30000, 'Rajesh IndianBank': 22000, 
 'Arya ICICI': 200000, 'Ramesh ICICI': 20000, 
 'Rajesh KVB': 30000, 'Vimala SBI': 3000, 
 'Vimala YesBank': 200000}

Then rejoin into a string if so needed:

>>> print('\n'.join([f'{k} {v}' for k,v in di.items()]))
Ramesh CUB 30000
Rajesh IndianBank 22000
Arya ICICI 200000
Ramesh ICICI 20000
Rajesh KVB 30000
Vimala SBI 3000
Vimala YesBank 200000

CodePudding user response:

Simple OOP solution :

# some fake data
data = [
    "Ramesh CUB 10000",
    "Rajesh IndianBank 20000",
    "Ramesh CUB 20000",
    "Arya ICICI 200000",
    "Ramesh ICICI 20000",
    "Rajesh KVB 30000",
    "Vimala SBI 3000",
    "vimala YesBank 200000",
    "Rajesh IndianBank 2000"
    ]


class Account:

    def __init__(self, name):
        self.name = name
        self.transactions = {}

    def add_transaction(self, trans):
        
        bank_trans = self.transactions.get(trans.bank, False)

        # if threre is no transaction for this bank
        if not bank_trans:
            bank_trans = []
            self.transactions[trans.bank] = bank_trans

        bank_trans.append(trans)

    def show_transactions(self):

        for bank in self.transactions:

            total = 0
            for trans in self.transactions[bank]:
                total  = trans.value
            
            print(f"{self.name} {bank} {total}")

class Transaction:

    def __init__(self, bank, value):
        self.bank = bank
        self.value = value


def split_info(trans_data):

    accounts = {}

    for data in trans_data:
        name, bank, value = data.split(" ")

        account = accounts.get(name, False)

        # if there is no account
        if not account:
            account = Account(name)
            accounts[name] = account
            
        # make transaction for this bank
        trans = Transaction(bank, float(value))

        # add to accounts records
        account.add_transaction(trans)

    return accounts

def accounts_preview(accounts):

    for account in accounts.values():
        account.show_transactions()
        print("------------")
        
# get the extracted info
accounts = split_info(data)

# show each info
accounts_preview(accounts)

CodePudding user response:

You have to create a list of elements where every element is like (x,y,z) where for example x = "Ramesh", y = "CUB", z = "10000".

After that you have your list then order by alphabetic order for the first element with the function:

your_list.sort(key=lambda x:x[0])

0 is the index that refers to your x values

If you want to order regarding the z then change 0 with 2

  • Related