简体   繁体   English

如何在python中返回列表的第N位数字?

[英]How can i return the Nth digit of a list in python?

X = [1,4,5,10,23,2,5,7,19]

I want to know the digit Nth of the list...我想知道列表的第 N 个数字...

Example:例子:

def func(n):
    print(nth digit of X)

def func(7):
print(7th digit of X)
Output = 3

Don't need to be a function...just the method to reach to solution of the problem: function returns nth digit of a list不需要是一个函数......只是解决问题的方法:函数返回列表的第n位数字

You can convert the list to a str, and then the indexing would work您可以将列表转换为 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]

def func(x, n):
    return ''.join(map(str, x))[n-1]

print(func(x, 7))

Prints:印刷:

3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM