+ 1

How to make a integer list provided by user on runtime.

How to make a integer list in which elements are given by the user while program is running. Example [1,2] .list like ["1","2" ] is not allowed. I want to make a program which counts no. of list within the biggest list. I want to input list like [1,2,[1,2]] in my function but what i am getting is ['1','2','[1,2]'] .

3rd Oct 2019, 7:35 PM
Ahsan Ali Khan
Ahsan Ali Khan - avatar
5 Respostas
0
User = input("type numbers: ") UserList = [] for i in range(len(User)): try: UserList.append(int(User[i])) except: pass #Even if the user gives any characters within his input they will be filtered out and just integers will be added to UserList. I hope I understood you right
3rd Oct 2019, 8:47 PM
William M
William M - avatar
0
You both give me the half answer of my question .Give me solution of this in my problem in this program #function to count no. of lists inside list def count_lists(l): count=0 for i in l: if type(i) == list: count+=1 return count # list0=input("Enter the list items").split() #not working correctly because its taking inner list as a string # print(f"No. of lists inside your list {list0} is {count_lists(list0)}") a=[1,2,[1,2]] b=[1,2,[1,2],["a",2,3,4.5]] print(f"no.of list in {a} is {count_lists(a)}") print(f"no.of list in {b} is {count_lists(b)}")
4th Oct 2019, 2:51 AM
Ahsan Ali Khan
Ahsan Ali Khan - avatar
0
Ahsan Ali Khan so here you go mylist = [1,2,[1,2],3,[4,5],6,[7,8]] count_list_in_list = 0 for i in mylist: try: if bool(type(i) is list): count_list_in_list += 1 else: pass except: pass print("number of lists in the list", count_list_in_list)
4th Oct 2019, 7:54 AM
William M
William M - avatar
0
I have done that before . I am asking about runtime input.
7th Oct 2019, 1:27 PM
Ahsan Ali Khan
Ahsan Ali Khan - avatar