Question

In: Computer Science

Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...

Python Question

Using lists, write the function non_unique(list) that takes a list list as argument. It
returns a list which duplicated elements remains and each duplicated element is followed by
a number which shows how many times it appears. All elements in return list should be in
the same order as their appearance in the original list.
For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function
would return [‘a’, 3, ‘b’, 2]. Another example, ['abc', 'def', 'abc', 'xyz', 'def','def', 'qwe'] -> ['abc', 2, 'def', 3]


If no such non_unique list exist, just return an empty list.
Your program should contain the function with format shown as below:

def non_unique(list):
# Your codes here
return result # ‘result’ is a list.

Solutions

Expert Solution

#code

def non_unique(mylist):
    newelements = dict()
    # Iterate over each element in list
    for element in mylist:
        # If element exists in dict then increment its value else add it in dict
        if element in newelements:
            newelements[element] += 1
        else:
            newelements[element] = 1    
 
    # Filter key-value pairs in dictionary. Keep pairs whose value is greater than 1 i.e. only duplicate elements from list.
    newelements = { key:value for key, value in newelements.items() if value > 1}
    #convert dictionary to list 
    newlist=list(newelements.items())
    # Returns a list of duplicate elements and thier frequency count
    return newlist



# List of strings
mylist = ['a','b','c','a','b','d','a','e']
# Get a list containing duplicate elements in given list and their frequency count
newlist = non_unique(mylist)   
#result
print(newlist)

co#screenshot of code and output

output 2:

--------------------------------------------------------------


Related Solutions

Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0'...
In Python, write a function one_bit_NOT that takes one argument, a character that is either '0' or '1'. It should perform the NOT operation and return a string with a single character as the result. I.e., if the character argument is "0", it returns a "1"'. If the character argument is "1", it returns a "0".
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and...
In Python, Q. Write a function max_increase(seq) which takes as argument a sequence of numbers and returns the maximum increase from one element in the sequence to an element at a higher index. Assumptions and restrictions: The function must return a number. If there is no increasing pair in the sequence, the function should return 0. This may happen for example if the sequence is decreasing, or if it contains fewer than 2 elements. You can assume that the argument...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT