+ 2
How to make this code?
l=[1,2,3,4] for i in l: x=l.count(i) print(x) ''' i want x to be a list i mean instead of printing each x on a different line i want it to be printed in a list [1,1,1,1]''' #then after i get the list for i in x: if all(i<2): print("none") else: print("anything") #how do i make this fit in the code
15 Antworten
+ 8
Allready a lot of answers. May be you look for something like this:
#l=[1,2,3,4,2,3,1,0,1]
l=[1,2,3,4,5,6,7,0]
res = []
for i in set(l): # it needs a set to iterate because of duplicates:
res.append(l.count(i))
print(res)
if all(j < 2 for j in res):
print('output-1')
else:
print('output-2')
+ 5
I made it using a dictionary, the condition checks and shows "None" if all values in dictionary is less than 2, "Anything" otherwise.
l = [1,2,3,4,1,3]
d = { v : l.count(v) for v in set(l) }
#print(d)
if( all(v < 2 for v in d.values()) ):
print("None")
else:
print("Anything")
+ 3
No need, loop does not make any difference
All I am saying is appending x in any other list like this👇
https://code.sololearn.com/cwTIibcJB9uh/?ref=app
+ 2
As 'i' is never less that 2 so it was returning false so this statement (all(i<2)) was treated as all(false)
That's why it was giving error as bool is not itreatable
+ 2
all() function checks wether all the members of LIST are iterable or not , but 'i' is not list
+ 1
Just make 'x' an empty list and then append it everytime you want add an element in it
+ 1
Oh I forgot to print l2[] instead of x
Well thanks for pointing that out
Now I have updated the code you can have a look at it now
+ 1
Ok what about the bottom part dude
+ 1
Saja Ali now the code is fully finished.
The error was occuring because you were checking is 'i' is iterable or not but as 'i' is not a list so it was giving an erro
+ 1
😁
0
Do I have to use while loop
0
But it still prints
1
1
1
1
Not a list and an error says bool object is not iterable
0
Doesn't if all (i <2) mean if all the elements in the new list smaller than 2 doesn't mean that
- 1
phl 50 65 78652 in all