【问题标题】:How to form a given list of elements in python to create another set of lists from it? [closed]如何在python中形成给定的元素列表以从中创建另一组列表? [关闭]
【发布时间】:2017-02-28 05:06:26
【问题描述】:
List1 =['000095', '000094', '000092', '000101', '000099', '000096', '000095']

def makecycle(list, startElement):

形成底部列表的循环,该列表由上部元素组成!

如果我将该函数传递给起始元素和列表,它应该像这样打印:

makecycle(list1, 000094) it should print:
['000094', '000092', '000101', '000099', '000096', '000095', '000094']
and if pass 
makecycle(list1, 000101) it should print:
['000101', '000099', '000096', '000095', '000094', '000092', '000101']

and if pass 
makecycle(list1, 000092) it should print:
['000092', '000101', '000099', '000096', '000095', '000094', '000092']

我知道它有点不够清楚,但我只能指出这一点!

【问题讨论】:

  • 所有这些 python 标签是怎么回事?不可能是全部
  • 首先尝试编写一些代码。在此处发布您的代码,并准确告诉我们您遇到问题的部分。
  • 为什么要第一项和最后一项相同?这使得这个简单的任务比它应该做的更难。

标签: python python-2.7 python-3.x wxpython ipython


【解决方案1】:
def makecycle(list1,startElement):
   ind = list1.index(startElement)
   l = len(list1)
   i = ind
   list2 = []
   while ind < (l-1):
      list2.append(list1[ind])
      ind = ind + 1

   for x in range(i):
       if list1[x] not in list2:
          list2.append(list1[x])

   print(list2)

【讨论】:

  • 当我用这些估值器 makecycle(list1, 000094) 调用函数时,它非常接近它打印:['000094', '000092', '000101', '000099', '000096', '000094'] 但是缺少一个元素应该打印: ['000094', '000092', '000101', '000099', '000096', '000095', '000094'] 谢谢哥们
  • 当我通过 000096 它返回:['000096', '000096']
  • 一般输出应该是什么?请说明。
  • 如果我将 000096 传递给给定列表的倒数第二个元素,则输出取决于传递的列表和起始元素,因此新形成的列表将从该列表开始,下一个元素在右(000095),然后是左边的内容,不包括(000095)的副本,即'000095','000094','000092','000101','000099',然后以起始000096结束,然后输出是将是:['000096',000095', '000094', '000092', '000101', '000099', '000096', '000095']
  • 现在检查,可以吗?
猜你喜欢
  • 2020-05-06
  • 2014-01-27
  • 2020-08-11
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
相关资源
最近更新 更多