In my Python programming course, we are discussing how to manipulate (add,subtract,etc.) arrays and sub-arrays. An example from class was if we had
ourArray = [['a','b','c'],['e','f','g','h'],['i','j'],['k','l','m','n','o','p']...]
and Array = ['a','e','i','k',...], would something like ourArray-Array be possible?
I tried
for w in ourArray:
w[0] - Array[0]
In the end, what I would like is
[['a','b','c'],['e','f','g','h'],['i','j'],['k','l','m','n','o','p']...] - ['a','e','i','k',...] = ['b','c'],['f','g','h'],['j'],['l','m','n','o','p']...].
Also, I am using Python 3 in Windows.
How about this list comprehension, Pythonic, one liner:
>>> ourArray = [['a','b','c'],['e','f','g','h'],['i','j'],['k','l','m','n','o','p']]
>>> Array = ['a','e','i','k']
>>> [[item for item in arr if item not in Array] for arr in ourArray]
[['b', 'c'], ['f', 'g', 'h'], ['j'], ['l', 'm', 'n', 'o', 'p']]
For each array in ourArray, take only the items that are not in Array.
You can always do the brute force method
>>> ourArray = [['a','b','c'],['e','f','g','h'],['i','j'],['k','l','m','n','o','p']]
>>> Array = ['a','e','i','k']
>>> for i in ourArray:
... for j in i:
... if j in Array:
... i.remove(j)
...
>>> ourArray
[['b', 'c'], ['f', 'g', 'h'], ['j'], ['l', 'm', 'n', 'o', 'p']]
Related
I am wondering if there is a way to iterate over individual positions in a sequence list using NumPy. For example, if I had a list of sequences:
a = ['AGHT','THIS','OWKF']
The function would be able to go through each individual characters in their position. So for the first sequence 'AGHT', it would be broken down into 'A','G','H','T'. The ultimate goal is to create individual grids based on character abundance in each one of these sequences. So far I have only been able to make a loop that goes through each character, but I need this in NumPy:
b = np.array(a)
for c in b:
for d in c:
print(d)
I would prefer this in NumPy, but if there are other ways I would like to know as well. Thanks!
list expands a string into a list:
In [406]: a = ['AGHT','THIS','OWKF']
In [407]: [list(item) for item in a]
Out[407]: [['A', 'G', 'H', 'T'], ['T', 'H', 'I', 'S'], ['O', 'W', 'K', 'F']]
You can use join() to join the array into a sequence of characters, then iterate over each character or print it like this:
>>> a = ['AGHT','THIS','OWKF']
>>> print(''.join(a))
'AGHTTHISOWKF'
Or to turn it into an array of individual characters:
>>> out = ''.join(a)
>>> b = np.array(list(out))
array(['A', 'G', 'H', 'T', 'T', 'H', 'I', 'S', 'O', 'W', 'K', 'F'],
dtype='<U1')
Given an array say x = ['A','I','R']
I would want output as an
[['A','I','R'],['A','I'],['I','R'],['A'],['I'],['R']]
What I don't want as output is :
[['A','I','R'],['A','I'],['I','R'],['A','R'],['A'],['I'],['R']] # extra ['A','R'] which is not in sequence .
Below is the code which gives the output I don't want:
letter_list = [a for a in str]
all_word = []
for i in xrange(0,len(letter_list)):
all_word = all_word + (map(list, itertools.combinations(letter_list,i))) # dont use append. gives wrong result.
all_word = filter(None,all_word) # remove empty combination
all_word = all_word + [letter_list] # add original list
My point is I only want combinations of sequences. Is there any way to use itertools or should I write custom function ?
Yes, you can use itertools:
>>> x = ['A', 'I', 'R']
>>> xs = [x[i:j] for i, j in itertools.combinations(range(len(x)+1), 2)]
>>> xs
[['A'], ['A', 'I'], ['A', 'I', 'R'], ['I'], ['I', 'R'], ['R']]
>>> sorted(xs, key=len, reverse=True)
[['A', 'I', 'R'], ['A', 'I'], ['I', 'R'], ['A'], ['I'], ['R']]
Credit: answer by hochl
Try to use yield:
x = ['A','I','R']
def groupme(x):
s = tuple(x)
for size in range(1, len(s) + 1):
for index in range(len(s) + 1 - size):
yield list(x[index:index + size])
list(groupme(x))
>>> [['A'], ['I'], ['R'], ['A', 'I'], ['I', 'R'], ['A', 'I', 'R']]
don't try to be so magical: two loops will do what you want; one over possible sequence starts, the inner over possible sequence lengths:
x = "AIR" # strings are iterables/sequences, too!
all_words = []
for begin in xrange(len(x)):
for length in xrange(1,len(x) - begin+1):
all_words.append(x[begin:begin+length])
using list comprehension:
letters=['A', 'I', 'R']
[letters[start:end+1]
for start in xrange(len(letters))
for end in xrange(start, len(letters))]
[['A'], ['A', 'I'], ['A', 'I', 'R'], ['I'], ['I', 'R'], ['R']]
if it is important to have the order you proposed (from longest to shortest and when the same length by starting position) you can do instead:
[letters[start:start+l+1]
for l in range(len(letters))[::-1]
for start in xrange(len(letters)-l)]
[['A', 'I', 'R'], ['A', 'I'], ['I', 'R'], ['A'], ['I'], ['R']]
Just to address Holroy comment. If instead of using list comprehension you use a generator expression (just substituting external [] with ()) you would get a much less memory requiring code. But in this case you must be careful of not using the result more than once or for instance not trying to use list methods (such as len, or removing elements) on the result.
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.
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']
I have a nested list that looks like this:
li = [['m', 'z', 'asdgwergerwhwre'],
['j', 'h', 'asdgasdgasdgasdgas'],
['u', 'a', 'asdgasdgasdgasd'],
['i', 'o', 'sdagasdgasdgdsag']]
I would like to sort this list alphabetically, BUT using either the first or second element in each sublist. For the above example, the desired output would be:
['a', 'u', 'asdgasdgasdgasd']
['h', 'j', 'asdgasdgasdgasdgas']
['i', 'o', 'sdagasdgasdgdsag']
['m', 'z', 'asdgwergerwhwre']
What is the best way to achieve this sort?
As the first step we perform some transformation (swap for first two items - if needed) and at the second aplly simple sort:
>>> sorted(map(lambda x: sorted(x[:2]) + [x[2]], li))
[['a', 'u', 'asdgasdgasdgasd'],
['h', 'j', 'asdgasdgasdgasdgas'],
['i', 'o', 'sdagasdgasdgdsag'],
['m', 'z', 'asdgwergerwhwre']]
You can make use of the built-in method sorted() to accomplish some of this. You would have to reverse the order of the list if you wanted to reverse the way it was printed, but that's not too difficult to do.
def rev(li):
for l in li:
l[0], l[1] = l[1], l[0]
return li
new_list = sorted(rev(li))
If you wanted to sort the list based on a specific index, you can use sorted(li, key=lambda li: li[index]).
import pprint
li = [['m', 'z', 'asdgwergerwhwre'],
['j', 'h', 'asdgasdgasdgasdgas'],
['u', 'a', 'asdgasdgasdgasd'],
['i', 'o', 'sdagasdgasdgdsag']]
for _list in li:
_list[:2]=sorted(_list[:2])
pprint.pprint(sorted(li))
>>>
[['a', 'u', 'asdgasdgasdgasd'],
['h', 'j', 'asdgasdgasdgasdgas'],
['i', 'o', 'sdagasdgasdgdsag'],
['m', 'z', 'asdgwergerwhwre']]