【问题标题】:Python: range operation with list [closed]Python:使用列表进行范围操作[关闭]
【发布时间】:2012-06-08 13:27:17
【问题描述】:

我有这样的元素列表:[1/1/9-1/1/13, 1/1/20-1/1/22]

我想打印介于 9 和 13、20 和 22 之间的数字

Result= [1/1/10, 1/1/11, 1/1/12, 1/1/21 ]

range() 方法可以做到这一点,但是如何捕捉它们呢?

【问题讨论】:

  • 那些应该是字符串吧?

标签: python list range


【解决方案1】:
>>>test = ['1/1/9-1/1/13', '1/1/20-1/1/22']
>>>test = [tuple(x.split('-')) for x in test]
>>>print test
[('1/1/9', '1/1/13'), ('1/1/20', '1/1/22')]
>>>result = [x[:x.rfind('/')+1]+str(t) for x,y in test for t in range(int(x.split('/')[-1])+1, int(y.split('/')[-1]))]
>>>print result
['1/1/10', '1/1/11', '1/1/12', '1/1/21']

我想这就是你想要的。

【讨论】:

  • Excellent :) 我是 python 新手,这就是我想要的,你在执行“结果”行时成功了吗?我收到此错误消息“invalid literal for int() with base 10: '1/1/9'”
  • @user1435173 抱歉,我犯了一个错误。现在是对的。
猜你喜欢
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多