Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 List items verification for Integer type
#1
Hi All,

I want to verify if the list objects are integers are not. My code is failing at 0 index, when there is a float value or when there is a string value.

Please help.


num=0
z=['11, 12, 14', '20', '21, 23', '24', 'man', '1', '27', '28', '29.1', '30']
mydefectlist=[]
mydefectlist1=[]
mydefectlist2=[]

while num < len(z):
    z[num] = int(z[num])
    num +=1

for s in z:
    if (isinstance(s, int)):
        flag=0
        mydefectlist.append(s)
        print('int values:',mydefectlist)
    elif(isinstance(s, float)):
        mydefectlist1.append(s)
        print('float values:',mydefectlist1)
    elif(isinstance(s, str)):
        mydefectlist2.append(s)
        print('string values:',mydefectlist2)
    else:
        print('please check the list again', s)
Quote
#2
As far as I understand there is conversion to int, not verification.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
Try this to see if that helps,

list1=['man12','11', '12', '14', '20', '21', '23', '24', 'man', '1', '27', '28', '29.1', '30']
mydefectlist=[]
mydefectlist1=[]
mydefectlist2=[]
 
for item in list1:
	try:
		if (isinstance(int(item), int)):
			mydefectlist.append(item)
	except ValueError:
		try:
			if (isinstance(float(item), float)):
				mydefectlist1.append(item)
		except ValueError:
			mydefectlist2.append(item)
		
print("int-->", mydefectlist)
print("float-->", mydefectlist1)
print("str-->", mydefectlist2)

Output:
python test1.py int--> ['11', '12', '14', '20', '21', '23', '24', '1', '27', '28', '30'] float--> ['29.1'] str--> ['man12', 'man']
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
vintysaw likes this post
Quote
#4
Thanks Sandeep. This works for me. I guess, I was missing to use try-except. The ifelse logic wasn't working for me.
Quote
#5
One way (which does not require to define possible classes in chain of if-s) is to use defaultdict and class name:

>>> import collections
>>> lst = ['abc', 1, 2, 3, 4.3, [42]] 
>>> types = collections.defaultdict(list)
>>> for item in lst: 
...     types[type(item).__name__].append(item)
...
>>> types
defaultdict(list, {'str': ['abc'], 'int': [1, 2, 3], 'float': [4.3], 'list': [[42]]})
>>> for k, v in types.items(): 
...     print(f'{k} --> {v}') 
...                                                                 
str --> ['abc']
int --> [1, 2, 3]
float --> [4.3]
list --> [[42]]
>>> types['int']                                                    
[1, 2, 3]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Select the other of 2 items in a list Clunk_Head 7 309 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Removing items from list if containing a substring pythonnewbie138 2 245 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Print the number of items in a list on ubuntu terminal buttercup 2 317 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  How to put the items of one list in new generated lists Bobbear 1 240 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 460 May-10-2020, 05:28 AM
Last Post: deanhystad
  mydict.items() is not a list Skaperen 2 320 May-07-2020, 07:03 AM
Last Post: DeaD_EyE
  Add items from one list to list of lists PUP280 5 731 May-05-2020, 03:47 PM
Last Post: PUP280
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 606 Feb-21-2020, 02:40 PM
Last Post: Cosmosso
  counting items in a list of number combinations Dixon 2 383 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Type hinting - return type based on parameter micseydel 2 411 Jan-14-2020, 01:20 AM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)