首页 > 解决方案 > 基数为 10 的 int() 的无效文字:'(1,2,3,4,5,6,7)'

问题描述

tuple1 = int(input())
list1 = list()
for i in tuple1:
    if tuple1[i-1] % 2 == 0:
        list1.append(tuple1[i-1])
tuple2 = tuple(list1)
print(tuple2)

我收到错误

基数为 10 的 int() 的无效文字:'(1,2,3,4,5,6,7)'

标签: python

解决方案


您可以使用literal_evalfromast模块尝试将输入转换为 a tuple,例如

from ast import literal_eval

s = '(1,2,3,4,5,6,7)' # your input...
t = literal_eval(s)

print(t, type(t))
# (1, 2, 3, 4, 5, 6, 7) <class 'tuple'>

推荐阅读