如何将此列表转换为没有' - '在它里面它们都是整数?
List1 = ['978-0262133838','978-0262201-629','978-0321758927']
所以列表将类似于
List1 = [9780262133838, 9780262201629, 9780321758927]
答案 0 :(得分:0)
使用.replace('-', '')
的简单列表理解:
List1 = ['978-0262133838','978-0262201-629','978-0321758927']
print([x.replace('-','') for x in List1])
# => ['9780262133838', '9780262201629', '9780321758927']
答案 1 :(得分:0)
实现这一目标的大多数Pythonic方法是使用map
lambda
函数(用''替换''然后将其转换为int
):
>>> my_list = ['978-0262133838','978-0262201-629','978-0321758927']
>>> map(lambda x: int(x.replace('-', '')), my_list)
[9780262133838, 9780262201629, 9780321758927]
答案 2 :(得分:0)
也许这会有所帮助。如果您的清单是:
List1 = ['978-0262133838','978-0262201-629','978-0321758927']
您可以像这样使用for循环,并创建一个新列表来保留新数字:
List2=[] #it is very important to have this list outside the for loop
For number in List1: #This does exactly what it says, you get the first number in the list, then you get the second and so on...
number=number.replace("-","") # I use "" to refer strings, that's just how i learned.
List2.append[int(number)]
如果变量号只是List1,那么只需输入
即可List1=List2
希望它有所帮助,祝你好运:D