How to remove first RemoveThisX\n from list
['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
Trying to remove RemoveThis1\n, RemoveThis2\n, RemoveThis3, RemoveThis4\n
Final result need to be
['DontRemove\nDontRemove','DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
a_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
result = [item[item.find('\n')+1:] for item in a_list]
print(result)
['DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
test_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
result = ["\n".join(item.split("\n")[1:]) for item in test_list]
print(result)
Output will be:
['DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove', 'DontRemove\nDontRemove']
assuming:
initial_list = ['RemoveThis1\nDontRemove\nDontRemove','RemoveThis2\nDontRemove\nDontRemove', 'RemoveThis3\nDontRemove\nDontRemove', 'RemoveThis4\nDontRemove\nDontRemove']
I would recommend using either the map function:
mapped_list = list(map(lambda x: x[x.find('\n') + 1:], initial_list))
or list comprehension:
comprehended_list = [string[string.find('\n') + 1:] for string in initial_list]
Both should produce the asked list.
Related
I was able to do it by looping accessing list[i][j]. but wanted to do it without looping.
Any ideas how to do it
Example list :
Input: ["abc","def","ghi"]
Output: ["$bc_","$ef_","$hi_"]
Use a list comprehension and concatenate or format the strings you want.
inlist = ['abc','def','ghi']
outlist = [f'${s[1:]}_' for s in inlist]
You can use list comprehension:
lstIn = ['abc', 'def', 'ghi']
lstOut = [f'${i[1:]}_' for i in lstIn]
print(lstOut)
Prints:
['$bc_', '$ef_', '$hi_']
Try this
lst = ["abc","def","ghi"]
out = [ "".join(("$",s[1:],"_")) for s in lst ]
Output: print(out)
['$bc_', '$ef_', '$hi_']
I have a list:
List = [('4022-a751',), ('0bfc-4d53',)]
And want to resolve it into the output below:
Output = ['4022-a751','0bfc-4d53']
You should read about List Comprehensions in Python
list_ = [('4022-a751',), ('0bfc-4d53',)]
res = [x for item in list_ for x in item]
Output
['4022-a751', '0bfc-4d53']
A tuple can be manipulated like an array with index.
input_arr = [('4022-a751',), ('0bfc-4d53',)]
output_arr = [a[0] for a in input_arr]
print(output_arr)
You can use this.
old_list= [('4022-a751',), ('0bfc-4d53',)]
new_list = [''.join(i) for i in old_list]
print(new_list)
I want to add a value to a list of lists.
For input of [[1,2],[2,3]]
I want output of [[2,3],[3,4]]
I can do it with loops:
list_of_lists = [[1,2],[2,3]]
output = []
for list in list_of_lists:
sub_output = []
for value in list:
sub_output.append(value+1)
output.append(sub_output)
print(output)
Can I do this with list comprehension?
If I do:
[value + 1 for list in list_of_lists for value in list]
It gives me [2,3,3,4]. Can I get it to keep the sublist format somehow?
Try...
[ [n + 1 for n in inner_list] for inner_list in list ]
Yeah, you need a nested comprehension:
[[item + 1 for item in list] for list in list_of_lists]
Another way would be to use map:
map(lambda l: map(lambda i: i + 1, l), list_of_lists)
You need to nest a comprehension into that comprehension. Unpack each sublist to make it easier.
[[a+1, b+1] for a,b in list_of_lists]
I have this list:
list1 = [['123'], ['456'], ['789']]
I want to convert this list into a string and later store it into a column in a database in this form:
123 / 456 / 789
I tried doing this:
s2 = ", ".join(repr(e) for e in list1)
print(s2)
But this is what I'm getting:
['123'], ['456'], ['789']
Any ideas on what should I do next to get the desired output?
You are close, but what you want to do is flatten your list of lists first, then convert to string. Like this:
" / ".join([item for sublist in list1 for item in sublist])
You can use itertools.chain
import itertools
list1 = [['123'], ['456'], ['789']]
", ".join(itertools.chain(*list1))
# => '123, 456, 789'
list1 = [['123'], ['456'], ['789']]
st = [ '/' + x[0] for x in list1]
st = ''.join(st)
print(st)
output
/123/456/789
Since you have lists in the list, you need to get the first indice so this helps you
" / ".join(map(lambda x:x[0],list1))
If you specifically only ever want the first element of a sublist even if it's got more than one element, then I think this one-liner is the clearest:
s2 = ' / '.join( x[0] for x in list1 )
I have a list:
my_list = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
How can I delete the \t and everything after to get this result:
['element1', 'element2', 'element3']
Something like:
>>> l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847']
>>> [i.split('\t', 1)[0] for i in l]
['element1', 'element2', 'element3']
myList = [i.split('\t')[0] for i in myList]
Try iterating through each element of the list, then splitting it at the tab character and adding it to a new list.
for i in list:
newList.append(i.split('\t')[0])
Do not use list as variable name.
You can take a look at the following code too:
clist = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', 'element5']
clist = [x[:x.index('\t')] if '\t' in x else x for x in clist]
Or in-place editing:
for i,x in enumerate(clist):
if '\t' in x:
clist[i] = x[:x.index('\t')]
Solution with map and lambda expression:
my_list = list(map(lambda x: x.split('\t')[0], my_list))
I had to split a list for feature extraction in two parts lt,lc:
ltexts = ((df4.ix[0:,[3,7]]).values).tolist()
random.shuffle(ltexts)
featsets = [(act_features((lt)),lc)
for lc, lt in ltexts]
def act_features(atext):
features = {}
for word in nltk.word_tokenize(atext):
features['cont({})'.format(word.lower())]=True
return features