如何合并线对?

时间:2017-06-22 20:25:26

标签: python list tuples

我从这个元组格式("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)
  6. 我想在一个元组中绑定两个句子。例如,我想将(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)

    你们能给我任何建议或帮助吗?这对我来说真的很有帮助。

2 个答案:

答案 0 :(得分: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)
>>> 

答案 1 :(得分:0)

如果列表中有元组:

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]))