Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.9k views
in Technique[技术] by (71.8m points)

ValueError: invalid literal for int() with base 10: for map(int, list) in Python error

I am trying to learn the functionality of map() in python

Now when i do this,

bits = ['and', '2', '3', '1']
ports = map(int, bits)
#The below line is for the next iteration, where plus_net can be considered a counter
ports = [p+_plusnet for p in ports]

Now bits is a list here, And when I try to map() to iterate along ports, I get the error,

ValueError: invalid literal for int() with base 10: 'and'

Not sure how to handle this. Suggestions? Thanks !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
bits = ['and', '2', '3', '1']

def myint(a):
    if a.isdigit():
        return int(a)
    else:
        return 0
ports = map(myint, bits)
ports = [p+_plusnet for p in ports]

if you want to replace string litral


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...