【问题标题】:Convert list into sub-list while maintaining "key"将列表转换为子列表,同时保持“键”
【发布时间】:2013-12-31 17:40:40
【问题描述】:

我有一个包含“键”和“段落”的列表。每个“键”都与一个“段落”相关联。

我的目标是将每个段落分成单独的句子,将每个句子分配给它们最初以段落形式属于的“键”。例如:

(['2925729', 'Patrick came outside and greeted us promptly.'], ['2925729', 'Patrick did not shake our hands nor ask our names. He greeted us promptly and politely, but it seemed routine.'], ['2925728', 'Patrick sucks. He farted politely, but it seemed routine.'])

现在我已经能够编写代码来将句子分成段落,并根据字典获取每个句子的命中数。我现在想为每个问题关联一个 ID。

这是处理没有任何“键”的句子的代码。第 1 步和第 2 步为了节省空间我省略了:

Dictionary = ['book', 'should have', 'open']

####Step3#####
#Create Blank list to append final output
final_out = []

##Find Matches
for sent in sentences:
  for sent in sentences:
      final_out.append((sent, sum(sent.count(col) for col in dictionary)))

#####Spit out final distinct output
##Output in dictionary structure
final_out = dict(sorted(set(final_out)))

####Get sentences and rank by max first

import operator
sorted_final_out = sorted(final_out.iteritems(),key = operator.itemgetter(1), reverse = True)

由此产生的输出是: (['johny ate the antelope', 80], ['sally has a friend',20]) 等等。然后我选择顶部的 X b 量级。我现在想要实现的是这样的:(['12222','johny ate the antelope', 80], [22332,'sally has a friend',20]).所以我基本上想确保解析出的所有句子都分配给一个“键”。这很复杂对不起。这也是 John 早期解决方案适用于更简单案例的原因。

【问题讨论】:

  • 你能发布你正在谈论的代码吗?以及预期的输出。
  • 你接受的答案有什么问题?
  • 它回答了这个问题,但结果是部分的,数据集更大更复杂。我不想不相信约翰的回答,因为事实上他回答了这个问题。

标签: python list key tuples


【解决方案1】:
from itertools import chain
list(chain(*[[[y[0],z] for z in y[1].split('. ')] for y in x]))

生产

[['2925729', 'Patrick came outside and greeted us promptly.'],
 ['2925729', 'Patrick did not shake our hands nor ask our names'],
 ['2925729', 'He greeted us promptly and politely, but it seemed routine.'],
 ['2925728', 'Patrick sucks'],
 ['2925728', 'He farted politely, but it seemed routine.']]

list(chain(*...))[[[y[0],z] for z in y[1].split('. ')] for y in x] 生成的嵌套列表展平。

如果您想“就地”更改列表,您可以使用

xl = list(x) # you gave us a tuple          
for i,y in enumerate(xl):
    xx = xl[i]
    xx = [[xx[0],y] for y in xx[1].split('. ')]
    xl[i:i+1] = xx

我不确定当数据集非常大时哪个会更快或更好。

【讨论】:

  • 谢谢你。这按预期工作。我可能有一些后续问题。我很感激帮助。谢谢。
  • 你好 Hpaul。如果我在键和句子之间有另一个字段怎么办?谢谢。
  • 你的意思是喜欢:[[xx[0], xx[1], y] for y in xx[2].split('. ')]?
  • 我试过了,但没用。预期的输出类似于: ['2893357', 'SUPER', 'sesame street.'] ['2893357', 'SUPER', 'The books are all open.'] ['2893357', 'SUPER', '我没有看到任何垃圾或碎片。'] ['2893357', 'SUPER', '她很干净,井井有条。'] ['2893357', 'STELLAR', '“我停下来把它关掉了。'] [' 2893357', 'STELLAR', '"他在微笑。'] ['2893357', 'STELLAR', '"他欢迎我来看看,并问他那天是如何帮助我的。']。谢谢。
  • 看起来您的输入现在是:(['2893357','SUPER','ses...'],['2893357','STELLAR','I stop...'])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
相关资源
最近更新 更多