I have the following list of lists:
db=[['CAGAAGT'],['TGACAG'],['GAAGT']]
I need to split the internal text of each sublist so that it looks like this:
db=[['C','A','G','A','A','G','T'],['T','G','A','C','A','G'],['G','A','A','G','T']]
I have tried the following code but an error appears, saying: list has no attribute .split()
db = [e.split() for e in db]
Is there a way to do this?
This should help you:
db=[['CAGAAGT'],['TGACAG'],['GAAGT']]
db = [list(elem) for lst in db for elem in lst]
print(db)
Output:
[['C', 'A', 'G', 'A', 'A', 'G', 'T'], ['T', 'G', 'A', 'C', 'A', 'G'], ['G', 'A', 'A', 'G', 'T']]
split can be applied on strings only but you are trying to apply it on e which in your case is a list.
Try instead:
db = [list(e[0]) for e in db]
Related
I have a list that I called lst, it is as follows:
lst = ['A', 'C', 'T', 'G', 'A', 'C', 'G', 'C', 'A', 'G']
What i want to know is how to split this up into four letter strings which start with the first, second, third, and fourth letters; then move to the second, third, fourth and fifth letters and so on and then add it to a new list to be compared to a main list.
Thanks
To get the first sublist, use lst[0:4]. Use python's join function to merge it into a single string. Use a for loop to get all the sublists.
sequences = []
sequence_size = 4
lst = ['A', 'C', 'T', 'G', 'A', 'C', 'G', 'C', 'A', 'G']
for i in range(len(lst) - sequence_size + 1):
sequence = ''.join(lst[i : i + sequence_size])
sequences.append(sequence)
print(sequences)
All 4-grams (without padding):
# window size:
ws = 4
lst2 = [
''.join(lst[i:i+ws])
for i in range(0, len(lst))
if len(lst[i:i+ws]) == 4
]
Non-overlapping 4-grams:
lst3 = [
''.join(lst[i:i+ws])
for i in range(0, len(lst), ws)
if len(lst[i:i+ws]) == 4
]
I think the other answers solve your problem, but if you are looking for a pythonic way to do this, I used List comprehension. It is very recommended to use this for code simplicity, although sometimes diminish code readability. Also it is quite shorter.
lst = ['A', 'C', 'T', 'G', 'A', 'C', 'G', 'C', 'A', 'G']
result = [''.join(lst[i:i+4]) for i in range(len(lst)-3)]
print(result)
Use:
lst = ['A', 'C', 'T', 'G', 'A', 'C', 'G', 'C', 'A', 'G']
i=0
New_list=[]
while i<(len(lst)-3):
New_list.append(lst[i]+lst[i+1]+lst[i+2]+lst[i+3])
i+=1
print(New_list)
Output:
['ACTG', 'CTGA', 'TGAC', 'GACG', 'ACGC', 'CGCA', 'GCAG']
I have multiple data frames. I need to merge them all and then set one by one column from all df.
I make it simple for you.i have multiple lists .like
l1=[a,b,c]
l2=[d,e,f]
l3=[g,h,i]
I want my list such that give below.
list=[a,d,g,b,e,h,c,f,i]
I am using numpy array
np.array([l1,l2,l3]).ravel('F')
Out[537]: array(['a', 'd', 'g', 'b', 'e', 'h', 'c', 'f', 'i'], dtype='<U1')
since you mention pandas
pd.DataFrame([l1,l2,l3]).melt().value.tolist()
Out[543]: ['a', 'd', 'g', 'b', 'e', 'h', 'c', 'f', 'i']
l1=['a','b','c']
l2=['d','e','f']
l3=['g','h','i']
list1 = []
for i in range(len(l1)):
list1.append(l1[i])
list1.append(l2[i])
list1.append(l3[i])
print (list1)
list(itertools.chain.from_iterable(zip(l1, l2, l3)))
worked for me.
I have a function that is supposed to merge two sorted lists into a combined sorted list. I know there are other ways of accomplishing this, but can someone explain why this code doesn't work
def merge_two(list1,list2):
new=[]
l1=list1[:]
l2=list2[:]
while l1 and l2:
if l1[0]<l2[0]:
new.append(l1.pop(0))
else:
new.append(l2.pop(0))
print(new,l1,l2)
return new+l1+l2
For some reason the while loop only seems to run once. For example if I use list=['a','x','z'] and list2=['b','c','f','g'], the print line at the end of the function results in ['a']['x','z']['b','c','f','g']
From debugging this seems to be due to the while loop only executing once, but I'm not sure why that's happening...it should go until either l1 or l2 is empty
That's because a function breaks after it returns something. You need to un-indent your return statement:
def merge_two(list1,list2):
new=[]
l1=list1[:]
l2=list2[:]
while l1 and l2:
if l1[0]<l2[0]:
new.append(l1.pop(0))
else:
new.append(l2.pop(0))
return new+l1+l2
Alternatively, if for some reason you would like a list of all the steps your function produced, you could actually use yield instead of return in the exact same spot you used your return at:
def merge_two(list1,list2):
new=[]
l1=list1[:]
l2=list2[:]
while l1 and l2:
if l1[0]<l2[0]:
new.append(l1.pop(0))
else:
new.append(l2.pop(0))
yield new+l1+l2
Then running it:
>>> list(merge_two(a, b))
[['a', 'x', 'z', 'b', 'c', 'f', 'g'],
['a', 'b', 'x', 'z', 'c', 'f', 'g'],
['a', 'b', 'c', 'x', 'z', 'f', 'g'],
['a', 'b', 'c', 'f', 'x', 'z', 'g'],
['a', 'b', 'c', 'f', 'g', 'x', 'z']]
And of course you'll see that the last list yielded is the sorted list :).
When I write this code:
f=['a','b','c',['d','e','f']]
def j(f):
p=f[:]
for i in range(len(f)):
if type(p[i]) == list:
p[i].reverse()
p.reverse()
return p
print(j(f), f)
I expect that the result would be:
[['f', 'e', 'd'], 'c', 'b', 'a'] ['a', 'b', 'c', ['d', 'e', 'f']]
But the result I see is:
[['f', 'e', 'd'], 'c', 'b', 'a'] ['a', 'b', 'c', ['f', 'e', 'd']]
Why? And how can I write a code that do what I expect?
reverse modifies the list in place, you actually want to create a new list, so you don't reverse the one you've got, something like this:
def j(f):
p=f[:]
for i in range(len(f)):
if type(p[i]) == list:
p[i] = p[i][::-1]
p.reverse()
return p
I've got the following list :
[['a','b','c'],['d','e'],['f','g','h','i',j]]
I would like a list like this :
['abc','de','fghij']
How is it possible?
[Edit] : in fact, my list could have strings and numbers,
l = [[1,2,3],[4,5,6], [7], [8,'a']]
and would be :
l = [123,456, 7, 8a]
thx to all,
you can apply ''.join method for all sublists.
This can be done either using map function or using list comprehensions
map function runs function passed as first argument to all elements of iterable object
initial = ['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h', 'i', 'j']]
result = map(''.join, initial)
also one can use list comprehension
initial = ['a', 'b', 'c'], ['d', 'e'], ['f', 'g', 'h', 'i', 'j']]
result = [''.join(sublist) for sublist in initial]
Try
>>> L = [['a','b','c'],['d','e'],['f','g','h','i','j']]
>>> [''.join(x) for x in L]
['abc', 'de', 'fghij']