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.
Related
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]
Here is my list,
a = [['a','b','c','d'],['e','f','g','h'],['i','j','k','l'],['m','n','o','p']]
and Here is my function,
def add(change,unchange):
a = change
b = unchange
a[0].insert(a[0].index(a[0][2]),"high_range")
a[0].insert(a[0].index(a[0][3]),"low_range")
print(a)
print(b)
When I try to execute this function,
add(a,a[0])
I'm getting this output,
[['a', 'b', 'high_range', 'low_range', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p']]
['a', 'b', 'high_range', 'low_range', 'c', 'd']
But my expected output is the following,
[['a', 'b', 'high_range', 'low_range', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p']]
['a', 'b', 'c', 'd']
How to make the first element of the list keep on same in the second variable ? Sorry I'm newbie.
Since a list is a mutable type, when you insert values into a this also gets reflected in b, since they are pointers to the same list. You can either print b before inserting values into the list, or make b a copy of unchange like this:
def add(change,unchange):
a = change
b = unchange[:]
a[0].insert(2, "high_range")
a[0].insert(3, "low_range")
print(a)
print(b)
Also, a[0].index(a[0][2]) is redundant, you already know that the index is 2.
The main problem is in line:
add(a, a[0])
as you are mutating a inside the function a[0] will change as well as they point to the same thing. You need to design your program accordingly. You can refer to this answer. How to clone or copy a list?
depending upon your requirement you can do this.
either suply a copy while calling a function.
add(a, a[0][:]) # or
read #alec's answer.
your function is perfect but execute this:
add(a,a[0][:])
this will make the second variable, namely a[0], a copy, which will be left unchanged.
Hi I have a list as following:
listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
15 members.
I want to turn it into 3 lists, I used this code it worked but I want unique lists. this give me 3 lists that have mutual members.
import random
listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
print(random.sample(listt,5))
print(random.sample(listt,5))
print(random.sample(listt,5))
Try this:
from random import shuffle
def randomise():
listt = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
shuffle(listt)
return listt[:5], listt[5:10], listt[10:]
print(randomise())
This will print (for example, since it is random):
(['i', 'k', 'c', 'b', 'a'], ['d', 'j', 'h', 'n', 'f'], ['e', 'l', 'o', 'g', 'm'])
If it doesn't matter to you which items go in each list, then you're better off partitioning the list into thirds:
In [23]: L = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
In [24]: size = len(L)
In [25]: L[:size//3]
Out[25]: ['a', 'b', 'c', 'd', 'e']
In [26]: L[size//3:2*size//3]
Out[26]: ['f', 'g', 'h', 'i', 'j']
In [27]: L[2*size//3:]
Out[27]: ['k', 'l', 'm', 'n', 'o']
If you want them to have random elements from the original list, you'll just need to shuffle the input first:
random.shuffle(L)
Instead of sampling your list three times, which will always give you three independent results where individual members may be selected for more than a single list, you could just shuffle the list once and then split it in three parts. That way, you get three random subsets that will not share any items:
>>> random.shuffle(listt)
>>> list[0:5]
>>> listt[0:5]
['b', 'a', 'f', 'e', 'h']
>>> listt[5:10]
['c', 'm', 'g', 'j', 'o']
>>> listt[10:15]
['d', 'l', 'i', 'n', 'k']
Note that random.shuffle will shuffle the list in place, so the original list is modified. If you don’t want to modify the original list, you should make a copy first.
If your list is larger than the desired result set, then of course you can also sample your list once with the combined result size and then split the result accordingly:
>>> sample = random.sample(listt, 5 * 3)
>>> sample[0:5]
['h', 'm', 'i', 'k', 'd']
>>> sample[5:10]
['a', 'b', 'o', 'j', 'n']
>>> sample[10:15]
['c', 'l', 'f', 'e', 'g']
This solution will also avoid modifying the original list, so you will not need a copy if you want to keep it as it is.
Use [:] for slicing all members out of the list which basically copies everything into a new object. Alternatively just use list(<list>) which copies too:
print(random.sample(listt[:],5))
In case you want to shuffle only once, store the shuffle result into a variable and copy later:
output = random.sample(listt,5)
first = output[:]
second = output[:]
print(first is second, first is output) # False, False
and then the original list can be modified without the first or second being modified.
For nested lists you might want to use copy.deepcopy().
I want to read a matfile in python and then export the data in a database. in order to do this I need to have the data type as list in python. I wrote the code below:
import scipy.io as si
import csv
a = si.loadmat('matfilename')
b = a['variable']
list1=b.tolist()
The variable has 1 row and 15 columns. when I print list1, I get the answer below: (It is indeed a list, but a list that contains only one element. It means when I call list1[0], I get the same result.):
[[array(['A'],
dtype='<U13'), array(['B'],
dtype='<U14'), array(['C'],
dtype='<U6'), array(['D'],
dtype='<U4'), array(['E'],
dtype='<U10'), array(['F'],
dtype='<U13'), array(['G'],
dtype='<U11'), array(['H'],
dtype='<U9'), array(['I'],
dtype='<U16'), array(['J'],
dtype='<U18'), array(['K'],
dtype='<U16'), array(['L'],
dtype='<U16'), array(['M'],
dtype='<U16'), array(['N'],
dtype='<U14'), array(['O'],
dtype='<U13')]]
While the form that I expect is:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
Does anyone know what the problem is?
To my experience, that is just like MATLAB files are structured, only nested arrays.
You can create the list yourself:
>>> [x[0][0] for x in list1[0]]
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']
I started with something like this:
[a,b,s,d]
[k,e,f,s,d]
[o,w,g]
Then I wanted to rearrange them by length in descending order so that I get this:
[k,e,f,s,d]
[a,b,s,d]
[o,w,g]
However, to do that, I appended each of those into an array as such:
arr = [[a,b,s,d], [k,e,f,s,d], [o,w,g]]
so that I could just use:
sorted(arr, key=len).reverse()
But now I can't unpack arr to just get:
[k,e,f,s,d]
[a,b,s,d]
[o,w,g]
Ideas?
Thanks.
reverse() is an in-place function:
arr = [['a','b','s','d'], ['k','e','f','s','d'], ['o','w','g']]
a = sorted(arr, key=len)
print a
# [['o', 'w', 'g'], ['a', 'b', 's', 'd'], ['k', 'e', 'f', 's', 'd']]
print a.reverse()
# None
print a
# [['k', 'e', 'f', 's', 'd'], ['a', 'b', 's', 'd'], ['o', 'w', 'g']]
reverse() has no output, but it does reverse the array.