Removing character in list of strings - python

If I have a list of strings such as:
[("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
What should I do in order to get rid of all the 8s in each string? I tried using strip or replace in a for loop but it doesn't work like it would in a normal string (that not in a list). Does anyone have a suggestion?

Try this:
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print([s.strip('8') for s in lst]) # remove the 8 from the string borders
print([s.replace('8', '') for s in lst]) # remove all the 8s

Beside using loop and for comprehension, you could also use map
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylst = map(lambda each:each.strip("8"), lst)
print mylst

A faster way is to join the list, replace 8 and split the new string:
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylist = ' '.join(mylist).replace('8','').split()
print mylist

mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print mylist
j=0
for i in mylist:
mylist[j]=i.rstrip("8")
j+=1
print mylist

Here's a short one-liner using regular expressions:
print [re.compile(r"8").sub("", m) for m in mylist]
If we separate the regex operations and improve the namings:
pattern = re.compile(r"8") # Create the regular expression to match
res = [pattern.sub("", match) for match in mylist] # Remove match on each element
print res

lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
msg = filter(lambda x : x != "8", lst)
print msg
EDIT:
For anyone who came across this post, just for understanding the above removes any elements from the list which are equal to 8.
Supposing we use the above example the first element ("aaaaa8") would not be equal to 8 and so it would be dropped.
To make this (kinda work?) with how the intent of the question was we could perform something similar to this
msg = filter(lambda x: x != "8", map(lambda y: list(y), lst))
I am not in an interpreter at the moment so of course mileage may vary, we may have to index so we do list(y[0]) would be the only modification to the above for this explanation purposes.
What this does is split each element of list up into an array of characters so ("aaaa8") would become ["a", "a", "a", "a", "8"].
This would result in a data type that looks like this
msg = [["a", "a", "a", "a"], ["b", "b"]...]
So finally to wrap that up we would have to map it to bring them all back into the same type roughly
msg = list(map(lambda q: ''.join(q), filter(lambda x: x != "8", map(lambda y: list(y[0]), lst))))
I would absolutely not recommend it, but if you were really wanting to play with map and filter, that would be how I think you could do it with a single line.

Related

Cut of first and last char of a string in python

I have a list with multiple strings
e.g.:
list = ["This", "is", "a", "test"]
Now I want to create a function that "cuts out" the first and the last char of each string in the given list.
So for this certain example the output of the function should be:
"Tsisaatt"
I have already experimented with list[0][1], but this code:
newList = []
newList = newList.append(list[0][0])
newList = newList.append(satz[0][1])
newList = newList.append(satz[0][2])
...
...
did't do what I want. Did I miss anything or is my idea completely wrong?
Thanks in advance for your feedback!
test.py
list = ["This", "is", "a", "test"]
print("".join([l[0] + l[-1] for l in list]))
Step 1: Just fetch first item with l[0], plus it with last item with l[-1]. It will be ['Ts', 'is', 'aa', 'tt'].
Step 2: Change the new list to string with "".join().
Execution result:
$ python test.py
Tsisaatt
you can do:
newlist.append(mylist[0][0])
newlist.append(mylist[0][-1])
you can write these two lines inside for loop and at last you can join the result
Use list comprehensions
"".join(i[0]+i[-1] for i in list)
You can do it using list comprehension -
output_str = "".join(["".join([x[0], x[-1]]) for x in input_list])
the inner ''.join will join a local list of x[0] and x[-1] and the outer ''.join will join all such list of strings.
You can acces first element of a string using [0] and last element using [-1]
output = ""
for text in text_list:
if text:
output += text[0]+text[-1]
print(output)
You can use this function
def cut_first_and_last(text_list):
output = ""
for text in text_list:
if text:
output += text[0]+text[-1]
return output
or for one-liner
def cut_first_and_last(text_list):
return "".join([text[0]+text[-1] for text in text_list])
If you aren't familiar with list comprehension you can use a simple for loop like this:
mylist = ["this", "is", "a", "list"]
output_string = ""
for item in word_list:
output_string.join(item[0], item[1])
print(output_string)
If you are familiar with list comprehensions just use this one liner:
mylist = ["this", "is", "a", "list"]
print("".join(["".join(item[0],item[1]) for item in mylist])

How to iteratively split a string using backward combinations?

I have a list of strings that look like this:
['C04.123.123.123', 'C03.456.456.456', 'C05.789.789.789']
I'm trying to split each string so I get different backward combinations of splits on the period delimiter. Basically, if I only take the example of the first string, I want to get:
['C04.123.123.123', 'C04.123.123', 'C04.123', 'C04']
How can I achieve this? I've tried looking into itertools.combinations and the standard split features but no luck.
One-line, easy to understand (was less easy to tune :)), using str.rsplit with maxsplit gradually increasing up to the number of dots:
lst = ['C04.123.123.123', 'C03.456.456.456', 'C05.789.789.789']
result = [x.rsplit(".",i)[0] for x in lst for i in range(x.count(".")+1) ]
result:
['C04.123.123.123',
'C04.123.123',
'C04.123',
'C04',
'C03.456.456.456',
'C03.456.456',
'C03.456',
'C03',
'C05.789.789.789',
'C05.789.789',
'C05.789',
'C05']
The only thing that annoys me is that it calls split a lot just to keep the first element. Too bad there isn't a built-in lazy split function we could call next on.
You can use a list comprehension:
d = ['C04.123.123.123', 'C03.456.456.456', 'C05.789.789.789']
new_d = [a+('.' if i else '')+'.'.join(i) for a, *c in map(".".split, d)
for i in [c[:h] for h in range(len(c)+1)][::-1]]
Output:
['C04.123.123.123', 'C04.123.123', 'C04.123', 'C04', 'C03.456.456.456', 'C03.456.456', 'C03.456', 'C03', 'C05.789.789.789', 'C05.789.789', 'C05.789', 'C05']
start_list = ['C04.123.123.123', 'C03.456.456.456', 'C05.789.789.789']
final_list = []
for item in start_list:
broke_up = item.split('.')
temp = []
full_item = []
for sect in broke_up:
temp.append(sect)
full_item.append(".".join(temp))
final_list.extend(full_item)
print(final_list)
Alternatively you can final_list.append(full_item) to keep seperate lists for each string in the original list.
Try this:
list(accumulate(s.split('.'), lambda a, b: a + '.' + b))[::-1]
You can use itertools.accumulate:
from itertools import accumulate
s = 'C04.123.123.123'
# define the incremental step
append = lambda s, e: s + '.' + e
result = list(accumulate(s.split('.'), append))[::-1]

Python: Find all integers inside string and store in list as an integer

Looking for an efficient way to search for all integers in a string and append them to a list. E.g. '(12, 15)' should become [12, 15]. Integers that are greater than 9, should remain joined and not separated when appended to the list.
If there is a way to use built-in functions, lambda or list comprehension, could you share those specifically? Thanks.
What I have so far seems too bloated.
user_input = '(3, 10)' # or '3 10'
def sti(n):
s = ''
l = []
for index, item in enumerate(n):
if item.isdigit():
s += item
if not item.isdigit():
l.append(s)
s = ''
l.append(s)
a = list(filter(None, l)) # remove spaces
a = list(map(lambda x: int(x), a)) # convert to int
return a
print(sti(user_input))
Use regular expressions:
import re
print(list(map(int, re.findall(r'\d+', user_input))))
If
new_string = "lol69on420for666"
then you can do something like,
for letter in new_string:
if letter == "0" or \
letter == "1" or \
...
letter == "9":
append the letter to some list
or
if "6" in new_string:
append "6" to some list
Assuming there are no negative numbers, you could use itertools.groupby together with str.isdecimal:
>>> from operator import itemgetter
>>> from itertools import groupby
>>> list(map(int, map(''.join, map(itemgetter(1), filter(itemgetter(0), groupby('(3, 10)', str.isdecimal))))))
[3, 10]
Pretty much better without importing any package. v is the string
new_list = [int(item) for item in v if item.isdigit()]

How to sort out first row in a list

I would like to sort out the first row of a given list.
I've been already tried to use python "replace" to remove the second row.
But the problem is that the replace function seems not work at all.
Here is the regular expression I used: replace(r'^ //.*$','')
Here is the list:
//SA/... //short_message/Saint/...
//SS-SA/... //long_message/wonder-girl/...
here is the output I am expecting:
//SA/...
//SS-SA/...
l = ["1 12","3 12","2 12"] # space separated
n = [x.split()[0] for x in l]
print sorted(n)

How to get string list index?

I have a list text_lines = ['asdf','kibje','ABC','beea'] and I need to find an index where string ABCappears.
ABC = [s for s in text_lines if "ABC" in s]
ABC is now "ABC".
How to get index?
Greedy (raises exception if not found):
index = next(i for i, s in enumerate(text_lines) if "ABC" in s)
Or, collect all of them:
indices = [i for i, s in enumerate(text_lines) if "ABC" in s]
text_lines = ['asdf','kibje','ABC','beea']
abc_index = text_lines.index('ABC')
if 'ABC' appears only once. the above code works, because index gives the index of first occurrence.
for multiple occurrences you can check wim's answer
Simple python list function.
index = text_lines.index("ABC")
If the string is more complicated, you may need to combine with regex, but for a perfect match this simple solution is best.
Did you mean the index of "ABC" ?
If there is just one "ABC", you can use built-in index() method of list:
text_lines.index("ABC")
Else, if there are more than one "ABC"s, you can use enumerate over the list:
indices = [idx for idx,val in enumerate(text_lines) if val == "ABC"]

Categories

Resources