我正在尝试将电子表格中两列的条目组合到python中的单个列表中。第一列包含每对中的第一个人,并且是提问者的问题,即提问者。第二个是回复的人:回答者。我希望列表看起来像这样:
[('Jack', 'Jill'), ('Jack', 'John'), ('Jack', 'Jason'), ('Jill', 'John')...]
但是,我的列表如下所示:
['(Jack, Jill)', '(Jack, John)', '(Jack, Jason)', '(Jack, john)'...]
关键区别在于,在第一个列表中,引号位于括号内侧,而第二个列表位于外侧。
这是我的流程:
answerers = line['answerers'].split(" ")
for answerer in answerers:
edgelist.append("(" + line['questioner'] + ", " + answerer + ")")
我应该做些什么来在内部,每个人周围加上引号,而不是在外面?
答案 0 :(得分:1)
edgelist.append(( line['questioner'] ,answerer))
假设我理解你的问题
答案 1 :(得分:1)
只需将它们保存为两个单独的列表,例如asker
和answerer
然后执行此操作
>>> asker = ['Jack','Jack','Jack','Jill']
>>> answerer = ['Jill','John','Jason','John']
>>> finalList = zip(asker, answerer)
>>>
>>>
>>> finalList
[('Jack', 'Jill'), ('Jack', 'John'), ('Jack', 'Jason'), ('Jill', 'John')]
答案 2 :(得分:0)
或者在一行中:
edgelist = [(line['answerer'], answerer) for answerer in answerers]