Welcome, guest | Sign In | My Account | Store | Cart
    def flattenlist(L):
        import types
        WhiteTypes = ('StringType', 'UnicodeType', 'StringTypes', 'ListType',
                  'ObjectType', 'TupleType')
        BlackTypes= tuple( [getattr(types, x) for x in dir(types)
                      if not x.startswith('_')
                      and x not in whites] )

        tmp = []
        def core(L):
            if  not hasattr(L,'__iter__'):
                return [L]
            else :
                for i in L:
                    if isinstance(i,BlackTypes):
                        tmp.append(i)
                        continue
                    if type(i) == type(str()):
                        tmp.append(i)
                    else:
                        core(i)
            return tmp
        return core(L)




#Examples x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['x']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
>>> flattenlist(x)
['x']
>>> x=(((((1)))))
>>> flattenlist(x)
[1]
>>> x=[(),(),[]]
>>> flattenlist(x)
[]
>>> x=[(1),('1'),[1.0]]
>>> flattenlist(x)
[1, '1', 1.0]
>>> x=[[[[[[(((([1,1]))))]]]]]]
>>> flattenlist(x)
[1, 1]
>>> x=1
>>> flattenlist(x)
[1]
>>> x=flattenlist
>>> flattenlist(x)
[<function flattenlist at 0x16a8320>]
>>> 

Diff to Previous Revision

--- revision 2 2010-06-12 07:11:56
+++ revision 3 2010-06-18 03:45:20
@@ -1,8 +1,8 @@
     def flattenlist(L):
         import types
-        whites = ('StringType', 'UnicodeType', 'StringTypes', 'ListType',
+        WhiteTypes = ('StringType', 'UnicodeType', 'StringTypes', 'ListType',
                   'ObjectType', 'TupleType')
-        Types= tuple( [getattr(types, x) for x in dir(types)
+        BlackTypes= tuple( [getattr(types, x) for x in dir(types)
                       if not x.startswith('_')
                       and x not in whites] )
 
@@ -12,7 +12,7 @@
                 return [L]
             else :
                 for i in L:
-                    if isinstance(i,Types):
+                    if isinstance(i,BlackTypes):
                         tmp.append(i)
                         continue
                     if type(i) == type(str()):
@@ -25,7 +25,7 @@
 
 
 
-#Examples x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['x']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
+#Examples x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['x']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
 >>> flattenlist(x)
 ['x']
 >>> x=(((((1)))))

History