How to effectively remove list items in a loop in python

I have the code as follows.

for item in my_list:
        print(item[0])
        temp = []
        current_index = my_list.index(item)
        garbage_list = creategarbageterms(item[0])

        for ele in my_list:
            if my_list.index(ele) != current_index:
                for garbage_word in garbage_list:
                    if garbage_word in ele:
                        print("concepts: ", item, ele)
                        temp.append(ele)
        print(temp)

Now I want to remove elefrom mylistwhen it is added to temp(so that it will not be processed in the main loop, since this is a garbage word).

I know that it is bad to remove items from a list when it is in a loop. So I am wondering if there is an effective way to do this?

For example, if mylistit looks like this:

    mylist = [["tim_tam", 879.3000000000001], ["yummy_tim_tam", 315.0], ["pudding", 298.2], 
["chocolate_pudding", 218.4], ["biscuits", 178.20000000000002], ["berry_tim_tam", 171.9], 
["tiramusu", 158.4], ["ice_cream", 141.6], ["vanilla_ice_cream", 122.39999999999999]]

1st iteration

for the first element tim_tam, I get garbage words like yummy_tim_tamand berry_tim_tam. Therefore, they will be added to my list temp.

yummy_tim_tam berry_tim_tam ( temp), .

, yummy_tim_tam , pudding. pudding , chocolate_pudding, biscuits, tiramu. , temp .

3-

ice_cream. .

, .

["tim_tam", 879.3000000000001], ["yummy_tim_tam", 315.0], ["berry_tim_tam", 171.9] , ["pudding", 298.2]

["chocolate_pudding", 218.4], ["biscuits", 178.20000000000002], ["tiramusu", 158.4]

["ice_cream", 141.6], ["vanilla_ice_cream", 122.39999999999999]
+4
3

, :

my_list = [['tim_tam', 879.3], ['yummy_tim_tam', 315.0], ['pudding', 298.2],
           ['chocolate_pudding', 218.4], ['biscuits', 178.2], ['berry_tim_tam', 171.9],
           ['tiramusu', 158.4], ['ice_cream', 141.6], ['vanilla_ice_cream', 122.39]
           ]

creategarbageterms = {'tim_tam' : ['tim_tam','yummy_tim_tam', 'berry_tim_tam'],
                      'pudding': ['pudding', 'chocolate_pudding', 'biscuits', 'tiramusu'],
                      'ice_cream': ['ice_cream', 'vanilla_ice_cream']}

all_data = {}
temp = []
for idx1, item in enumerate(my_list):
    if item[0] in temp: continue
    all_data[idx1] = [item]

    garbage_list = creategarbageterms[item[0]]

    for idx2, ele in enumerate(my_list):
        if idx1 != idx2:
            for garbage_word in garbage_list:
                if garbage_word in ele:
                    temp.append(ele[0])
                    all_data[idx1].append(ele)

for item in all_data.values():
    print('-', item)  

:

- [['tim_tam', 879.3], ['yummy_tim_tam', 315.0], ['berry_tim_tam', 171.9]]
- [['pudding', 298.2], ['chocolate_pudding', 218.4], ['biscuits', 178.2], ['tiramusu', 158.4]]
- [['ice_cream', 141.6], ['vanilla_ice_cream', 122.39]]  

, moker creategarbageterms ( ), , . defaultdict, , .

+3

:

mylist = [["tim_tam", 879.3000000000001],   
          ["yummy_tim_tam", 315.0],
          ["pudding", 298.2], 
          ["chocolate_pudding", 218.4], 
          ["biscuits", 178.20000000000002],
          ["berry_tim_tam", 171.9], 
          ["tiramusu", 158.4], 
          ["ice_cream", 141.6], 
          ["vanilla_ice_cream", 122.39999999999999]]

d = set()   # remembers unique keys, first one in wins

for i in mylist:
    shouldAdd = True
    for key in d:
        if i[0].find(key) != -1:    # if this key is part of any key in the set
            shouldAdd = False       # do not add it

    if not d or shouldAdd:          # empty set or unique: add to set
        d.add(i[0]) 

myCleanList = [x for x in mylist if x[0] in d]    # clean list to use only keys in set

print(myCleanList)

:

[['tim_tam', 879.3000000000001], 
 ['pudding', 298.2], 
 ['biscuits', 178.20000000000002], 
 ['tiramusu', 158.4], 
 ['ice_cream', 141.6]]

, - dict.

, :

similarThings = [ [x for x in mylist if x[0].find(y) != -1] for y in d]

print(similarThings)

:

[
    [['tim_tam', 879.3000000000001], ['yummy_tim_tam', 315.0], ['berry_tim_tam', 171.9]], 
    [['tiramusu', 158.4]], 
    [['ice_cream', 141.6], ['vanilla_ice_cream', 122.39999999999999]], 
    [['pudding', 298.2], ['chocolate_pudding', 218.4]], 
    [['biscuits', 178.20000000000002]]
]

@joaquin , creategarbageterms(), tiramusu biscuits pudding, 100% - " interations, dict, . - , .

+2

, , , .

, , , , temp. , .

, , . , , , , . - , :

idx = 0
while idx < len(my_list)
    item = my_list[idx]
    print(item[0])
    temp = []
    garbage_list = creategarbageterms(item[0])

    ele_idx = 0
    while ele_idx < len(my_list):
        if ele_idx != idx:
            ele = my_list[ele_idx]
            for garbage_word in garbage_list:
                if garbage_word in ele:
                    print("concepts: ", item, ele)
                    temp.append(ele)
                    del my_list[ele_idx]
        ele_idx += 1
    print(temp)
    idx += 1

The key concept here is that, using a loop whileinstead of a loop for, you can take more detailed "manual" control of the program control flow and more safely perform "" unconventional "things in your loop. I would recommend doing this if you really This reason is closer to the literal question you asked and closer to your source code, but perhaps not the easiest to read / Pythonic code itself.

+1
source

Source: https://habr.com/ru/post/1692161/


All Articles