打印全局变量后,将其附加到for循环中的全局列表中。为什么每个for循环和列表中的值都不相同?

时间:2018-12-15 05:34:33

标签: python

transactions_clean = \
['Edith Mcbride', '$1.21', 'white', '09/15/17', 'Herbert Tran', 
'$7.29', 'white&blue', 
'09/15/17', 'Paul Clarke', '$12.52', 'white&blue', '09/15/17', 'Lucille 
Caldwell', '$5.13', 
'white', '09/15/17', 'Eduardo George', '$20.39', 'white&yellow', 
'09/15/17', 'Danny Mclaughlin', 
'$30.82', 'purple', '09/15/17', 'Stacy Vargas', '$1.85', 
'purple&yellow', '09/15/17', 'Shaun Brock', 
'$17.98', 'purple&yellow', '09/15/17', 'Erick Harper', '$17.41', 
'blue', '09/15/17', 'Michelle Howell', 
'$28.59', 'blue', '09/15/17', 'Carroll Boyd', '$14.51', 'purple&blue', 
'09/15/17', 'Teresa Carter', '$19.64', 
'white', '09/15/17', 'Jacob Kennedy', '$11.40', 'white&red', 
'09/15/17', 
'Craig Chambers', '$8.79', 'white&blue&red', 
'09/15/17', 'Peggy Bell', '$8.65', 'blue', '09/15/17', 'Kenneth 
Cunningham', '$10.53', 'green&blue', '09/15/17', 
'Marvin Morgan', '$16.49', 'green&blue&red', '09/15/17', 'Marjorie 
Russell', '$6.55', 'green&blue&red', '09/15/17', 
'Israel Cummings', '$11.86', 'black', '09/15/17', 'June Doyle', 
'$22.29', 'black&yellow', '09/15/17', 'Jaime Buchanan', 
'$8.35', 'white&black&yellow', '09/15/17', 'Rhonda Farmer', '$2.91', 
'white&black&yellow', '09/15/17', 'Darren Mckenzie', 
'$22.94', 'green', '09/15/17', 'Rufus Malone', '$4.70', 'green&yellow', 
'09/15/17', 'Hubert Miles', '$3.59', 
'green&yellow&blue', '09/15/17']


thread_sold = []
temp_thread = [[], []]
thread_sold1 =[]
for i in range(len(transactions_clean)):
  if i % 4 == 2:
    temp_thread[0] = transactions_clean[i]
    thread_sold1.append(transactions_clean[i])
  if i % 4 == 3:
    temp_thread[1] = transactions_clean[i]
    thread_sold1.append(transactions_clean[i])
    print(temp_thread)
    thread_sold.append(temp_thread)

print(thread_sold)
print(thread_sold1)

在(如果i%4 == 3)部分中,在打印temp_thread之后,我将temp_thread附加到thread_sold列表中。直观地,这两个语句中的temp_thread的值应该是相同的。

当我在(如果i%4 == 3 :)部分中打印temp_thread时,它将生成正确的输出,该输出在迭代过程中会发生变化。但是,当我打印thread_sold时,应该是所有temp_thread的列表,所有元素都变成同一件事。 thread_sold列表中的所有元素都变为['green&yellow&blue','09 / 15/17']。

有人可以告诉我为什么吗?

output:
['white', '09/15/17']
['white&blue', '09/15/17']
['white&blue', '09/15/17']
['white', '09/15/17']
['white&yellow', '09/15/17']
['purple', '09/15/17']
['purple&yellow', '09/15/17']
['purple&yellow', '09/15/17']
['blue', '09/15/17']
['blue', '09/15/17']
['purple&blue', '09/15/17']
['white', '09/15/17']
['white&red', '09/15/17']
['white&blue&red', '09/15/17']
['blue', '09/15/17']
['green&blue', '09/15/17']
['green&blue&red', '09/15/17']
['green&blue&red', '09/15/17']
['black', '09/15/17']
['black&yellow', '09/15/17']
['white&black&yellow', '09/15/17']
['white&black&yellow', '09/15/17']
['green', '09/15/17']
['green&yellow', '09/15/17']
['green&yellow&blue', '09/15/17']
[['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17'], ['green&yellow&blue', '09/15/17'], 
['green&yellow&blue', '09/15/17']]
['white', '09/15/17', 'white&blue', '09/15/17', 'white&blue', 
'09/15/17', 'white', '09/15/17', 'white&yellow', '09/15/17', 'purple', 
'09/15/17', 'purple&yellow', '09/15/17', 'purple&yellow', '09/15/17', 
'blue', '09/15/17', 'blue', '09/15/17', 'purple&blue', '09/15/17', 
'white', '09/15/17', 'white&red', '09/15/17', 'white&blue&red', 
'09/15/17', 'blue', '09/15/17', 'green&blue', '09/15/17', 
'green&blue&red', '09/15/17', 'green&blue&red', '09/15/17', 'black', 
'09/15/17', 'black&yellow', '09/15/17', 'white&black&yellow', 
'09/15/17', 'white&black&yellow', '09/15/17', 'green', '09/15/17', 
'green&yellow', '09/15/17', 'green&yellow&blue', '09/15/17']
[Finished in 0.0s]

2 个答案:

答案 0 :(得分:0)

这是指针的古老问题。您的代码反复向temp_thread添加一个指针,因此所有条目均反映最新值。我添加了两行将正确附加值(当前已注释掉)。

thread_sold = []
temp_thread = [[], []]
thread_sold1 =[]
for i in range(len(transactions_clean)):
    if i % 4 == 2:
        temp_thread[0] = transactions_clean[i]
        thread_sold1.append(transactions_clean[i])
        print('Clean', i, transactions_clean[i])
    if i % 4 == 3:
        temp_thread[1] = transactions_clean[i]
        thread_sold1.append(transactions_clean[i])
        print('Temp Thread', temp_thread)
        thread_sold.append(temp_thread) #Remove this line
        #thread_sold.append(temp_thread[0])
        #thread_sold.append(temp_thread[1])

temp_thread[0] = 'Well'
temp_thread[1] = 'Now'
print('First Thread')
print(thread_sold)
print('Second Thread')
print(thread_sold1)

答案 1 :(得分:0)

列表是可变的,因此每次执行此操作时:temp_thread [0]或temp_thread [1],您总是在更改同一List的值。由于它们是易变的,因此当您执行thread_sold.append(temp_thread)时,您只是向该列表添加了一个新指针,该指针在每次迭代时都会将其值更改为最近评估的值。

要真正轻松地解决此问题,可以更改

    if i % 4 == 3:
      temp_thread[1] = transactions_clean[i]
      thread_sold1.append(transactions_clean[i])
      print(temp_thread)
      thread_sold.append(temp_thread)

    if i % 4 == 3:
      temp_thread[1] = transactions_clean[i]
      thread_sold1.append(transactions_clean[i])
      print(temp_thread)
      t = tuple(temp_thread)
      thread_sold.append(t)

使用元组(因为它不是可变对象)将避免更新值的问题。