运行代码时,我得到一个包含
<class 'str'>
GRV-DAL-7777-05/28/2019_23:03:55-PT01
GRV-DAL-7777-05/28/2019_23:03:55-PT01
but expect
a contains <class 'str'>
GRV-DAL-7777-05/28/2019_23:03:55-PT01
DAL-7777-05/28/2019_23:03:55-PT01
我想念什么?在我通过函数信息运行列表b时仍如何传递?我觉得这是我不了解的基本python内容。
我已将其保存到多个不同的列表名称。
import pandas as pd
#Import data from spread sheet
info = pd.read_csv('information.csv', delimiter = ',')
info['Data1'] #as a series
a =info['Data1'].values #as a numpy array
a.tolist()
print("a contains ", type(a[1]))
#
#Put data into a list
#for each item in the list
x = []
y = []
#j is a junk list to throw away
j = []
b = []
c = []
def remove_head_of_string(g, h, t, p):
#g is list to iterate over #h is list name to save the heads to
#t is list name to save tails to # p is location to partition
for i in g:
head, sep, tail = a[1].partition(p)
h.append(head)
t.append(tail)
remove_head_of_string(a, x, b, "-")
remove_head_of_string(b, j, c, "-")
#print(b)
print(b[0])
print(c[0])
我希望: 包含
<class 'str'>
GRV-DAL-7777-05/28/2019_23:03:55-PT01
DAL-7777-05/28/2019_23:03:55-PT01
但是我得到了: 包含
<class 'str'>
GRV-DAL-7777-05/28/2019_23:03:55-PT01
GRV-DAL-7777-05/28/2019_23:03:55-PT01
答案 0 :(得分:2)
因此,您似乎在循环中专门使用了变量“ a”,而不是要尝试传递的值。
for i in g:
head, sep, tail = a[1].partition(p) # <--- a on this line
h.append(head)
t.append(tail)
我怀疑您打算这样做:
for i in g:
head, sep, tail = i[1].partition(p) # <--- i on this line
h.append(head)
t.append(tail)