Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.0k 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...