Posts: 27
Threads: 11
Joined: Mar 2020
hi guys , i want to print same element in each item in a list,
my code returns nothing except if i print list2,
it should return 3 elements in first item and 2 elements in a second one
list1=[10,20,30,40]
list2 =[[10,20,30,1],[10,20,1,2]]
#print(list2)
for i in list2:
num= i.count(list1)
if (num > 2):
print(num)
Posts: 741
Threads: 122
Joined: Dec 2017
I suspect that you will get a result if you do:
print(len(list1))
print(len(list2))
Although i see 4 elements in list1.
Count is not necessary.
Paul
Posts: 353
Threads: 13
Joined: Mar 2020
May-01-2020, 10:34 AM
(This post was last modified: May-01-2020, 10:48 AM by pyzyx3qwerty.)
If you want to print the same element in both lists, you can do:
for i in list1 :
if i in list2 :
#your code
Posts: 27
Threads: 11
Joined: Mar 2020
Thanks for your response,len() just return number of item in list2 which is 2, i want if there is/are same element on each item from list2 will be printed, using list1 as comparison
Posts: 94
Threads: 0
Joined: Apr 2020
May-01-2020, 10:39 AM
(This post was last modified: May-01-2020, 10:40 AM by anbu23.)
>>> list1=[10,20,30,40]
>>> list2 =[[10,20,30,1],[10,20,1,2]]
>>> for lst in list2:
... len([value for value in lst if value in list1])
...
3
2
Posts: 27
Threads: 11
Joined: Mar 2020
(May-01-2020, 10:34 AM)pyzyx3qwerty Wrote: If you want to print the same element in both lists, you can do:
for i in list1 :
if i in list2 :
#your code it returns nothing since list2 has no same item from list1 it has only 3 same elements that which i want to be printed
Posts: 353
Threads: 13
Joined: Mar 2020
(May-01-2020, 10:45 AM)glennford49 Wrote: (May-01-2020, 10:34 AM)pyzyx3qwerty Wrote: If you want to print the same element in both lists, you can do:
for i in list1 :
if i in list2 :
#your code it returns nothing since list2 has no same item from list1 it has only 3 same elements that which i want to be printed
I know, as i misread your thread as print the elements, not print the number of elements
Posts: 27
Threads: 11
Joined: Mar 2020
May-01-2020, 10:50 AM
(This post was last modified: May-01-2020, 10:54 AM by glennford49.)
(May-01-2020, 10:47 AM)pyzyx3qwerty Wrote: (May-01-2020, 10:45 AM)glennford49 Wrote: it returns nothing since list2 has no same item from list1 it has only 3 same elements that which i want to be printed
I know, as i misread your thread as print the elements, not print the number of elements
its just fine, it just happen that i can hardly fix my code, thanks for your response
(May-01-2020, 10:39 AM)anbu23 Wrote: >>> list1=[10,20,30,40]
>>> list2 =[[10,20,30,1],[10,20,1,2]]
>>> for lst in list2:
... len([value for value in lst if value in list1])
...
3
2
how to return the exact same element found in list2?
Posts: 94
Threads: 0
Joined: Apr 2020
(May-01-2020, 10:50 AM)glennford49 Wrote: how to return the exact same element found in list2?
Remove len()
Posts: 27
Threads: 11
Joined: Mar 2020
(May-01-2020, 11:09 AM)anbu23 Wrote: (May-01-2020, 10:50 AM)glennford49 Wrote: how to return the exact same element found in list2?
Remove len()
thumbs up, this saves my day!
|