【问题标题】:Comparing and Concatenating Tuples in Python在 Python 中比较和连接元组
【发布时间】: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


【解决方案1】:

你可以使用itertools.groupby:

import itertools  
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.')
new_data = tuple(' '.join(i[-1] for i in b) for _, b in itertools.groupby(enumerate(tuple1), key=lambda x:x[-1][0].islower() or tuple1[x[0]+1][0].islower() if x[0]+1 < len(tuple1) else True))

输出:

('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.')

【讨论】:

  • 哎呀,我不知道 itertools 中的 groupby 方法!非常好。
  • 这实际上适用于最初的问题。但是如果有多个句子,它就不起作用。我编辑了元组以添加更多句子。您可以相应地编辑代码吗?谢谢
  • @Ajax1234 我正在尝试使用相同的过程来连接不以'.''?''!' 结尾的句子。我尝试重组您提供给tuple(' '.join(i[-1] for i in b) for _, b in itertools.groupby(enumerate(tuple1), key=lambda x:x[-1][-1] != '.' or tuple1[x[0]+1][-1] == '.' if x[0]+1 &lt; len(tuple1) else True)) 的代码。这样如果你有tuple1 = ('Traditionally', 'consumers.', 'The rapid changes?', 'This sentence', 'which is incomplete.')new_data应该给'Traditionally consumers.', 'The rapid changes?', 'This sentence which is incomplete.'你能帮忙吗?谢谢
  • @TheSolider 我在当前代码上测试了你的新输入并得到了你想要的输出,('Traditionally consumers.', 'The rapid changes?', 'This sentence which is incomplete.')。你能澄清一下吗?
  • @Ajax1234 它可以工作,但并不完美。如果输入更改为tuple1 = ('Traditionally', 'consumers.', 'The rapid changes', 'This sentence', 'which is incomplete.'),则我不会得到所需的输出(以句号、问号或感叹号结尾的完整句子)。以及如何解释'?''!'
【解决方案2】:

希望这能满足您的需求。我没有使用任何外部模块就做到了。

b = ('Traditionally, companies have been communicating', 'with consumers through traditional or old media.', 'The rapid changes in the cyber world.')
i=0
a=[]
num=0
for element in b:
    print(element)
    if element[i][0].islower():
        a[-1]=a[-1]+element
    else:
        a.append(element)
        num=num+1
print(a)

【讨论】:

    【解决方案3】:

    正如第一条评论所说,元组是不可变的。使用列表可能更容易,如果您真的需要一个元组,您可以在之后将结果列表转换为一个。

    phrases = ['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.']
    def join_on_uppercase(items):
      final_phrases = []
      temp_list = [phrases[0]]
      for phrase in phrases[1:]:
        if phrase[0].isupper():
          final_phrases.append(' '.join(temp_list))
          temp_list = []
        temp_list.append(phrase)
      final_phrases.append(' '.join(temp_list))
      return tuple(final_phrases)
    print(join_on_uppercase(phrases))
    

    那应该照顾它。您需要最后一行来清除 temp_list。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-16
      • 2021-06-24
      • 1970-01-01
      • 2015-08-01
      • 2013-11-24
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多