Python Forum
List items verification for Integer type
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)
Reply
#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.
Reply
#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
Reply
#4
Thanks Sandeep. This works for me. I guess, I was missing to use try-except. The ifelse logic wasn't working for me.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 2,822 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Finding combinations of list of items (30 or so) LynnS 1 1,495 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  search a list or tuple for a specific type ot class Skaperen 8 3,467 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  For Word, Count in List (Counts.Items()) new_coder_231013 6 6,474 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 3,598 May-26-2022, 01:37 PM
Last Post: Mark17
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 3,529 May-07-2022, 08:40 AM
Last Post: ibreeden
  unsupported operand type(s) for %: 'list' and 'int' RandomCoder 4 35,137 May-07-2022, 08:07 AM
Last Post: menator01
  how to assign items from a list to a dictionary CompleteNewb 3 2,618 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Reading list items without brackets and quotes jesse68 6 6,676 Jan-14-2022, 07:07 PM
Last Post: jesse68
Question How to gather specific second-level items from a list chatguy 2 2,261 Dec-17-2021, 05:05 PM
Last Post: chatguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020