0
votes

I have three lists, let's call them List1, List2, and List3.

Lists 1 and 2 are received by the function as arguments, and List3 is created in the function.

I'm trying to take List1 and count how many times each unique element within it appears, and display those numbers next to a list of only those unique elements, without duplicates, in the order that the first instance of that element appeared in List1.

The breakdown: List1 contains many elements, with some duplicates. List2 contains a unique set of elements from List1, in the order they appeared first in List1. List3 contains the number of times each unique element in List2 appeared in List1, next to the unique element the number corresponds to in List2.

Below is the way I tried to accomplish this, but I'm missing something. I may just be off my rocker completely on the coding. Please let me know how I can accomplish this task. Thank you!

def successfulTrials(List1, List2):

    List3 =[y]

    for x in List1:

        y = List1.count(i) for i in List2

            for z in List3:

                z.extend([i])

                print(z)

People have asked for an example of the lists: List1 = ['Jan', 'Feb', 'Aug', 'Mar', 'Jan', 'Apr', 'May', 'Sep', 'Jun', 'Jan', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Jan', 'Dec']

List2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

A display of List3 would look like: 4 Jan 1 Feb 2 Aug 1 Mar 1 Apr 1 May 2 Sep 1 Jun 1 Jul 1 Oct 1 Nov 1 Dec

5
Could you give an example of List1, List2, and a resulting List3?pjvandehaar
Please dont use the word List.user2459905
you got any error,as you function has syntax errorsHackaholic
can you give expected input and expected output??Hackaholic
I gave an example of expected input and expected output, but it won't let me orient the output of List3 vertically, as I would like it to. Just for your information :)Zach

5 Answers

0
votes

I suggest you to use:

  • list type for List1;
  • set type for List2 (it stores only unique values);
  • dictionary type for List3 (you'll have pair of all values and their counts).

So after getting List1 and List2, your code for succesfulTrials will look like this:

def succesfulTrials(List1, List2):
    List3 = {}
    # Grab data
    for item in List2:
        List3[item] = List1.count(item)

    # Print data
    for (key, value) in List3.items():
        print value, key,
1
votes
List1 = ['Jan', 'Feb', 'Aug', 'Mar', 'Jan', 'Apr', 'May', 'Sep', 'Jun', 'Jan', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Jan', 'Dec']
List2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

def successfulTrials(List1, List2):
    List3 = []
    for x in List2:
        List3.extend([List1.count(x),x])    

Result:

 [4, 'Jan', 1, 'Feb', 1, 'Mar', 1, 'Apr', 1, 'May', 1, 'Jun', 1, 'Jul', 2, 'Aug', 2, 'Sep', 1, 'Oct', 1, 'Nov', 1, 'Dec']
1
votes

using dictionary will be better here:

>>> List1 = ['Jan', 'Feb', 'Aug', 'Mar', 'Jan', 'Apr', 'May', 'Sep', 'Jun', 'Jan', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Jan', 'Dec']
>>> List2 = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
>>> my_dict = { x:List1.count(x) for x in List2 }
>>> my_dict
{'Mar': 1, 'Feb': 1, 'Aug': 2, 'Sep': 2, 'Apr': 1, 'Jun': 1, 'Jul': 1, 'Jan': 4, 'May': 1, 'Nov': 1, 'Dec': 1, 'Oct': 1}

but if you want list:

>>> List3 = [ [x,List1.count(x)] for x in List2 ]
>>> List3
[['Jan', 4], ['Feb', 1], ['Mar', 1], ['Apr', 1], ['May', 1], ['Jun', 1], ['Jul', 1], ['Aug', 2], ['Sep', 2], ['Oct', 1], ['Nov', 1], ['Dec', 1]]

if you want to use extend:

>>> List3 = []
>>> for x in List2:
...     List3.extend([" ".join([x,str(List1.count(x))])])
... 
>>> List3
['Jan 4', 'Feb 1', 'Mar 1', 'Apr 1', 'May 1', 'Jun 1', 'Jul 1', 'Aug 2', 'Sep 2', 'Oct 1', 'Nov 1', 'Dec 1']
0
votes
list2=[]
list3={}
for a in List1:
    if a in list2:
        list3[a]+=1
    else:
        list2.append(a)
        list3[a]=1
0
votes

For counting duplicates, this can be useful, check it out.

import collections

List1 = ['Jan', 'Feb', 'Aug', 'Mar', 'Jan', 'Apr', 'May', 'Sep', 'Jun', 'Jan', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Jan', 'Dec']

List3 = collections.Counter(List1)

print(List3)

Output

Counter({'Jan': 4, 'Aug': 2, 'Sep': 2, 'Mar': 1, 'Feb': 1, 'Apr': 1, 'Jun': 1, 'Jul': 1, 'May': 1, 'Nov': 1, 'Dec': 1, 'Oct': 1})