Adding to list indices - list

say I have a list:
x = [2, [2, 5, 6], 'car', 7]
I understand I can append to x[1] by using x[1].append('hi')
which gives:
x = [2, [2, 5, 6, 'hi'], 'car', 7]
How can I turn other index positions into their own lists so I can append.
At the moment I'm trying:
x[0].append('hi')
which gives this error:
AttributeError: 'int' object has no attribute 'append'
The output I'm looking for would be:
x = [[2, 'hi'] , [2, 5, 6, 'hi'], 'car', 7]

Just make a simple casting type:
x[0] = [x[0]]
x[0].append(...)

Related

Classification of same rows in 2D numpy

Hi i want to classify indexes of same rows in 2D numpy array. Is there any function to do it ?
Something like this :
a= [[1,2,3] , [2,3,4] , [5,6,7] ,[1,2,3] ,[1,2,3] , [2,3,4]]
then f(a) returns same row indexes [[0,3,4],[1,5],[2]]
I would appreciate for your solutions
Here's one to output list of arrays of row indices -
def classify_rows(a):
sidx = np.lexsort(a.T)
b = a[sidx]
m = ~(b[1:] == b[:-1]).all(1)
return np.split(sidx, np.flatnonzero(m)+1)
If you need a list of lists as output -
def classify_rows_list(a):
sidx = np.lexsort(a.T)
b = a[sidx]
m = np.concatenate(( [True], ~(b[1:] == b[:-1]).all(1), [True]))
l = sidx.tolist()
idx = np.flatnonzero(m)
return [l[i:j] for i,j in zip(idx[:-1],idx[1:])]
Sample run -
In [78]: a
Out[78]:
array([[1, 2, 3],
[2, 3, 4],
[5, 6, 7],
[1, 2, 3],
[1, 2, 3],
[2, 3, 4]])
In [79]: classify_rows(a)
Out[79]: [array([0, 3, 4]), array([1, 5]), array([2])]
In [80]: classify_rows_list(a)
Out[80]: [[0, 3, 4], [1, 5], [2]]

Zipp lists in python by iterating through a list of lists

I have two lists of lists as follows. To merge them, I usually do the following:
>>>from itertools import imap, ilist
>>>a = [1,2,3]
>>>b = [4,5,6]
>>> c = list(imap(list,izip(a,b)))
>>> c
[[1, 4]], [2, 5], [3, 6]]
However, now I have a list of list as follows:
[[1,2,3],
[4,5,6],
[7,8,9],
]
How do I iterate through each list and pass it to the izip function to obtain the following output:
[[1,4,7],[2,5,8],[3,6,9]]
Answer for edited Question:
>>> input_list=[[1,2,3],
[4,5,6],
[7,8,9],
]
Using map and zip:
>>> map(list,zip(*input_list))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Using imap and izip:
>>> list(imap(list,list(izip(*input_list))))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Answer for previous question:
By using list comprehension and two for loops:
input_list =[[[1],[2],[3]],
[[4],[5],[6]],
[[7],[8],[9]],
]
out_list = [[] for i in range(len(input_list))]
for each_row in input_list:
for i in range(len(each_row)):
out_list[i].extend(each_row[i])
print out_list
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
I think u need something like this:
input_list =[[1,2,3],
[4,5,6],
[7,8,9],
]
result = []
for i in range(len(input_list)):
temp = []
for list in input_list:
temp.append(list[i])
result.append(temp)
print result
result will be:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

How do I reassign the first index of this matrix 'a' without changing 'b'?

I am trying to change a[0][0] without changing the matrix b. Is there an easy way to do this? I tried to use b = list(a) and b = a[:] but no luck.
a = [[1,2,3,4],[1,2,3,4]]
b = a
print(a)
print(b)
a[0][0] = "WWWW"
print(a)
print(b)
Output:
[[1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4]]
[['WWWW', 2, 3, 4], [1, 2, 3, 4]]
[['WWWW', 2, 3, 4], [1, 2, 3, 4]]
This worked but it seems like there could be a better way?
a = [[1,2,3,4],[1,2,3,4]]
b = [[0,0,0,0], [0,0,0,0]]
for i in range(len(a)):
for j in range(len(a[i])):
b[i][j] = a[i][j]
print(a)
print(b)
a[0][0] = "WWWW"
print(a)
print(b)
Output:
[[1, 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4]]
[['WWWW', 2, 3, 4], [1, 2, 3, 4]]
[[1, 2, 3, 4], [1, 2, 3, 4]]
Lists are mutable.
This link explains in detail what mutable objects are
This doesn't work in your code because, you are using nested lists. If you were to do just a[0]="something", this wouldn't reflect in b however if you do something like this a[0][0]="wow" this would.
b = a[:]
The nested list in both a and b still point towards the same block of memory.
To solve this try:
import copy
a = [[1,2,3,4],[1,2,3,4]]
b = copy.deepcopy(a)
Explained in detail here

How to get the following two list?

Hello I am working with some lists I have the following list:
a = [1,2,3,4,5,6]
I would like to get the these two lists from a:
b = [2,3,4,5,6]
c = [1,2,3,4,5]
I would like to get the firt one removing the first element of a and the second one removing the last element of a, I tried:
b = a
c = a
b.pop(0)
c.pop(len(a)-1)
print(b)
print(c)
print(a)
However the output is:
[2, 3, 4, 5]
[2, 3, 4, 5]
[2, 3, 4, 5]
that is affecting my fist list, I am not sure about what I am doing, I would like to appreciate support with this.
You should not modify the original list - it's simpler than you think, just slice the input list passing the right indexes. Try this:
a = [1, 2, 3, 4, 5, 6]
b = a[1:]
c = a[:-1]
a
=> [1, 2, 3, 4, 5, 6]
b
=> [2, 3, 4, 5, 6]
c
=> [1, 2, 3, 4, 5]
I think slicing would be ok:
b =a[1:];
c =a[:-1]

How to select values 2+ after any number in a list?

Is there any way to select the second, third (etc) value from a value in a list in Groovy? I'm still very new to programming in general and am just wondering if there is an easy way to do this.
For example, if I have the list
[1, 2, 3, 4, 5, 6]
I want to select the next two values after each value using a for loop:
for 1: (1, 2, 3)
for 2: (2, 3, 4)
...and so on.
Is that easily possible? Thanks in advance!
If you're using groovy 1.8.1 or later, you can use the take and drop methods:
def foo = [1, 2, 3, 4, 5, 6]
foo.size().times { i ->
println foo.drop(i).take(3)
}
This will result in
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6]
[6]
If you want the iteration to stop at the last group of three, try something like this:
def foo = [1, 2, 3, 4, 5, 6]
if (foo.size() > 2) {
(foo.size() - 2).times { i ->
println foo.drop(i).take(3)
}
}
which gives
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
If you're not using Groovy 1.8.1+, then you can acheive a similar result by writing a function like so:
List split( List foo, int size ) {
(0..foo.size()-size).collect { foo[ it..it+size-1 ] }
}
Then, you can call this like:
split( [1, 2, 3, 4, 5, 6], 3 )
to print
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]