Lists in Python 2.7 (compatible with 3.x) -


i have code, in code, i'm asking ip address (it can random, since code show, in other words, doesn't anything), split using "." delimeter, , want check, has 4 segments, ip has. don't want check contents of segments, want check, if can real ip (i know there more 1 conditions validity of ip let's keep simple).

how can check, list has 4 "elements", without tripping off python error. can supply part of code in question, if required.

edit 1: thank guys quick answer. here's code asked for, changed, , not working (the previous version didn't either)(i imported fnmatch fnmatch , functions defined):

if fnmatch(list.split("."), "[*,*,*,*]"):     success() else:     fail() 

i'm trying something, call success() when list has 4 items, , fail() if doesn't.

you need little more debugging here. instance, check split gives want. second, please read https://stackoverflow.com/help/mcve -- lists our expectations posting.

giving actual input , error message have given answer sooner: fed list fnmatch, expects string. you're on right track, can more simply.

def success():     print "good"  def fail():     print "bad"  list = "10.10.10.127" fields = list.split(".")  print fields  if len(fields) == 4:     success() else:     fail() 

do need check each field entirely numeric?


Comments