簡體   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]

我想知道列表的第 N 個數字...

例子:

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

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

不需要是一個函數......只是解決問題的方法:函數返回列表的第n位數字

您可以將列表轉換為 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))

印刷:

3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM