Home > Net >  Nested list,what's list sizes varies Python
Nested list,what's list sizes varies Python

Time:12-22

Anyone could help me with this problem?(python) :
I have a nested list what's list's size is varies for example: list=[[1,2],[3,4,5],[6,7,8]] and I want to remove the element for example 4,then I want to store the list that contains 4 so i want to store 3,4,5 , then i want to remove this list from my nested list so it would look like: list=[[1,2],[6,7,8]] and if any elements in my new list(retuuurnnlist) equals with any element in my text,throw back my boolean variable as true


text="asd 1 4,5 32fas dst sf"
givennumber=
asd=False
list=[[1,2],[3,4,5],[6,7,8]]
for x in range(len(list)):                           
    for y in range(len(list[x])):                    
        if givennumber == y:                                   
            retuuurnnlist.clear()                            
            retuuurnnlist=x.copy()      
            list.remove(list[x][y])          
            if any(element in text for element in list):  
                asd = True                 

what I want to get:
retuuurnnlist prints [3,4,5]
list prints [1,2],[6,7,8]
asd variable prints true if any elements in my new list(retuuurnnlist) equals with any element in my text,throw back my boolean variable as true

CodePudding user response:

Below code will return the output as expected

list=[[1,2],[3,4,5],[6,7,8]]
n = 4
asd = False
text="asd 1 4,5 32fas dst sf"
returnList=[]
for i in list:
    if n in i:
        returnList.append(i)
        list.remove(i)
flat_list = [i for sublist in list for i in sublist]
if any(str(x) in text for x in flat_list):
    asd = True
print(list)
print(returnList)
print(asd)
  • Related