what would be the quickest way to get to :
[[0, 2, 0, 0],\
[0, 2, 0, 0],\
[0, 2, 0, 0],\
[0, 2, 0, 0]]
from:
[[0, 0, 2, 0],\
[0, 0, 2, 0],\
[0, 0, 2, 0],\
[0, 0, 2, 0]]
without using numpy or any other external library
For your specific task:
l = [[0, 2, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0]]
for arr in l:
arr[1], arr[2] = arr[2], arr[1]
Let's say we have a list l, which is [0, 0, 2, 0], and we want to shift all the elements one place to left.
Firstly, we need to get all the elements to the right except the first one. List slicing l[1:] would work here, which would get [0, 2, 0].
Secondly, we need to get the remaining elements on the left with l[1:], which would get [0].
You can now probably see that we can shift the elements one place to the left with adding the above 2 lists together:
>>> lst = [0, 0, 2, 0]
>>> first = lst[1:]
>>> second = lst[:1]
>>> first + second
[0, 2, 0, 0]
Which can be summarized in this function:
def shift(lst, n):
return lst[n:] + lst[:n]
Since this can shift one lists position, it can applied to all lists in a nested list and shift their positions to left by 1:
nested_lst = [shift(sublist, 1) for sublist in nested_lst]
Related
So I have a list from which i want to remove something like a sub list. I don't know how to call it exactly so i couldn't find any results searching for an answer.
list = [[0, 1, 0, 0], [0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
I just want to remove one of those 4 brackets, for example the first one.
Should i use "remove" or "delete" or how do i do that?
list = [[0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
You can del it by its index. For example to delete the first one, you can do:
my_list = [[0, 1, 0, 0], [0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
del my_list[0]
print(my_list)
# output:
[[0, 1, 1, 1], [1, 1, 0, 0], [0, 0, 0, 0]]
If you know the index which you want to remove i:
del l[i]
# Or l.pop(i)
If you know the value you want to remove val:
l.remove(val)
P.S: Don't use list as the name of a variable. It's a name of a builtin class
What is the best way to assign a list of value to a 2D list with different indices?
temp_list = [2, 1, 1, 5]
I want to iterate thru a 2D list:
list_to_assign =
[[0, 0, 0, 0],
[0, 0, 0, 0],
[2, 0, 0, 1000],
[0, 0, 0, 0]]
For every value of temp_list, I want to assign that value to the zero index of every inner list in the 2D list.
My desired result is
[[2, 0, 0, 0],
[1, 0, 0, 0],
[1, 0, 0, 1000],
[5, 0, 0, 0]]
IIUC, try using this list comprehension below:
print([[x] + y[1:] for x,y in zip(temp_list,list_to_assign)])
Output:
[[2, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 1000], [5, 0, 0, 0]]
for i in range(len(temp_list)):
list_to_assign[i][0] = temp_list[i]
How do I create a matrix which would look like this? I don't want to use numpy.
[[0,1,2,3,4,5],
[1,0,0,0,0,0],
[2,0,0,0,0,0],
[3,0,0,0,0,0],
[4,0,0,0,0,0]]
I have tried this, which is wrong:
class MinimiumEdits(object):
def mini(self,str1,str2):
temp = [[0]*len(str1)]*len(str2)
for i in range(len(temp[0])):
temp[0][i] = i
for i in range(len(temp)):
temp[i][0] = i
MinimiumEdits().mini("apple","apples")
The inner lists all reference the same sublist (that's a side-effect of the list multiplication):
temp[0] is temp[1] # True
The is comparison checks if these are the same object. If you know about pointers this is doing a pointer equality check. Not a value equality (that would be == in Python).
That means that all changes are propagated to all "lines". To fix this you could simply change it to:
temp = [[0]*len(str1) for _ in range(len(str2))]
That should suffice to fix the problem.
Edit:
Without importing anything, you can do it like this:
def get_list(val, fillvalue=0):
return [list(range(val+1))] + [[k] + [fillvalue]*val for k in range(1, val)]
# Try with different values like
# get_list(4) or get_list(3)
final = get_list(5)
print(final)
Output:
[[0, 1, 2, 3, 4, 5],
[1, 0, 0, 0, 0, 0],
[2, 0, 0, 0, 0, 0],
[3, 0, 0, 0, 0, 0],
[4, 0, 0, 0, 0, 0]]
Well, you can do it in a little convoluted way with pure list comprehension:
[[x if not y else y if not x else 0 for x in range(6)] for y in range(5)]
# [[0, 1, 2, 3, 4, 5],
# [1, 0, 0, 0, 0, 0],
# [2, 0, 0, 0, 0, 0],
# [3, 0, 0, 0, 0, 0],
# [4, 0, 0, 0, 0, 0]]
I have an array of zeros and ones like [0,0,0,1,1,1,0,0,0,1,1]. How to write a program to save neighboring 0's and 1's in different arrays.
E.g.
[0,0,0,1,1,1,0,0,0,1,1] giving [0,0,0],[1,1,1],[0,0,0],[1,1].
You can group them with itertools.groupby, like this
>>> data = [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1]
>>> from itertools import groupby
>>> [list(group) for item, group in groupby(data)]
[[0, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1]]
The result of groupby, will be a tuple of actual item and an iterator which gives the grouped items. We just convert the grouped items to a list, with list(group).
As per the comments,
>>> data = [1, 2, 1, 3, 4, 5, 6, 7, 1, 2, 3]
>>> flag = [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1]
Create a generator which will give the values multiplied,
>>> gen = (d * v for d, v in zip(data, flag))
Now, group based on the result of calling bool on each of the numbers. So, if the bool is called on 0, it will give False, otherwise True.
>>> [list(g) for _, g in groupby(gen, key=bool)]
[[0, 0, 0], [3, 4, 5], [0, 0, 0], [2, 3]]
I am having a two dimensional list like:
[[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [0, 1, 1, 0], [1, 1, 1, 1]]
and I want to XOR the inner sub-lists with each other. So at some point of time with some combination I will be getting a sub list with all zeros like [0,0,0,0] and if I don't get with two sub lists I have to go for XORing of three sub-lists till I get again [0,0,0,0], if not then, have to go for four sub-lists.
The problem is I can do like picking up two lists and XOR each element then save it in separate sub-list and this works but each time I have to change my code so is there any way to XOR sub-list like [1, 1, 0, 0] ^ [1, 0, 1, 0] instead of doing like lis[i][j]^lis[i+1][j] so that I can manage my code recursively?
You could find shorter but I think this would work:
STOP=[0,0,0,0]
def nxor(l,res=None,p=1):
if not res: res= l[0]
if res == STOP:
return p-1 # or l[:p]
try:
return nxor(l,list(iter.imap (operator.xor, *list([res,l[p]]) )),p+1)
except:
return None # or res
print nxor (l)
It returns the position of the last sub-list that lead to [0,0,0,0]; it would be easy to return the sub-list instead, or the actual result (see comments).
If you want to test all the combinations, you could use itertools:
for i in xrange(2,len(l)):
for j in itertools.combinations(l,i):
print nxor(list(j))
What you could do is to convert the 2-D bit lists to a 1-D number list and then XOR them together.
a = [[1, 1, 0, 0], [1, 0, 1, 0], [0, 1, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [0, 1, 1, 0], [1, 1, 1, 1]]
b = map(lambda x: int("".join(map(lambda y: str(y), x)),2), a)
b is now: [12, 10, 6, 14, 14, 6, 15]
Now you are working with a 1-D list which will make that easier.