如何合并线对?

2024-09-28 23:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我以这种元组格式从文件中读取标题数据("string " , start_time , end_time),并希望将两行合并为一个元组。你知道吗

with open(fname) as f:
    content = f.readlines()

# remove whitespace character
content = [x.strip() for x in content]

for index, line in enumerate(content):
    if line.startswith('<p class=KRCC>'):
        trans = line.strip('<p class=KRCC>')
        start_time =  int(re.search(r'\d+', content[index -1]).group())
        end_time =  int(re.search(r'\d+', content[index +1]).group())
        myTuple = (trans, start_time, end_time)
        print myTuple
  1. ("I said that you had a nice butt. It's just not a great butt.", 1658412, 1662644)
  2. ("You wouldn't know a great butt if it bit you.", 1663012, 1665606)
  3. ("There's an image.", 1665852, 1667331)
  4. ('Would anybody like more coffee?', 1669612, 1671523)
  5. ('Did you make it?', 1673372, 1675044)

我想把两句话连成一个元组。例如,我想把两个句子组合起来,比如(1, 2)(2, 3)(3, 4)(4, 5),去掉中心时间值。你知道吗

例如

  • (1, 2)

    ("I said that you had a nice butt. It's just not a great butt.You wouldn't know a great butt if it bit you.", 1658412, 1665606)

  • (2, 3)

    ("You wouldn't know a great butt if it bit you.There's an image", 1663012, 1667331)

  • (3, 4)

    ("There's an image.Would anybody like more coffee?", 1665852, 1671523)

  • (4, 5)

    ('Would anybody like more coffee?Did you make it?', 1669612, 1675044)

你们能给我一些建议或帮助吗?这对我真的很有帮助。你知道吗


Tags: youindexiftimelineitcontentstart
2条回答

如果列表中有元组:

tups = [("I said that you had a nice butt. It's just not a great butt.", 1658412, 1662644),
        ("You wouldn't know a great butt if it bit you.", 1663012, 1665606),
        ("There's an image.", 1665852, 1667331),
        ('Would anybody like more coffee?', 1669612, 1671523),
        ('Did you make it?', 1673372, 1675044)]

words, st, en = zip(*tups)

# combine 1 and 2
print((' '.join((words[0], words[1])), st[0], en[1]))

像这样的?你知道吗

def combine_tuples(t1, t2):
    return ("%s %s" %(t1[0], t2[0]), t1[1], t2[2])

示例:

>>> combine_tuples(t1, t2)
("I said that you had a nice butt. It's just not a great butt. You wouldn't know a great butt if it bit you.", 1658412, 1665606)
>>> 

相关问题 更多 >