Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,如何返回三元组?_Python_String - Fatal编程技术网

Python,如何返回三元组?

Python,如何返回三元组?,python,string,Python,String,任务是在第一个“.”出现时返回字符串中的内容量。然后按照您已经看到的输出的方式对其进行格式化(直到第一个“.”的句子,直到第二个“.”为止的内容数量,剩余的字符串 就像你看到的,我已经得到了想要的输出,但是我必须以三元组的形式返回它,我不知道怎么做 我希望你能在这件事上帮我。你是说这个 def split_3(s): s=s.split(".",1) s_left=s[0] s_right=s[1] for words in s:

任务是在第一个“.”出现时返回字符串中的内容量。然后按照您已经看到的输出的方式对其进行格式化(直到第一个“.”的句子,直到第二个“.”为止的内容数量,剩余的字符串

就像你看到的,我已经得到了想要的输出,但是我必须以三元组的形式返回它,我不知道怎么做

我希望你能在这件事上帮我。

你是说这个

   def split_3(s):
      s=s.split(".",1)
      s_left=s[0]
      s_right=s[1]
      for words in s:   
          t=len(s[0])
      return ("{s_left},{t},{s_right}").format(s_left=s_left,t=t,s_right=s_right)
      #example: split_3('this is it.another one.')
      #output='this is it,10,another one.'
      #desired output= ('this is it', 10, ' another one.')
你是说这个

   def split_3(s):
      s=s.split(".",1)
      s_left=s[0]
      s_right=s[1]
      for words in s:   
          t=len(s[0])
      return ("{s_left},{t},{s_right}").format(s_left=s_left,t=t,s_right=s_right)
      #example: split_3('this is it.another one.')
      #output='this is it,10,another one.'
      #desired output= ('this is it', 10, ' another one.')

您还可以从拆分中为元组赋值,因此函数变得更简单:

return (s_left,t,s_right)

您还可以从拆分中为元组赋值,因此函数变得更简单:

return (s_left,t,s_right)

('this is it,10,other one.')
不是三元组,它是一个在括号中带有逗号的字符串。你的意思是
('this is it',10,'other one.')
?是的,很抱歉我没有正确键入for循环没有任何用处。你可以只设置
t=len(s[0])
一次
('this is it,10,other one.'))
不是三元组,它是一个括号中带有逗号的字符串。您是指
(“就是这个”,10,“另一个”)
?是的,很抱歉我没有正确键入for循环没有任何用处。您可以只设置
t=len(s[0])
onceys,差不多……为什么我不先试试显而易见的一个……我觉得我还是新手,觉得我必须用“奇怪的方式”来做…不用担心。如果这让你得到了你想要的,请放心,接受它来结束问题。是的,差不多…为什么我不先尝试一下显而易见的一个…我仍然是新手,我认为我必须用“奇怪的方式”来结束问题。不用担心。如果这让你得到了你想要的,请放心,接受它来结束问题。