top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: How does max () method work with the list that containing different types of elements ?

+1 vote
483 views

A list can contain different types of elements but I am not able to understand how max () method work with different types of data elements.

posted Apr 16, 2016 by Ganesh Kumar

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
+1 vote

I want to find the maximal number of elements contained in a nested dictionary, e.g.

data = {
 'violations':
 {
 'col1': {'err': [elem1, elem2, elem3]},
 'col2': {'err': [elem1, elem2]}
 }
 }

so to find the maximal number of elements in the lists for key 'err' in key 'col1' and 'col2'. Also key 'violations' may contain many keys (e.g. 'col1' , 'col2', 'col3' etc), so what's the best way to do this (using a loop)?

max = 0for col in data.violations:
 if max < len(data.violations.col.err):
 max = len(data.violations.col.err)
0 votes

I can create a list that has repeated elements of another list as follows:

xx = ["a","b"]
nrep = 3
print xx
yy = []
for aa in xx:
 for i in range(nrep):
 yy.append(aa)
print yy

output:

['a', 'b']
['a', 'a', 'a', 'b', 'b', 'b']

Is there a one-liner to create a list with repeated elements?

+2 votes

I saw three different types of quotes in python. For me all gave same result.
Can anyone explain what could be the reason of having three different quotes in python and which one is used in what circumstances ?

+2 votes

I would like to apply the Pool.map method to a member of a class. Here is a small example that shows what I would like to do:

from multiprocessing import Pool

class A(object):
 def __init__(self,x):
 self.value = x
 def fun(self,x):
 return self.value**x

l = range(10)

p = Pool(4)

op = p.map(A.fun,l)

using this with the normal map doesn't cause any problem

This fails because it says that the methods can't be pickled. (I assume it has something to do with the note in the documentation: "functionality within this package requires that the __main__ module be importable by the children.", which is obscure to me).

I would like to understand two things: why my code fails and when I can expect it to fail? what is a possible workaround?

...