user12091669
user12091669

Reputation:

Finding average per integer compared to each element in a given list

So I'm practising data processing of a survey of 3 people for 21 questions.I need to give the average answer given per #.I'm not sure how to separate the numbers AND compare while leaving the letters out.

name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5",
      "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2",
      "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]

Eg. 1=4.33

My attempt:

def most_frequent(name):  

    counter = 0
    num = name[0]  
    for i in range (len(name)): 
        curr_frequency = name[0].count(str(i)) 
        if(curr_frequency> counter): 
            counter = curr_frequency 
            num = i 
    return num

Upvotes: 1

Views: 120

Answers (4)

Sarques
Sarques

Reputation: 463

If you want to print the average of the numbers per #, what you can try is this, without having to split the line every time in a loop.

name = ["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5",
  "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2",
  "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]
name = [name[i].split(" ") for i in range(len(name))]
for i in range(1, len(name[0])):
    print((int(name[0][i])+int(name[1][i])+int(name[2][i]))/3, sep = " ")

Upvotes: 1

oppressionslayer
oppressionslayer

Reputation: 7224

Try this:


name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5",
      "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2",
      "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]
Average = Counter() 
length = 0
for item in name:
    thelist = item.split(' ')[1:]
    length += len(thelist)
    Average += Counter(thelist)
print(Average)

print([(i, Average[i] / length * 100.0) for i in Average])

Outputs:

Counter({'2': 18, '1': 17, '4': 14, '5': 7, '3': 7})

[('4', 22.22222222222222), ('2', 28.57142857142857), ('1', 26.984126984126984), ('5', 11.11111111111111), ('3', 11.11111111111111)]

Upvotes: 1

CanadianCaleb
CanadianCaleb

Reputation: 136

You can make a simple for loop to remove the spaces and letters from the list.

You can use the sum() function on a list to get the sum of that full list, and then divide by the len() of the list to get the average.

This should work :

name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5", "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2", "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]

for i in range(0, len(name)) :

    tempOut = []

    temp = list(name[i])

    for j in range(0, len(temp)) : #65 - 123

        if ord(temp[j]) not in range(65, 122) :

            tempOut.append(temp[j])

    name[i] = ''.join(tempOut)

sums = []

for i in range(0, len(name)) :

    sumTemp = []

    temp = list(name[i].replace(' ', ''))

    for j in range(0, len(temp)) :
        sumTemp.append(int(temp[j]))

    tempSums = sum(sumTemp)/len(temp)

    sums.append(tempSums)

Upvotes: 1

lenik
lenik

Reputation: 23556

You may try this:

name=["AAAAA 4 2 1 2 4 2 4 4 5 2 2 1 5 2 4 3 1 1 3 3 5",
      "BBB 5 2 1 2 4 5 4 4 1 2 2 2 4 4 4 3 1 2 3 3 2",
      "K 4 1 2 1 2 1 2 5 1 1 1 1 4 2 2 1 5 1 3 4 1"]

for line in name :
    parts = line.split()  # using space as a separator
    word = parts[0]       # extract the word
    numbers = map( float, parts[1:] )   # convert the numbers

    print( word, numbers )
    # now you may calculate whatever you want =)

Upvotes: 3

Related Questions