Separating List Items in Python

Given the list (listEx) in the following code, I am trying to separate the string and integer and float types and put them all in my own lists. If I want to extract lines only from listEx, the program should go through listEx and put the lines in a new strList, and then print it to the user. Similarly for integer and float types. But if I can figure out the right way to do just one, I will be fine for the rest. Still no luck, for an hour now.

listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55] strList=['bcggg'] for i in listEx: if type(listEx) == str: strList = listEx[i] print strList[i] if i not in listEx: break else: print strList for i in strList: if type(strList) == str: print "This consists of strings only" elif type(strList) != str: print "Something went wrong" else: print "Wow I suck" 
+4
source share
5 answers
 integers = filter(lambda x: isinstance(x,int), listEx) strings = filter(lambda x: isinstance(x,str), listEx) 

etc.

+2
source

Just change type(strList) and type(listEx) to type(i) . You iterate over the list, but then check to see if the list is a string and not whether this element is a string.

+2
source

Perhaps instead of if type(item) == ... use item.__class__ so that item tells you its class.

 import collections listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55] oftype = collections.defaultdict(list) for item in listEx: oftype[item.__class__].append(item) for key, items in oftype.items(): print(key.__name__, items) 

gives

 int [1, 2, 3, 55] str ['moeez', 'string', 'another string'] float [2.0, 2.345] 

Thus, the three lists you are looking for can be obtained as oftype[int] , oftype[float] and oftype[str] .

+2
source

Python for loops iterates over actual object references. You can observe strange behavior partly because you give a reference to object i, where the index of the number list should be indicated (the listEx[i] operator does not make sense. Array indices can be values ​​i = 0 ... length_of_list, but for one point I = "moeez")

You also replace the entire list every time you find an element ( strList = listEx[i] ). Instead, you can add a new element to the end of the list using strList.append(i) , but here's a shorter and slightly simpler option, which creates the entire list on one line using a very useful python construct called list .

 listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55] strList = [ i for i in listEx if type(i) == str ] 

gives:

 print strList >>> print strList ['moeez', 'string', 'another string'] 

For the rest

 >>> floatList = [ i for i in listEx if type(i) == float ] >>> print floatList [2.0, 2.345] >>> intList = [ i for i in listEx if type(i) == int ] >>> intList [1, 2, 3, 55] >>> remainders = [ i for i in listEx if ( ( i not in strList ) and (i not in floatList ) and ( i not in intList) ) ] >>> remainders [] 
+1
source
  python 3.2 listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55] strList = [ i for i in listEx if type(i) == str ] ## this is list comprehension ## ### but you can use conventional ways. strlist=[] ## create an empty list. for i in listex: ## to loop through the listex. if type(i)==str: ## to know what type it is strlist.append(i) ## to add string element print(strlist) or: strlist=[] for i in listex: if type(i)==str: strlist=strlist+[i] print(strlist) 
-1
source

Source: https://habr.com/ru/post/1445675/


All Articles