1.空格转逗号
在我们编程时候,有时候会遇到将一个程序里边的结果放到另一个程序中,就比如这样:
[1 5 6 5 2 3 7 0 2 6 3 0 1 2 2 3 0 2 1 7 4 6 2 7 7 4 6 6 7 0 0 6 4 7 7 3 1 1 0 4 4 0 1 1 5 4 4 4 1 4 7 1 4 0 0 7 4 6 6 0 6 3 6 1 5 5 3 5 5 6 1 2 2 2 3 5 2 3 3 7 7 4 3 3 7 0 1 2 2 5 6 5 5 0 3 3 1 2 5 0]#输出 #但是我们要的列表肯定是中间逗号,是这样 [1, 5, 6, 5, 2, 3, 7, 0, 2, 6, 3, 0, 1, 2, 2, 3, 0, 2, 1, 7, 4, 6, 2, 7, 7, 4, 6, 6, 7, 0, 0, 6, 4, 7, 7, 3, 1, 1, 0, 4, 4, 0, 1, 1, 5, 4, 4, 4, 1, 4, 7, 1, 4, 0, 0, 7, 4, 6, 6, 0, 6, 3, 6, 1, 5, 5, 3, 5, 5, 6, 1, 2, 2, 2, 3, 5, 2, 3, 3, 7, 7, 4, 3, 3, 7, 0, 1, 2, 2, 5, 6, 5, 5, 0, 3, 3, 1, 2, 5, 0]
怎么办呢?一个一个改?反正我不那么做,哈哈。
方法1 isspace方法(适合1位数):
a = "1 5 6 5 2 3 7 0 2 6 3 0 1 2 2 3 0 2 1 7 4 6 2 7 7 4 6 6 7 0 0 6 4 7 7 3 1 1 0 4 4 0 1 1 5 4 4 4 1 4 7 1 4 0 0 7 4 6 6 0 6 3 6 1 5 5 3 5 5 6 1 2 2 2 3 5 2 3 3 7 7 4 3 3 7 0 1 2 2 5 6 5 5 0 3 3 1 2 5 0" b = [] for i in a: if i.isspace():#如果是空格 i = ',' else: b.append(int(i)) print(b,type(b))
方法2 print
a = "1 5 6 5 2 3 7 0 2 6 3 0 1 2 2 3 0 2 1 7 4 6 2 7 7 4 6 6 7 0 0 6 4 7 7 3 1 1 0 4 4 0 1 1 5 4 4 4 1 4 7 1 4 0 0 7 4 6 6 0 6 3 6 1 5 5 3 5 5 6 1 2 2 2 3 5 2 3 3 7 7 4 3 3 7 0 1 2 2 5 6 5 5 0 3 3 1 2 5 0" for i in a: if not i.isspace(): print(i,end = ',')
方法3 split方法(实用性更广):
b = a.split() c = [] for i in b: c.append(int(i)) print(c)
2.去掉array的[]:
#ps是一个列表,我们需要求出其中第一个大于0.9的元素的下标,但是最后结果是:[数字] #我们需要:数字 import numpy as np ps_array = np.array(ps) confidence_indexs = np.argwhere(ps_array > 0.9) print(confidence_indexs) confidence_index = confidence_indexs[0] print("数据点数应大于{}时候,置信度为90%".format( list(confidence_index)[0] ))