S
S
Svoboo2022-02-18 14:24:16
Python
Svoboo, 2022-02-18 14:24:16

How to check if a string exists in an array in python?

There is a file with lines: If I do a check: then the match is only on the last element of the array. As I understand it, there is an exact check, and not a check for availability. All elements of the array except the last one contain line breaks, which is why it happens. What is the easiest way to deal with such a situation? In php, I just trimmed the elements of the array:
blacklist = list(open('blacklist.txt'))
myline not in blacklist
array_map('trim', file('blacklist.txt'));

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LXSTVAYNE, 2022-02-18
@Svoboo

You are looking for a match of your line in the list, and you, as I understand it, need to be in the lines from this list. One solution:

arr = ['hello', 'world', 'nord']
any(filter(lambda s: 'ord' not in s, arr))

V
Vindicar, 2022-02-18
@Vindicar

So why not look up how to trim spaces and how to apply a function to all elements of an array in python? There are equivalents to both array_map() and trim() .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question