import pandas as pd id_value = 0 l=[] for i in range(8): if i < 3: id_value = 1, elif 3 <= i <6: id_value = 2, elif 6 <= i < 9: id_value = 3, elif 9 <= i <12: id_value = 4 elif 12 <= i < 16: id_value = 5 l.append(id_value) df = pd.DataFrame(l) print(df) df.to_csv('static/data/aaa.csv',index=False) print('Save Successful!')
1: 为什么上述if判断后的变量id_value值变成tuple类型了???
2: 而将上述代码的range()扩大为20000,判断条件也加大了之后,改成下面这样:前面的12000判断出的id_value依旧是tuple类型,但是12000之后的判断出的结果又莫名其妙变成了int类型???
3: 我该如何才能避免产生tuple类型的这个bug ???
import pandas as pd id_value = 0 l=[] for i in range(20000): if i < 4000: id_value = 1, elif 4000 <= i <8000: id_value = 2, elif 8000 <= i < 12000: id_value = 3, elif 12000 <= i <16000: id_value = 4 elif 16000 <= i < 20000: id_value = 5 l.append(id_value) df = pd.DataFrame(l) print(df) df.to_csv('static/data/aaa.csv',index=False) print('Save Successful!')