There's no mention of numpy in the question. If by array you mean list, then if you treat a list as a boolean it will yield True if it has items and False if it's empty.
l = []
if l:
print "list has items"
if not l:
print "list is empty"
Answer from John Kugelman on Stack OverflowThere's no mention of numpy in the question. If by array you mean list, then if you treat a list as a boolean it will yield True if it has items and False if it's empty.
l = []
if l:
print "list has items"
if not l:
print "list is empty"
with a as a numpy array, use:
if a.size:
print('array is not empty')
(in Python, objects like [1,2,3] are called lists, not arrays.)
I have read that this operation works in constant time. But how is that possible?
Given an array of length n, I would have thought that computing n would require accessing every element. In pseudocode, something like the following:
n = 0
for each item in array:
n = n + 1
return nBut that would clearly be O(n), suggesting that’s not how it actually works.
Can anyone shed light on this?
with open (my_file) as my_probe:
for fileLine in my_probe:
src_string = fileLine.split()
my_list = list(src_string) # The list may contain any number of items
if len(my_list) >= 5: # Only if lists is longer than 4 items
print("Info I need: "+my_list[4])
Note down the list index starts with 0 however length is the count of actual element in that list.
First one there is no string named my_string in your code. Other Problem is you are accessing the 5th element of list which has length of 3. so you have change your if statement to if len(my_list)>=5.
with open (my_file, 'r') as my_probe:
for fileLine in my_probe:
src_string = fileLine.split()
my_list = list(src_string) # The list may contain any number of items
if len(my_list) >= 5: # Only if lists is longer than 4 items
print(my_list[4])
Replace for i < len(explored): with for i in range(0, len(explored)):
That is not valid Python syntax. Actually, it's not a valid statement in any pseudocode, because you need a start value for i. Assuming that the said value is 0, you'll want:
def validate(self, testnode, explored):
if((testnode.wolf == testnode.sheep != testnode.farmer) or (testnode.sheep == testnode.cabbage != testnode.farmer)):
#return failure
return false
for i in range(len(explored)):
if testnode == explored[i]:
#return failure
return false
else: return true
Or, even better:
def validate(self, testnode, explored):
if((testnode.wolf == testnode.sheep != testnode.farmer) or (testnode.sheep == testnode.cabbage != testnode.farmer)):
#return failure
return false
for node in explored:
if testnode == node:
#return failure
return false
else: return true
Incidentally, there are some other issues with your code:
- replace
trueandfalsewithTrueandFalse testnode.wolf == testnode.sheep != testnode.farmerwon't behave in the way you think it will do, you should break that in two statements joined byand- avoid using
elsewithfor: it's tricky (non intuitive)
My 2 cents: don't learn Python the hard way, there are plenty of great, intuitive resources on the Internet. My favourite is http://pythonmonk.com/
A non-empty array A consisting of N integers is given.
Array A represents a linked list. A list is constructed from this array as follows:
the first node (the head) is located at index 0;
the value of a node located at index K is A[K];
if the value of a node is −1 then it is the last node of the list;
otherwise, the successor of a node located at index K is located at index A[K] (you can assume that A[K] is a valid index, that is 0 ≤ A[K] < N).
For example, for array A such that:
A[0] = 1 A[1] = 4 A[2] = -1 A[3] = 3 A[4] = 2
📷
the following list is constructed:
the first node (the head) is located at index 0 and has a value of 1;
the second node is located at index 1 and has a value of 4;
the third node is located at index 4 and has a value of 2;
the fourth node is located at index 2 and has a value of −1.
Write a function:
def solution(A)
that, given a non-empty array A consisting of N integers, returns the length of the list constructed from A in the above manner.
For example, given array A such that:
A[0] = 1 A[1] = 4 A[2] = -1 A[3] = 3 A[4] = 2
the function should return 4, as explained in the example above.
Assume that:
N is an integer within the range [1..200,000];
each element of array A is an integer within the range [−1..N-1];
it will always be possible to construct the list and its length will be finite.
I read this question and still don't understand why the order of list in that way. Would you please explain? Could not post a photo here to make it easier to understand. Thank you so much!
my_list = [1,2,3,4,5]
len(my_list)
# 5
The same works for tuples:
my_tuple = (1,2,3,4,5)
len(my_tuple)
# 5
And strings, which are really just arrays of characters:
my_string = 'hello world'
len(my_string)
# 11
It was intentionally done this way so that lists, tuples and other container types or iterables didn't all need to explicitly implement a public .length() method, instead you can just check the len() of anything that implements the 'magic' __len__() method.
Sure, this may seem redundant, but length checking implementations can vary considerably, even within the same language. It's not uncommon to see one collection type use a .length() method while another type uses a .length property, while yet another uses .count(). Having a language-level keyword unifies the entry point for all these types. So even objects you may not consider to be lists of elements could still be length-checked. This includes strings, queues, trees, etc.
The functional nature of len() also lends itself well to functional styles of programming.
lengths = map(len, list_of_containers)
The way you take a length of anything for which that makes sense (a list, dictionary, tuple, string, ...) is to call len on it.
l = [1,2,3,4]
s = 'abcde'
len(l) #returns 4
len(s) #returns 5
The reason for the "strange" syntax is that internally python translates len(object) into object.__len__(). This applies to any object. So, if you are defining some class and it makes sense for it to have a length, just define a __len__() method on it and then one can call len on those instances.