Count inside an array with values from another array

Asked

Viewed 138 times

7

I would like to realize a Count of the values contained within the array1 with the array values nomes, but he’s returning me an output with the strange values

SCRIPT

array1 = [
'VALUE1',
'VALUE1',
'VALUE2',
'VALUE2',
'VALUE2',
'VALUE3',
'VALUE4',
'VALUE5',
]

nomes = ['VALUE1', 'VALUE2']

for valores in array1:
        for linha in nomes:
                print(valores.count(linha))

OUTPUT

1 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0

DESIRED OUTPUT

2 3

4 answers

7


You don’t need two for unless you are going to count 'manually' - without using the menu .count.

The correct thing is to call the method .count of array1, passing each word you want to tell there

array1 = [
'VALUE1',
'VALUE1',
'VALUE2',
'VALUE2',
'VALUE2',
'VALUE3',
'VALUE4',
'VALUE5',
]

nomes = ['VALUE1', 'VALUE2']

for linha in nomes:
        print(array1.count(linha))

The way you are doing, is provided the occurrence of the word on the list "names" within every word of "array1". (Strings are also sequences, so it has the "Count" method")

This works, but it is not the best way - since it will go through array1 once for each searched word - it will not make any difference to a case with only a few lines, and in a program that runs only once - the time must be less than 10 milliseconds. But if this happens in a web application where response time is critical, it can start to make a difference.

The ideal is an algorithm that goes through the initial list (array1) only once, and already count the values of all the keys you find. Python has the class collections.Counter that can do this at once - but to better understand the algorithm:



array1 = ...
nomes = {'VALUE1': 0, 'VALUE2': 0}

for frase in array1:
   if frase in nomes:
       nomes[frase] += 1

print(nomes)

(The two forms so far are only if the match of "names" is for the entire line of "array1" - if the value in "names" is only a part of the sentence in "array1" you have to create something using two "for" even - or by transferring a "for" to a regular Express)

4

Python has a native function called Count() which, as the name says, accounts for.

So it’s much simpler to run it inside a for for each of the items on your list nomesand print the amount of them. It looks like it got much shorter and simple your code:

array1 = [
'VALUE1',
'VALUE1',
'VALUE2',
'VALUE2',
'VALUE2',
'VALUE3',
'VALUE4',
'VALUE5',
]

nomes = ['VALUE1', 'VALUE2']

for x in nomes:
  print(array1.count(x))

3

this method works , has this site that has some other methods for python maybe help https://rstudio-pubs-static.s3.amazonaws.com/325708_d9b07b07bf2b4bc8803ba6d53d834eeb.html

array1 = ['VALUE1','VALUE1','VALUE2','VALUE2','VALUE2','VALUE3','VALUE4','VALUE5',]
nomes = ['VALUE1', 'VALUE2']
value1 = 0
value2 = 0
x= 0
while x < len(array1):
    if array1[x] == nomes[0]:
        value1= value1+1
    elif array1[x] == nomes[1]:
        value2= value2+1
    x= x+1
print(f'o Numero de {nomes[0]} = {value1} \no Numero de {nomes[1]} = {value2}')

There is also the Counts method

2

The problem is that in doing for valores in array1: you are going through the values of this list, passing to the variable valores a string. To get the desired output, do so as follows:

array1 = [
'VALUE1',
'VALUE1',
'VALUE2',
'VALUE2',
'VALUE2',
'VALUE3',
'VALUE4',
'VALUE5',
]

nomes = ['VALUE1', 'VALUE2']

for linha in nomes:
    print(array1.count(linha))

In the code you made, you are counting how many times a sub-string appeared in the strings contained in the list array1. In the code I made, I’m counting how many times the string linha appears in the array1.

  • It’s only now that I realize I’ve been given negative for no reason....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.