-5
I am a beginner in Python and I would like to count on your help for the problems below. I have worked on two lists as the following objectives: 1.Check which items in List2 are in List1 2.Count each List2 item in List1 and sort it in Orderly List2.
Lista1 = ['a', 'k', 'j', 'd', 'p', 'u', 'j', 'h', 'k', 'k', 'c', 'p', 'e', 'u', 'z', 'z', 'f', 's', 'u', 'k', 'p', 'u', 'j', 'h', 'b', 'k', 'j', 'd', 'e', 'u', 'j', 'z']
Lista2 = ['j', 'u', 'z', 'k', 'u', 'p']
Lista_ordenada = [ ]
for i in Lista1:
if i == 'j' :
break
Lista_ordenada.append('j')
.
.
.
for i in Lista1:
if i == 'p' :
break
Lista_ordenada.append('p')
Of problems:
- Although the code works, I see that it is very repetitive and slow;
- Although I managed to put them in the ordered list (in a disorderly way), I could not do the count, nor order them.
Any help will be welcome.
It sounds interesting your exposure, but that’s not exactly it. Correction: In Lista2 there is only one 'u'. In the second problem, I ask to be seen the count (occurrences of elements) of List2 in List1. Ex: 'j' occurs 5 times in List1. Then 'j' must be inserted in a new List in order from > to <, for example: Sorted list = ['j', 'k'...] If the solution comes in the form of a better function, because if List2 has fewer elements, these will be exposed in a new order.
– Oberdan Santos
@Oberdansantos Look your question is written
Lista2 = ['j', 'u', 'z', 'k', 'u', 'p']
in your question the elementu
is repeated twice,– Augusto Vasques
@Oberdansantos, the question is written Count each List2 item in List1 and sort it in Orderly List2. does not speak at any time that it is to order based on its frequency.
– Augusto Vasques
@Oberdansantos I can only answer what was asked, I can’t guess your intentions and objectives if you don’t express them.
– Augusto Vasques
I apologize for not expressing myself correctly. I know that we need a logic of the problem to solve it. I believe it has now become clear and that you can help me.
– Oberdan Santos
@Oberdansantos Not my time is expensive and this is pro bono. I answered what was written if not written correctly open another question for someone else to answer it.
– Augusto Vasques