How to print last line in a list in Python? - python-2.7

import random
s = []
for i in range(14):
s.append(random.randint(0,5))
print(s)
how i just print last line in a list? Because Python print me s like this:
[3]
[3, 0]
[3, 0, 3]
[3, 0, 3, 0]
[3, 0, 3, 0, 2]
[3, 0, 3, 0, 2, 3]
[3, 0, 3, 0, 2, 3, 3]
[3, 0, 3, 0, 2, 3, 3, 0]
[3, 0, 3, 0, 2, 3, 3, 0, 5]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5, 5]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5, 5, 1]
[3, 0, 3, 0, 2, 3, 3, 0, 5, 3, 5, 5, 1, 4] <- this is the only line I want Python print me..
Please help me.
And another question...
How i change this program:
for i in s:
if i < 1:
print ("It's all good.")
else:
print("Not good")
that he will print me "Not good" when at least one of numbers in list is 0?

You are almost certainly doing this:
import random
s = []
for i in range(14):
s.append(random.randint(0,5))
print(s)
Instead, change it to this:
import random
s = []
for i in range(14):
s.append(random.randint(0,5))
print(s)
In Python, indentation is significant, so if you have your print statement indented inside your for loop, it will be called each time through the loop. If you have it indented at the same level as the for ... it will only execute once after the loop has finished completely.
As to the second question regarding:
for i in s:
if i < 1:
print ("It's all good.")
else:
print("Not good")
I am not sure where you want to place the logic you are looking for, but you can test if "at least one of numbers in list is 0" with:
if 0 in s:
print("Not good")
You should have this check outside of your for loop though, because this check need only be performed once per list, rather than once per value in the list.

Related

convert data from 2d to 3d numpy array

If I have a 2d array like:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
And I want to end up with a 3d array like:
array([[[0, 4, 8],
[1, 5, 9]],
[[2, 6, 10],
[3, 7, 11]]])
How should I reshape the array to get what I want?
Reshape and permute axes -
In [11]: a # Input array
Out[11]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [12]: a.reshape(-1,2,2).transpose(1,2,0)
Out[12]:
array([[[ 0, 4, 8],
[ 1, 5, 9]],
[[ 2, 6, 10],
[ 3, 7, 11]]])
With np.moveaxis -
np.moveaxis(a.reshape(-1,2,2), 0,-1)
Generalizing it and assuming that you want the length along the first axis as half of no. of columns -
In [16]: m,n = a.shape
In [17]: a.reshape(m,-1,2).transpose(1,2,0)
Out[17]:
array([[[ 0, 4, 8],
[ 1, 5, 9]],
[[ 2, 6, 10],
[ 3, 7, 11]]])
If that length is supposed to be 2 -
In [15]: a.reshape(m,2,-1).transpose(1,2,0)
Out[15]:
array([[[ 0, 4, 8],
[ 1, 5, 9]],
[[ 2, 6, 10],
[ 3, 7, 11]]])

Create a function genSet() that takes in a list of numbers and returns a sorted set in python

I am new to python and trying to solve this example on pyschool
I need to write a function,
a) that takes a list of numbers
b) removes duplicates from the list
c) returns a sorted set:
In python, example :
>>> genSet([5,4,8,4,9,8])
[4, 5, 8, 9 ]
>>> genSet([3,-2,-1,-1,3,-2,0])
[-2, -1, 0, 3 ]
Removing Duplicates:
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> list(set(t))
[1, 2, 3, 5, 6, 7, 8]
>>> s = [1, 2, 3]
>>> list(set(t) - set(s))
[8, 5, 6, 7]
Sort the List:
>>> sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
Now you could write your custom function, that removes duplicates and sort.
The following code snippet works:
def genSet(clist):
t = list(set(clist))
return sorted(t)
if __name__ == "__main__":
print genSet([5,4,8,4,9,8])
print genSet([3,-2,-1,-1,3,-2,0])
If you want to iterate over multiple list try this:
a = [
[5,4,8,4,9,8],
[3,-2,-1,-1,3,-2,0]
]
for aa in a:
print genSet(aa)
set will automatically remove duplicates and sorted will sort the list.
def genSet(l):
return (sorted(set(l)))

Understanding Python list.append behavior

Why in the following example is appending to the big_l in the for loop changes also the last lists already added to the big_l?
l=[1,2,3,4,5]
big_l=[]
def f(ll):
x=ll.pop(0)
ll.append(x)
return ll
for i in range(4):
big_l.append(l)
print l,big_l
l=f(l)
It prints:
[1, 2, 3, 4, 5] - [[1, 2, 3, 4, 5]]
[2, 3, 4, 5, 1] - [[2, 3, 4, 5, 1], [2, 3, 4, 5, 1]]
[3, 4, 5, 1, 2] - [[3, 4, 5, 1, 2], [3, 4, 5, 1, 2], [3, 4, 5, 1, 2]]
[4, 5, 1, 2, 3] - [[4, 5, 1, 2, 3], [4, 5, 1, 2, 3], [4, 5, 1, 2, 3], [4, 5, 1, 2, 3]]

