【发布时间】:2018-02-22 18:11:37
【问题描述】:
我有一个 python 元组,其元素实际上是句子。我想比较每个元素的第一个字母,如果它们是小写,我将它加入到前一个元素中。如果他们不是,我只是加入第一个元素。 例如,如果我有:
tuple1 = ('Traditionally, companies have been communicating', 'with consumers through traditional or old media.', 'The rapid changes in the cyber world.', 'This is another sentence', 'which is incomplete.')
我的结果应该是:
tuple1 = ('Traditionally, companies have been communicating with consumers through traditional or old media.', 'The rapid changes in the cyber world.', 'This is another sentence which is incomplete.')
这是我的工作代码:
i=0
while i<(len(tuple1)-1):
if tuple1[i+1][0].islower():
tuple1[i] + " " + tuple[i+1]
i+=1
我怎样才能做到这一点?谢谢
【问题讨论】:
-
你在正确的轨道上,有一个问题:你不能修改你的元组的内容。元组是不可变的。您必须创建一个新元组来保存结果。事实上,我会创建一个新的
list,因为这样填充它会更容易。
标签: python tuples concatenation