|
Technical Interview Questions
C++ Interview Questions
Php Interview Questions
Xml
Interview Questions
C Interview Questions
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Python Interview Questions and Answers
How do you remove duplicates from a list?
If you don't mind reordering the list, sort it and then
scan from the end of the list, deleting duplicates as
you go:
if List:
List.sort()
last = List[-1]
for i in range(len(List)-2, -1, -1):
if last==List[i]: del List[i]
else: last=List[i]
If all elements of the list may be used as dictionary
keys (i.e. they are all hash able) this is often faster
d = {}
for x in List: d[x]=x
List = d.values()
How do you make an array in Python?
Use a list:
["this", 1, "is", "an", "array"]
Lists are equivalent to C or Pascal arrays in their time
complexity; the primary difference is that a Python list
can contain objects of many different types.
The array module also provides methods for creating
arrays of fixed types with compact representations, but
they are slower to index than lists. Also note that the
Numeric extensions and others define array-like
structures with various characteristics as well.
To get Lisp-style linked lists, you can emulate cons
cells using tuples:
lisp_list = ("like", ("this", ("example", None) ) )
If mutability is desired, you could use lists instead of
tuples. Here the analogue of lisp car is lisp_list[0]
and the analogue of cdr is lisp_list[1]. Only do this if
you're sure you really need to, because it's usually a
lot slower than using Python lists.
How do I create a multidimensional list?
You probably tried to make a multidimensional array like
this:
A = [[None] * 2] * 3
This looks correct if you print it:
>>> A
[[None, None], [None, None], [None, None]]
But when you assign a value, it shows up in multiple
places:
>>> A[0][0] = 5
>>> A
[[5, None], [5, None], [5, None]]
The reason is that replicating a list with * doesn't
create copies, it only creates references to the
existing objects. The *3 creates a list containing 3
references to the same list of length two. Changes to
one row will show in all rows, which is almost certainly
not what you want.
The suggested approach is to create a list of the
desired length first and then fill in each element with
a newly created list:
A = [None]*3
for i in range(3):
A[i] = [None] * 2
This generates a list containing 3 different lists of
length two. You can also use a list comprehension:
w,h = 2,3
A = [ [None]*w for i in range(h) ]
Or, you can use an extension that provides a matrix
datatype; Numeric Python is the best known.
How do I apply a method to a sequence of objects?
Use a list comprehension:
result = [obj.method() for obj in List]
More generically, you can try the following function:
def method_map(objects, method, arguments):
"""method_map([a,b], "meth", (1,2)) gives [a.meth(1,2),
b.meth(1,2)]"""
nobjects = len(objects)
methods = map(getattr, objects, [method]*nobjects)
return map(apply, methods, [arguments]*nobjects)
I want to do a complicated sort: can you do a
Schwartzman Transform in Python?
Yes, it's quite simple with list comprehensions.
The technique, attributed to Randal Schwartz of the Perl
community, sorts the elements of a list by a metric
which maps each element to its "sort value". To sort a
list of strings by their uppercase values:
tmp1 = [ (x.upper(), x) for x in L ] # Schwartzman
transform
tmp1.sort()
Usorted = [ x[1] for x in tmp1 ]
To sort by the integer value of a subfield extending
from positions 10-15 in each string:
tmp2 = [ (int(s[10:15]), s) for s in L ] # Schwartzman
transform
tmp2.sort()
Isorted = [ x[1] for x in tmp2 ]
Note that Isorted may also be computed by
def intfield(s):
return int(s[10:15])
def Icmp(s1, s2):
return cmp(intfield(s1), intfield(s2))
Isorted = L[:]
Isorted.sort(Icmp)
but since this method calls intfield() many times for
each element of L, it is slower than the Schwartzman
Transform.
Page Numbers : 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
HTML Interview
Questions for more HTML Interview Questions with Answers
Check
Job Interview Questions
for more Interview Questions with Answers
|