0

I wish to first loop from the delete list for word "man" then "eater". Compare these words with the strings within the item_list. If matched, then ignore it and if not match then append to "a".

it should ignore the any words that are matched with the "man" and "eater". Somehow, My output should be ignoring the list of strings that contain words "man" and "eater" but somehow it just keep looping for the first and second word in delete. Example, when it loop word for "man" it properly deleted the list of strings that are matched with this word, but then when it loop for word "eater", it started to find again from the item_list(2nd time) and then reappend again a new set of strings. This makes everything becoming very wrong.

My code:

item = []
delete=["man", "eater"]
item_list = ['sharper_task|$none_venue|man', 'sharper_task|man_venue|king', 'sharper_task|king_venue|world', 'sharper_task|world_venue|dont', 'sharper_task|を_venue|eater', 'sharper_task|eater_venue|todo', 'sharper_task|todo_venue|,']


a = [[] for i in range(len(item_list))]
if delete is not None:
    for dele in delete:
        for ii, f in enumerate(item_list):
            if dele not in f:
                a[ii].append(f)
else:
    a = [[nn] for nn in item_list]
item.append(a)

print(item)

Current output:

[[['sharper_task|$none_venue|man'], ['sharper_task|man_venue|king'], ['sharper_task|king_venue|world', 'sharper_task|king_venue|world'], ['sharper_task|world_venue|dont', 'sharper_task|world_venue|dont'], ['sharper_task|を_venue|eater'], ['sharper_task|eater_venue|todo'], ['sharper_task|todo_venue|,', 'sharper_task|todo_venue|,']]]

Expected output:

['sharper_task|king_venue|world', 'sharper_task|world_venue|dont', 'sharper_task|todo_venue|,']

I think this might be tough but if possible, I wish to stick with a shorter codes and not to include too many for loops here. I am still learning and I found out that I used too many for loops when iterating a list.

2 Answers 2

1

You could iterate through item_list and get elements that does not contain any of the elements in delete list:

delete = ["man", "eater"]
item_list = ['sharper_task|$none_venue|man', 'sharper_task|man_venue|king', 'sharper_task|king_venue|world', 'sharper_task|world_venue|dont', 'sharper_task|を_venue|eater', 'sharper_task|eater_venue|todo', 'sharper_task|todo_venue|,']

print([x for x in item_list if not any(y in x for y in delete)])
# ['sharper_task|king_venue|world', 'sharper_task|world_venue|dont', 'sharper_task|todo_venue|,']

I'm not sure why you need an output that's repeating, but then you could do:

print([[x, x] for x in item_list if not any(y in x for y in delete)])

which gives you the expected one.

any(...) returns a True if any of the condition that we check within it becomes a truthy else returns a False. Here, we need to return a True when any of the elements in delete is in item_list.

Non list-comprehension version:

lst = []
for x in item_list:
    if not any(y in x for y in delete):
        lst.append([x, x])

print(lst)
Sign up to request clarification or add additional context in comments.

6 Comments

This is my idea too, but somewhat does not match OP's desired output
@U9-Forward, I think that's a mistake from OP's part.
Hi sir, could you tell me how can I make it the same dimension as the previous output? Somehow here it became single list. Is it possible not to use print() directly, Sorry i am still learning.
Do you need to repeat the same item twice like in the expected output given in the question? I assumed you need a single list with no repetition.
Now I understand, it works like a magic. Could you please share me what is the not any do? If possible, can you write not in list comprehensions way? Im sorry, for beginner like me, its hard to decipher the list comprehensions.
|
1

Unsure exactly, but if want to only check if there something continuously going in one split of '|'(so man|blah|foo yes, and man_blah|foo|bar no):

print([i for i in item_list if not any(x in i.split('|') for x in delete)])

For exact output of yours:

l=[[i]*2 for i in item_list if not any(x in i for x in delete)]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.