我如何 return python 中列表的第 N 个数字?
How can i return the Nth digit of a list in python?
X = [1,4,5,10,23,2,5,7,19]
我想知道列表的第 N 位...
示例:
def func(n):
print(nth digit of X)
def func(7):
print(7th digit of X)
Output = 3
不需要是函数...只是达到解决问题的方法:函数returns第n位列表
x = [1,4,5,10,23,2,5,7,19]
def func(x, n):
return ''.join(map(str, x))[n-1]
print(func(x, 7))
打印:
3
您可以将列表转换为 str,然后索引就可以工作了
x_str = ''.join([str(i) for i in x]) # x_str = "145102325719"
# x_str[idx] would return the digit
X = [1,4,5,10,23,2,5,7,19]
我想知道列表的第 N 位...
示例:
def func(n):
print(nth digit of X)
def func(7):
print(7th digit of X)
Output = 3
不需要是函数...只是达到解决问题的方法:函数returns第n位列表
x = [1,4,5,10,23,2,5,7,19]
def func(x, n):
return ''.join(map(str, x))[n-1]
print(func(x, 7))
打印:
3
您可以将列表转换为 str,然后索引就可以工作了
x_str = ''.join([str(i) for i in x]) # x_str = "145102325719"
# x_str[idx] would return the digit