Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

mysql - Values are overriding to same values in python lists when I use append

my program:

values = []
for i in 10:
  values.append({0,1,2,3,4,i})     

the result is

  values[0] = [0,1,2,3,4]  
  values[1] = [0,1,2,3,4]  
  values[2] = [0,1,2,3,4] 

but the result should be :

values[0] = [0,1,2,3,4,0]
values[1] = [0,1,2,3,4,1]
values[2] = [0,1,2,3,4,2]

in each sub list the same values is repeated only once.. this array i.e values[0] is to use in python

  str = """INSERT INTO tbl_user_custom_adjustments (col1, col2, col3,col4,col5, col6) VALUES {}"""

  str=str.format(tuple(data))

the result query is :

   INSERT INTO tbl_user_custom_adjustments (col1,col2,col3,col4,col5,col6) VALUES (0,1,2,3,4)

but the expected result should be:

    INSERT INTO tbl_user_custom_adjustments (col1,col2,col3,col4,col5,col6) VALUES (0,1,2,3,4,0)
question from:https://stackoverflow.com/questions/66063416/values-are-overriding-to-same-values-in-python-lists-when-i-use-append

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you are saving them in a set() which takes only unique values, change them to []. Also use range not only 10 like

values = []
for i in range(0,10):
  values.append([0,1,2,3,4,i]) 
print(values)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...