簡體   English   中英

為什么我無法訪問在我的方法中生成的這個列表?

[英]Why can't I access this list generated in my method?

我正在嘗試編寫一種方法,將給定的時間轉換為其數字(以便將整數從小時輕松轉換為分鍾)。 當我在方法中打印生成的列表時,它工作得很好。

但是,當我嘗試從方法外部訪問 first_two_digits_list 或 last_two_digits_list 時,它無法這樣做(即使使用全局聲明)。

有人可以向我解釋為什么會發生這種情況,因為我不知道。 另外,如果有人能幫我找到解決方案或其他方式來訪問此列表,那就太完美了!

times_list = [["11:00","12:00"], ["12:00","13:00"]]

# Method that converts list into sections of digits (for ease of integer use)
def ConvertToDigits():
    global first_value, second_value, third_value, fourth_value, last_two_digits_list

    # initial counter for while loop
    n = 0

    # two new lists of digits
    first_two_digits_list = []
    last_two_digits_list = []

    while n < len(times_list):
        first_digit_value = ((((times_list[n])[0])[0]) + (((times_list[n])[0])[1]))
        second_digit_value = ((((times_list[n])[1])[0]) + (((times_list[n])[1])[1]))

        third_digit_value = ((((times_list[n])[0])[3]) + (((times_list[n])[0])[4]))
        fourth_digit_value = ((((times_list[n])[1])[3]) + (((times_list[n])[1])[4]))

        first_two_digits_list.append([(first_digit_value), (second_digit_value)])
        last_two_digits_list.append([(third_digit_value), (fourth_digit_value)])

        n+= 1

在您的代碼中, first_two_digits_listlast_two_digits_list不是全局變量。 它們是函數ConvertToDigits局部變量。 但是, times_list是一個全局變量,因為它是在函數外部定義的,這就是您可以在ConvertToDigits訪問它的原因。 解決您的問題的一個簡單方法是,

times_list = [["11:00","12:00"], ["12:00","13:00"]]
# two new lists of digits
first_two_digits_list = []
last_two_digits_list = []
# Method that converts list into sections of digits (for ease of integer use)
def ConvertToDigits():
    # initial counter for while loop
    n = 0
    while n < len(times_list):
        first_digit_value = ((((times_list[n])[0])[0]) + (((times_list[n])[0])[1]))
        second_digit_value = ((((times_list[n])[1])[0]) + (((times_list[n])[1])[1]))
        third_digit_value = ((((times_list[n])[0])[3]) + (((times_list[n])[0])[4]))
        fourth_digit_value = ((((times_list[n])[1])[3]) + (((times_list[n])[1])[4]))
        first_two_digits_list.append([(first_digit_value), (second_digit_value)])
        last_two_digits_list.append([(third_digit_value), (fourth_digit_value)])
        n+= 1
ConvertToDigits()
print(first_two_digits_list,last_two_digits_list)

輸出

[['11', '12'], ['12', '13']] [['00', '00'], ['00', '00']]

從評論中跟進

另一種方法:

times_list = [["11:00","12:00"], ["12:00","13:00"]]
# Method that converts list into sections of digits (for ease of integer use)
def ConvertToDigits():
    # initial counter for while loop
    global first_two_digits_list, last_two_digits_list
    first_two_digits_list = []
    last_two_digits_list = []
    n = 0
    while n < len(times_list):
        first_digit_value = ((((times_list[n])[0])[0]) + (((times_list[n])[0])[1]))
        second_digit_value = ((((times_list[n])[1])[0]) + (((times_list[n])[1])[1]))
        third_digit_value = ((((times_list[n])[0])[3]) + (((times_list[n])[0])[4]))
        fourth_digit_value = ((((times_list[n])[1])[3]) + (((times_list[n])[1])[4]))
        first_two_digits_list.append([(first_digit_value), (second_digit_value)])
        last_two_digits_list.append([(third_digit_value), (fourth_digit_value)])
        n+= 1
ConvertToDigits()
print(first_two_digits_list,last_two_digits_list)

輸出

[['11', '12'], ['12', '13']] [['00', '00'], ['00', '00']]

暫無
暫無

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

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