[英]Python Loop and Append to String
我有一個字符串字段和一個數字數組。 當我遍歷列表時,我應該使用數組中的數字獲取到 append 的字符串。
目前,它只返回數組的字符串+當前position。 以下是我的代碼。 我怎么能解決這個問題?
digit_list = list(map(int, str(extra_digits)))
pi_local = "PI"
for digit in range(len(digit_list)):
pi_local = pi_local + str(digit_list[counter])
pi_label.config(text = pi_local)
我通過嘗試迭代列表嘗試了以下建議,但我仍然沒有得到正確的結果。 完整代碼如下
pi = "PI"
extra_digits = "159265358979323846"
counter = 0
init = 2
#digit_list = list(map(int, str(extra_digits)))
def button_pressed():
global counter
global pi
#global digit_list
global init
digit_list = list(map(int, str(extra_digits)))
our_label.config(text="Pi to " + str(init+counter) + " decimals")
pi_local = pi
for digit in digit_list:
pi_local = pi_local + str(digit)
pi_label.config(text = str(digit))
#pi_label.config(text = str(pi) + str(digit_list[counter]))
counter = counter + 1
In [77]: pi_local = "PI"
In [78]: digit_list = [1,3,5,2]
In [79]: [str(i) + pi_local for i in digit_list]
Out[79]: ['1PI', '3PI', '5PI', '2PI']
您需要遍歷digit_list
中的元素
digit_list = list(map(int, str(extra_digits)))
pi_local = "PI"
for digit in digit_list:
pi_local = pi_local + str(digit)
pi_label.config(text = pi_local)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.