Just use str.translate():
In [4]: 'abcdefabcd'.translate(None, 'acd')
Out[4]: 'befb'
From the documentation:
string.translate(s, table[, deletechars])Delete all characters from
sthat are indeletechars(if present), and then translate the characters usingtable, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. Iftableis None, then only the character deletion step is performed.
If -- for educational purposes -- you'd like to code it up yourself, you could use something like:
''.join(c for c in str1 if c not in str2)
Answer from NPE on Stack Overflowpython - Filtering Characters from a String - Stack Overflow
Trying to create a function that will filter out strings in my list
python - Filtering a list of strings based on contents - Stack Overflow
Python filter function in for loop - Stack Overflow
Just use str.translate():
In [4]: 'abcdefabcd'.translate(None, 'acd')
Out[4]: 'befb'
From the documentation:
string.translate(s, table[, deletechars])Delete all characters from
sthat are indeletechars(if present), and then translate the characters usingtable, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. Iftableis None, then only the character deletion step is performed.
If -- for educational purposes -- you'd like to code it up yourself, you could use something like:
''.join(c for c in str1 if c not in str2)
Use replace:
def filter_string(str1, str2):
for c in str2:
str1 = str1.replace(c, '')
return str1
Or a simple list comprehension:
''.join(c for c in str1 if c not in str2)
Is there a way I can loop through a list and filter out the strings in the list? I have the below so far
new_list = []
def num_int(str_list):
for i in str_list:
#I would want to loop though my list and filter out the strings. Can I do an if statement based on a datatype check?This simple filtering can be achieved in many ways with Python. The best approach is to use "list comprehensions" as follows:
>>> lst = ['a', 'ab', 'abc', 'bac']
>>> [k for k in lst if 'ab' in k]
['ab', 'abc']
Another way is to use the filter function. In Python 2:
>>> filter(lambda k: 'ab' in k, lst)
['ab', 'abc']
In Python 3, it returns an iterator instead of a list, but you can cast it:
>>> list(filter(lambda k: 'ab' in k, lst))
['ab', 'abc']
Though it's better practice to use a comprehension.
[x for x in L if 'ab' in x]
filter returns a generator, which is why you only obtain a list after passing the generator to list(), which takes all the elements generated and returns them in a list.
A way to get what you want without filter() and using for:
nums = list(range(1, 15))
result = [x for x in nums for i n range(2, 5) if x % i == 0]
This is called a list comprehension and it's very efficient and readable way of constructing a list like this.
Filter is generator. Therefore it uses lazy evaluation of expression. From documentation:
Variables used in the generator expression are evaluated lazily when the
__next__()method is called for the generator object (in the same fashion as normal generators).
It means that lambda expression is evaluated when you call list(nums) because it calls __next__() method under the hood.
So in your second example it will (I guess) filter 3 times always with divider 4:
nums = filter(lambda x: x % 4 == 0)
nums = filter(lambda x: x % 4 == 0)
nums = filter(lambda x: x % 4 == 0)
Maybe that piece of code gives you better understanding. Notice that expression is evaluated when list() is called. As you can see, loop here doesn't change the result. Using variable i makes the difference:
nums = list(range(1, 15))
i = 2
nums = filter(lambda x: x % i == 0, nums)
i = 3
nums = filter(lambda x: x % i == 0, nums)
i = 4
nums = filter(lambda x: x % i == 0, nums)
print(list(nums)) # here i==4
### [4, 8, 12]
nums = list(range(1, 50))
for i in range(2, 5):
nums = filter(lambda x: x % i == 0, nums)
i = 11
print(list(nums)) # here i==11
### [11, 22, 33, 44]
One more solution:
def f(x):
for i in range(2, 5):
if x % i != 0:
return False
return True
nums = list(range(1, 15))
nums = filter(f, nums)
print(list(nums))
You could use any to iterate through the keywords
SSID = ['Test SSID' , 'Public' , '....etc...']
with open('/home/pi/unFilter.csv', 'r') as in_file:
for line in infile:
if any(item in line for item in SSID):
with open("/home/pi/dataLog.csv", "a") as finalFile:
finalFile.write(str(line))
One solution is to use any (as described in another answer), but in case, you are able to parse
the name of SSID from your line, you can use more efficient solution just testing membership of
found SSID name in list of names of interest.
Following code uses get_ssid function, which attempts to get the SSID name from line.
Having SSID name, the test is much simpler and faster.
def ssidFilter(ssids_to_log, input_csv, output_csv):
def get_ssid(line):
# parse SSID from the line, return it
# Here I assume it is first part of the line, delimited by ;
return line.split(";", 1)[0]
with open(input_csv) as in_f:
with open(output_csv, "a") as out_f:
for line in in_f:
if get_ssid(line) in ssids_to_log:
out_f.write(str(line))
if __name__ == "__main__":
ssids_to_log = ["Test SSID", "Public", "HomeNet", "Network 1",
"LimeOak", "BlackCrow", "GuestWiFi"]
input_csv = "/home/pi/unFilter.csv"
output_csv = "/home/pi/dataLog.csv"
ssidFilter(ssids_to_log, input_csv, output_csv)
Check out pandas.Series.str.contains, which you can use as follows.
df[~df.tweets.str.contains('filter_word')]
MWE
In [0]: df = pd.DataFrame(
[[1, "abc"],
[2, "bce"]],
columns=["number", "string"]
)
In [1]: df
Out[1]:
number string
0 1 abc
1 2 bce
In [2]: df[~df.string.str.contains("ab")]
Out[2]:
number string
1 2 bce
Timing
Ran a small timing test on the following synthetic DataFrame with three million random strings the size of a tweet
df = pd.DataFrame(
[
"".join(random.choices(string.ascii_lowercase, k=280))
for _ in range(3000000)
],
columns=["strings"],
)
and the keyword abc, comparing the original solution, map + regex and this proposed solution (str.contains). The results are as follows.
original 99s
map + regex 21s
str.contains 2.8s
I create the following example:
df = pd.DataFrame("""Suggested order for Amazon Prime Doctor Who series
Why did pressing the joystick button spit out keypresses?
Why tighten down in a criss-cross pattern?
What exactly is the 'online' in OLAP and OLTP?
How is hair tissue mineral analysis performed?
Understanding the reasoning of the woman who agreed with King Solomon to "cut the baby in half"
Can Ogre clerics use Purify Food and Drink on humanoid characters?
Heavily limited premature compiler translates text into excecutable python code
How many children?
Why are < or > required to use /dev/tcp
Hot coffee brewing solutions for deep woods camping
Minor traveling without parents from USA to Sweden
Non-flat partitions of a set
Are springs compressed by energy, or by momentum?
What is "industrial ethernet"?
What does the hyphen "-" mean in "tar xzf -"?
How long would it take to cross the Channel in 1890's?
Why do all the teams that I have worked with always finish a sprint without completion of all the stories?
Is it illegal to withhold someone's passport and green card in California?
When to remove insignificant variables?
Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?
Cut the gold chain
Why do some professors with PhDs leave their professorships to teach high school?
"How can you guarantee that you won't change/quit job after just couple of months?" How to respond?""".split('\n'), columns = ['Sentence'])
You can juste create a simple function with regular expression (more flexible in case of capital characters):
def tweetsFilter(s, keyword):
return bool(re.match('(?i).*(' + keyword + ').*', s))
This function can be called to obtain the boolean series of strings which contains the specific keywords. The mapcan speed up your script (you need to test!!!):
keyword = 'Why'
sel = df.Sentence.map(lambda x: tweetsFilter(x, keyword))
df[sel]
And we obtained:
Sentence
1 Why did pressing the joystick button spit out ...
2 Why tighten down in a criss-cross pattern?
9 Why are < or > required to use /dev/tcp
17 Why do all the teams that I have worked with a...
20 Why does Linux list NVMe drives as /dev/nvme0 ...
22 Why do some professors with PhDs leave their p...