sequentially shorter list from the right via indexing - need [:-0] to act like [:]

I've abstracted my problem to something very simple. I'd like the [:-0] to give me [:] but can't see how to do it without using a special case.
I'm trying to get the behavior indicated below:
The first cases are just for completeness.
a = range(6)
print "a[i:] for i in range(3)"
for i in range(3):
print a[i:]
print "a[:i] for i in range(3)"
for i in range(3):
print a[:i]
print "a[-i:] for i in range(3)"
for i in range(3):
print a[-i:]
print "a[:i] for i in [None, -1, -2] DESIRED RESULT"
for i in [None, -1, -2]:
print a[:i]
print "a[:-i] for i in range(3) BUT CAN'T GET IT HERE"
for i in range(3):
print a[:-i]
print "a[:i] for i in [0, -1, -2] OR HERE EITHER"
for i in [0, -1, -2]:
print a[:i]
You can run it in 2.7 or just refer to the results below:
a[i:] for i in range(3)
[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[2, 3, 4, 5]
a[:i] for i in range(3)
[]
[0]
[0, 1]
a[-i:] for i in range(3)
[0, 1, 2, 3, 4, 5]
[5]
[4, 5]
a[:i] for i in [None, -1, -2] DESIRED RESULT
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
a[:-i] for i in range(3) BUT CAN'T GET IT HERE
[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
a[:i] for i in [0, -1, -2] OR HERE EITHER
[]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
You need to take advantage of Python's coalescing operators:
>>> for i in range(3):
... print a[:-i or None]
...
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
>>> for i in [0, -1, -2]:
... print a[:i or None]
...
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]

Python How to sort a list of list with certain conditions

I have list of list and I want to sort the list by 4 index by alphabetically but there are cases where it will be none or empty so those cases needed to be at the bottom.
For further clarfication - It should be sorted by AaBbCc then either None or empty if fine. I just want the first items to be aplhabetically orderd case insenstive.
from operator import itemgetter
list_of_list = [[0,1,2,3,'Ab'],[0,1,2,3,'bA'],[0,1,2,3,' '],[0,1,2,3,'None'], [0,1,2,3,''],[0,1,2,3,'Ca'] ]
list_of_list = sorted(list_of_list, key=itemgetter(4))
print list_of_list
Output: [[0, 1, 2, 3, ''], [0, 1, 2, 3, ' '], [0, 1, 2, 3, 'Ab'], [0, 1, 2, 3, 'Ca'], [0, 1, 2, 3, 'None'], [0, 1, 2, 3, 'bA']]
Should be outputed as such:
[[0, 1, 2, 3, 'Ab'], [0, 1, 2, 3, 'bA'],[0, 1, 2, 3, 'Ca'], [0, 1, 2, 3, 'None'], [0, 1, 2, 3, ''], [0, 1, 2, 3, ' ']]
You can try this:
>>> list_of_list = [[0, 1, 2, 3, 'Ab'],
[0, 1, 2, 3, 'bA'],
[0, 1, 2, 3, ' '],
[0, 1, 2, 3, None],
[0, 1, 2, 3, ''],
[0, 1, 2, 3, 'Ca']]
>>> list_of_list = sorted(list_of_list,
key=lambda x: x[4] if isinstance(x[4], basestring) else "",
reverse=True)
>>> print list_of_list
[[0, 1, 2, 3, 'bA'], [0, 1, 2, 3, 'Ca'], [0, 1, 2, 3, 'Ab'], [0, 1, 2, 3, ' '], [0, 1, 2, 3, None], [0, 1, 2, 3, '']]
What this does is use the fourth element as sort key if it is a string, else it will use the empty string as comparison key.
Alternately you could split your list in two lists, only sort the first one, and append the remaining elements like this:
list_of_list = sorted(x
for x in list_of_list
if isinstance(x[4], basestring) and len(x[4].strip())) + \
[x
for x in list_of_list
if not isinstance(x[4], basestring) or not len(x[4].strip())]
print list_of_list
yields
[[0, 1, 2, 3, 'Ab'], [0, 1, 2, 3, 'Ca'], [0, 1, 2, 3, 'bA'], [0, 1, 2, 3, ' '], [0, 1, 2, 3, None], [0, 1, 2, 3, '']]
It is not entirely clear if there are other sorting criteria that you need to follow, and this solution is not totally pretty, but at least it sorts the strings in front and keeps the rest at the back.