How do I separate arrays and add them based on their index in the array?

how to split an array into two arrays in java
java split array into multiple arrays
how to split an array into two arrays in c
split an array into two equal sum subarrays
divide array into equal parts in c
split the array gcd
split an array into n equal sum subarrays
split an array into three equal sum subarrays

I am trying to make a wage calculator where a user inserts a .txt file and the program calculates the number of hours worked.

So far I am able to separate the names, wage value, and hours, but I can't figure out how to add the hours together.

So my desired result would be:

Names of Employees Wage (how much they make Added number of hours per employee

Here is the data set (file name of txt is -> empwages.txt):

(Edit: the formatting is messed so heres a screen grab of the text:

Spencer 12.75   8   8   8   8   10
Ruiz    18  8   8   9.5 8   8
Weiss   14.80   7   5   8   8   10
Choi    15  4   7   5   3.3 2.2
Miller  18  6.5 9   1   4   1
Barnes  15  7.5 9   4   0   2

Desired Outcome:

'Spencer', 'Ruiz', 'Weiss', 'Choi', 'Miller', 'Barnes'
'12.75', '18', '14.80', '15', '18', '15'
'42', '41.5', ... and so on

Current code:

infile = open("empwages.txt","r")
masterList = infile.readlines()

nameList = []
hourList = []
plushourList = []
for master in masterList:
    nameList.append(master.split()[0])
    hourList.append(master.split()[1])
    x = 2
    while x <= 6:
        plushourList.append(master.split()[x])
        x += 1


print(nameList)
print(hourList)
print(plushourList)

It is useful that you get familar with the concept of unpacking a list in Python. You can use the following code to solve your problem:

names = []
hours = []
more_hours = []
with open('empwages.txt') as f:
    for line in f:
        name, hour, *more_hs = line.split()
        names.append(name)
        hours.append(hour)
        more_hours.append(more_hs)

print(*names, sep=', ')
print(*hours, sep=', ')
print(*[sum(float(q) for q in e) for e in more_hours])

In case you need the strings as you have requested:

names = []
hours = []
more_hours = []
with open('empwages.txt') as f:
    for line in f:
        name, hour, *more_hs = line.split()
        names.append(name)
        hours.append(hour)
        more_hours.append(more_hs)

print(more_hours)

names = ', '.join(names)
hours = ', '.join(hours)
more_hours = ', '.join(str(s) for s in [sum(float(q) for q in e) for e in more_hours])


print(names)
print(hours)
print(more_hours)
Output
Spencer, Ruiz, Weiss, Choi, Miller, Barnes
12.75, 18, 14.80, 15, 18, 15
42.0 41.5 38.0 21.5 21.5 22.5

Split the array and add the first part to the end, There is a given an array and split it from a specified position, and move Explanation : Split from index 2 and first part {12, 10} add to the end . An array is a range or group of related data values.The values in an array are normally stored in adjacent cells. Learn how arrays are used in formulas and as arguments for specific functions, such as the array forms of the LOOKUP and INDEX functions.

Split an array into two equal Sum subarrays, Recommended: Please try your approach on {IDE} first, before moving on to the solution. A Simple solution is to run two loop to split array and check it is to split array into two parts such that sum of first_part equal to sum of split point index Arrays. thumb_up 4. To-do. Done. 1.8. Based on 39 vote(s). If the array contains an odd number of items, the extra item should be part of the first array. 1. Naive. Naive solution would to create two new arrays and assign elements from first half of the source array to the first array and elements from second half of the source array to the second array.

Well if you're not opposed to using pandas:

import pandas as pd
from StringIO import StringIO
import re

initial_data = '''Spencer 12.75   8   8   8   8   10
Ruiz    18  8   8   9.5 8   8
Weiss   14.80   7   5   8   8   10
Choi    15  4   7   5   3.3 2.2
Miller  18  6.5 9   1   4   1
Barnes  15  7.5 9   4   0   2'''

df = pd.read_csv(StringIO(re.sub(r'[ ]+', ',', initial_data, flags=re.M)), header=None)
print(df)
     0      1    2  3    4    5     6
0  Spencer  12.75  8.0  8  8.0  8.0  10.0
1     Ruiz  18.00  8.0  8  9.5  8.0   8.0
2    Weiss  14.80  7.0  5  8.0  8.0  10.0
3     Choi  15.00  4.0  7  5.0  3.3   2.2
4   Miller  18.00  6.5  9  1.0  4.0   1.0
5   Barnes  15.00  7.5  9  4.0  0.0   2.0

Then you can quickly sum over the columns like so:

df.loc[:, 1:].sum(axis=1)
0    54.75
1    59.50
2    52.80
3    36.50
4    39.50
5    37.50
dtype: float64

Array methods, To make things easier, in this chapter they are split into groups. It returns a new array copying to it all items from index start to end (not If an argument argN is an array, then all its elements are copied. Other objects, even if they look like arrays, are added as a whole: They are based on objects. Based on @Jon's second variant, I made this following for use with PHP Smarty v3 template engine.This is for displaying news/blog with both one or two columns template model.

Add elements of two arrays into a new array, Given two arrays of positive integers, add their elements into a new array. from 0'th index and split the sum into individual digits if it's a two-digit number. In both arrays, data is stored as per index in localstorage. However, in the time array, values are stored in the arrays like [ [key1's value], [key2's value]]. These values will be passed to React component as shown below. But, these values will be converted into separate objects.

Review: Arrays (article) | Arrays, To create an array, we declare a variable like we always do, but then we surround our list of values with square brackets and separate each value with a comma: The zero-based indexing is one of the most confusing aspects of arrays for new We can also add entirely new values to them, using the push() method,� I also want to add a solution to split an array into several smaller arrays containing a determined number of cells. A nice way would be to create a generic/extension method to split any array.

Indexed collections, This includes arrays and array-like constructs such as Array objects and Alternatively, create an empty array first before adding the single element to it. store their elements as standard object properties, using the array index as Remember, JavaScript Array indexes are 0-based: they start at 0 , not 1 . Indexed arrays – Array with numeric indexes. Associative arrays – Array with key-value pairs, its similar to Map in java. Multidimensional arrays – An array of arrays. PHP Indexed arrays. There are two ways to create indexed arrays. – first way to use array() function without any index, index are assigned automatically starting from 0.

Comments
  • Are you opposed to using pandas? This would be a great package to consider for this type of problem.
  • not sure, ultimately i will be putting part of this code into a tkinter GUI imgur.com/a/Ry83ZiB
  • if I were to implement it into a tkinter gui, how would it be like? please see my imgur for reference. (imgur.com/a/Ry83ZiB)
  • @jimmyjohn123, what do you mean? I am no following your question.
  • The code you gave me works perfectly when used in a normal .py file, however, I am trying to implement your code into a gui interface. I am unable to use print(*[sum(float(q) for q in e) for e in more_hours]) as a print statement in the tkinter format.
  • @jimmyjohn123, got you, you need the string.
  • @jimmyjohn123, I updated the answer, now you have variables that you can use containing the string, and if you want to contatenate them, just use the string concatenation operator +.