Function to check the number of Python list factors...

Asked 2 years ago, Updated 2 years ago, 15 views

I made a list of numbers=[1,2,3,4,5,6,1,2,3,4,5,2]

a1=numbers.count(1)

a2=numbers.count(2)

I want to make up to a6 in this way, but is there a way to express this as a function?

python

2022-09-22 20:32

3 Answers

There is a module that can be used comfortably.

It's an itertools module.

You can do it comfortably by using itertools' groupby.

import itertools

numbers=[1,2,3,4,5,6,1,2,3,4,5,2]
for k, v in itertools.groupby(sorted(numbers)):
    print(list(v))

[1, 1]
[2, 2, 2]
[3, 3]
[4, 4]
[5, 5]
[6]

The role of groupby is grouped like 112233 when [1,1,2,2,3,3] exists.

Sort it out and sort it from 1 to 6. [1, 2, 2, 2, 3, 4, 4, 5, 6] Make it like [1, 2, 2, 3, 4, 5, 6], and if you group by, the same things are tied together.


2022-09-22 20:32

Well, I'm going to focus on allocating variables. First of all, we need to discuss variable assignment, and computer programming language is actually a program. It's a program that works as a compiler? The reason why I can't do that is because it's a reason for the compiler and also because it deviates from what the programming language is aiming for.

for i in range (0,3):
    ai = i

I'll use it as a straightforward example. For us to understand intuitively, if we do it like this,

a0 = 0
a1 = 1
a2 = 2
a3 = 3

It looks like we'll be together, but it doesn't. Because the above code is dual, there is a problem with the compiler's interpretation. Are you asking me to assign a value to a variable named ai? Are you asking me to assign a value to a variable named a1, a2, and a3 or it can be interpreted as two, right? Most compiler languages will probably be interpreted electronically. I don't like it, author! I'm going to do the latter! You could say that. Or, to the compiler, this is how you interpret it. That's what you might say. But you don't have to. There's another way. You don't have to, but do you have to?

a = []
for i in range (0,6):
    a[i] = i

I think this is a wise way to do it.


2022-09-22 20:32

#target is what we need to compare for each data in the list
#data is a list
def listCount(target, data):
    count = 0

    for i in range(len(data)):
        if target == data[i]: # If the i-th in the data list is the same as the target,
            count = count + 1

    print(count)
    return count

numbers = [1, 2, 3, 4, 5, 2, 1, 1]

a1 = listCount(1, numbers) # The result is 3
a2 = listCount(2, numbers) # The result is 2```

From what I understand, make a list,

Set a value to compare

Check the target (value to compare) for each index

If yes, add 1 and if not, then no.

So, how many values corresponding to the target are in this list?

Is it right to ask this?

I made the code for now code.

If what I understand is wrong, please write it in the comments or something.

The code is located at https://github.com/proogrammer/MyAnswers/blob/master/python/listCount.py.

:)


2022-09-22 20:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.