永发信息网

python里怎么选取一串数字的任何其中相邻的三个数字,第一个和最后一个是相邻

答案:2  悬赏:50  手机版
解决时间 2021-03-18 21:38
  • 提问者网友:蓝琪梦莎
  • 2021-03-17 21:16
python里怎么选取一串数字的任何其中相邻的三个数字,第一个和最后一个是相邻
最佳答案
  • 五星知识达人网友:我住北渡口
  • 2021-03-17 21:33
用列表解析,比如:
b=[1001,1002,1003,1004,1005]
[x for x in b if b.index(x)%2 ==0] 就是你要的结果,实际就是取列表中奇数位的值
全部回答
  • 1楼网友:轻熟杀无赦
  • 2021-03-17 22:00
下次问问题可以描述得清晰点。 我理解你的意识是把一串数字首尾连接形成一个环。 #coding=utf-8 """ 从一个环形数字串中取出相邻三个数字 """ def function(string,startIndex):  length=len(string)  newString=string  if (startIndex<0 or startIndex >=length):#开始下标必须小于字符串长度;如果不想限定的话可以取模   print "Error,index is illegal"   exit(1)  else:   if (startIndex > length-3):    newString=string+string[0:3]   return newString[startIndex:3+startIndex] """ test """  testString="hello" #print function(testString,-1) #print function(testString,0) #print function(testString,1) #print function(testString,2) #print function(testString,3) #print function(testString,4) #print function(testString,5)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