In: Computer Science
Set the random number Pythonlist to variable x and write the code according to the items below.
(1)Write a code to find the second largest element among List x Element.
(ex.x=[4,4,2,1]->2,x=[5,4,3,1]-4)4)
(2)Write a code to find elements that appear only once in a while.
(ex.x=[4,4,2,1]->[2,1],x=[5,4,3,1]=>[5,4,3,1])
(3)Write a code that generates a list of the frequency of elements for each element of Listx.
(ex.x=[4,4,2,1]->y=[2,2,2,1,1],x=[5,4,3,1]->y=[1,1,1,1])
(4)Write a code to find the value that comes to the center of the list in descending order among the listx members. (*If the number of lists is even, suggest the average value of the two values that come to the center.)
(ex.x=[4,4,2,1]->3=(4+2)/2,x=[5,4,2,1,0]->2)
I am currently using Python and I am a beginner of Python. Tell me the detailed code for the questions (1), (2), (3) and (4).
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. Let me know for any help with any other questions. Thank You! =========================================================================== def main(): # (1)Write a code to find the second largest element among List x Element. x = [4, 4, 2, 1] print('Second Largest Element:', list(set(x))[-2]) #2)Write a code to find elements that appear only once in a while. x = [4, 4, 2, 1] appear_once = [num for num in x if x.count(num)==1] print('Elements that appear once:',appear_once) #3)Write a code that generates a list of the frequency of elements for each element of Listx. x = [4, 4, 2, 1] frequency = [x.count(num) for num in x ] print('Frequency of each element:',frequency) #(4)Write a code to find the value that comes to the center of the list x = [4, 4, 2, 1] median = x[len(x)//2] if len(x)%2==1 else (x[len(x)//2]+x[len(x)//2-1])/2 print('Median:',median) x = [5, 4, 2, 1, 0] median = x[len(x) // 2] if len(x) % 2 == 1 else (x[len(x) // 2] + x[len(x) // 2 - 1]) / 2 print('Median:', median) main()
====================================================================