如何在嵌套列表中将元素添加在一起?
How to add elements together in nested list?
我正在尝试将来自 .csv 文件的嵌套列表中的人员的姓氏和名字加在一起。我的数据如下所示:
Picture of data spreadsheet
我的列表 peopleNames 具有以下当前输出:
[['Barreuther', 'Mark', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['Demaio', 'Daniel', 'Certo', 'Catherine', 'Frankel', 'Stewart', 'Levesque', 'Aime', 'Mahan', 'Eric J.', 'Rosiene', ' Pe', 'Haruta', 'Mako E.', '', '']... many more lists]
我想把相邻的两个字符串加在一起。前任。 [['Barreuther Mark']]。我试过以下代码:
def getInfo():
"""Open csv, read it, get nested list, separate names and schools, return two lists"""
#
with open("CT_schools.csv") as f:
reader = csv.reader(f)
data = []
#start for
for row in reader:
data.append(row)
#end for
schoolNames = []
peopleNames = []
#start for
for i in range(len(data)):
schoolNames.append(data[i][0])
peopleNames.append(data[i][1:])
#end for
index = 0
name = 0
NewpeopleNames = []
#start while
while index < len(peopleNames):
for i in range(len(peopleNames)):
fullName = peopleNames[index][i] + " " + peopleNames[index][i+1]
NewpeopleNames.append(fullName)
index = index + 1
name = name + 2
我得到以下输出:
[' ', 'Pe Haruta', 'Ronald Golbazi', 'Christoph Raskin', ' ', ' ', 'Barry Oliver', ' ', ' ', ' ', ' ', 'Douglad M. ']
然后我用嵌套的 for 循环替换了 while 循环:
#start nested for
for index in range(len(peopleNames)):
for name in range(len(peopleNames[index])):
fullName = peopleNames[index][name] + peopleNames[index][name + 1]
然后我收到一条错误消息,指出它不在列表的索引中。您认为有人可以尝试为我指明正确的方向吗?非常感谢!
嵌套 for 循环是天真的解决方案(如果您不了解代码的复杂性)。
我建议先过滤掉列表中的所有空值,然后才引用 "add the two strings that are next to each other together"。
NewPeopleNames = list()
for sub_list in peopleNames:
tmp_list = list()
filtered_list = list(filter(None, sub_list))
for i in range(0, len(filtered_list), 2):
if i+1 >= len(filtered_list):
print('There is no last name for \'{0}\''.format(filtered_list[i]))
continue
tmp_list.append('{0} {1}'.format(filtered_list[i], filtered_list[i+1]))
NewPeopleNames.append(tmp_list)
请注意,奇数列表大小(名字没有姓氏)有一点 "protection"。
您可以修改打印行以仅将姓名 (filtered_list[i]) 附加到 NewPeopleNames 列表。
我正在尝试将来自 .csv 文件的嵌套列表中的人员的姓氏和名字加在一起。我的数据如下所示: Picture of data spreadsheet
我的列表 peopleNames 具有以下当前输出:
[['Barreuther', 'Mark', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['Demaio', 'Daniel', 'Certo', 'Catherine', 'Frankel', 'Stewart', 'Levesque', 'Aime', 'Mahan', 'Eric J.', 'Rosiene', ' Pe', 'Haruta', 'Mako E.', '', '']... many more lists]
我想把相邻的两个字符串加在一起。前任。 [['Barreuther Mark']]。我试过以下代码:
def getInfo():
"""Open csv, read it, get nested list, separate names and schools, return two lists"""
#
with open("CT_schools.csv") as f:
reader = csv.reader(f)
data = []
#start for
for row in reader:
data.append(row)
#end for
schoolNames = []
peopleNames = []
#start for
for i in range(len(data)):
schoolNames.append(data[i][0])
peopleNames.append(data[i][1:])
#end for
index = 0
name = 0
NewpeopleNames = []
#start while
while index < len(peopleNames):
for i in range(len(peopleNames)):
fullName = peopleNames[index][i] + " " + peopleNames[index][i+1]
NewpeopleNames.append(fullName)
index = index + 1
name = name + 2
我得到以下输出:
[' ', 'Pe Haruta', 'Ronald Golbazi', 'Christoph Raskin', ' ', ' ', 'Barry Oliver', ' ', ' ', ' ', ' ', 'Douglad M. ']
然后我用嵌套的 for 循环替换了 while 循环:
#start nested for
for index in range(len(peopleNames)):
for name in range(len(peopleNames[index])):
fullName = peopleNames[index][name] + peopleNames[index][name + 1]
然后我收到一条错误消息,指出它不在列表的索引中。您认为有人可以尝试为我指明正确的方向吗?非常感谢!
嵌套 for 循环是天真的解决方案(如果您不了解代码的复杂性)。
我建议先过滤掉列表中的所有空值,然后才引用 "add the two strings that are next to each other together"。
NewPeopleNames = list()
for sub_list in peopleNames:
tmp_list = list()
filtered_list = list(filter(None, sub_list))
for i in range(0, len(filtered_list), 2):
if i+1 >= len(filtered_list):
print('There is no last name for \'{0}\''.format(filtered_list[i]))
continue
tmp_list.append('{0} {1}'.format(filtered_list[i], filtered_list[i+1]))
NewPeopleNames.append(tmp_list)
请注意,奇数列表大小(名字没有姓氏)有一点 "protection"。
您可以修改打印行以仅将姓名 (filtered_list[i]) 附加到 NewPeopleNames 列表。