简体   繁体   中英

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...

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

You can convert the list to a str, and then the indexing would work

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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