我在list
中有以下示例python
:
room_list = [['Room A','ok'],['Room B','ok'],['Room C','ok'],['Room D','ok'],['Room E','ok'],['Room F','ok'],['Room G','ok'],['Room H','ok'],['Room I','ok']]
我正在尝试来自list
list
的另一个['%d. Room number: %s']
,%d
其中%s
的数字越来越多,5
是房间号值从上面的列表(每个内部列表中的第1项)。但我想要做的是跳过%d
的{{1}}号并将其称为6
- 意味着以下应该是上面示例列表的新列表的输出:
new_room_list = ['1. Room number: Room A','2. Room number: Room B','3. Room number: Room C','4. Room number: Room D','6. Room number: Room E','7. Room number: Room F','8. Room number: Room G','9. Room number: Room H','10. Room number: Room I']
正如您所看到的,我不想跳过任何数据,只是跳过号码5
并将其称为6
(上面的输出Room E
应该是5
但被命名为6
)。我为代码编写了这一行,但它没有跳过第5行:
new_room_list = ["%d. Room number: %s" % (i,*(x[:-1])) for i, x in enumerate(room_list, 1)]
如何跳过5
并将其重命名为6
?
答案 0 :(得分:3)
new_room_list = ["%d. Room number: %s" % (i+1 if i > 4 else i,*(x[:-1])) for i, x in enumerate(room_list, 1)]
输出:
['1. Room number: Room A', '2. Room number: Room B', '3. Room number: Room C', '4. Room number: Room D', '6. Room number: Room E', '7. Room number: Room F', '8. Room number: Room G', '9. Room number: Room H', '10. Room number: Room I']
我所做的唯一更改是将(i,*(x[:-1]))
替换为(i+1 if i > 4 else i,*(x[:-1]))
我使用了python的三元运算符版本,其使用方法如下:
a if condition else b
答案 1 :(得分:2)
只需使用自定义迭代器,itertools
通常可以帮助您解决此问题:
In [7]: from itertools import chain, count
In [8]: it = zip(chain(range(1, 5), count(6)), room_list)
In [9]: ['%d. Room number: %s'%(i, rm) for i, (rm, _) in it]
Out[9]:
['1. Room number: Room A',
'2. Room number: Room B',
'3. Room number: Room C',
'4. Room number: Room D',
'6. Room number: Room E',
'7. Room number: Room F',
'8. Room number: Room G',
'9. Room number: Room H',
'10. Room number: Room I']
小心,chain(range(1, 6), count(7))
是一个无限的迭代器。当最短的迭代器停止时zip
停止,但是,例如,如果你使用itertools.zip_longest
,则会爆炸。
答案 2 :(得分:0)
您可以使用next
:
room_list = [['Room A','ok'],['Room B','ok'],['Room C','ok'],['Room D','ok'],['Room E','ok'],['Room F','ok'],['Room G','ok'],['Room H','ok'],['Room I','ok']]
l = iter(range(1, len(room_list)+2))
final_rooms = ["{}. Room number: {}".format(next(l) if i != 4 else [next(l), next(l)][0], a) for i, (a, _) in enumerate(room_list, start = 1)]
输出:
['1. Room number: Room A', '2. Room number: Room B', '3. Room number: Room C', '4. Room number: Room D', '6. Room number: Room E', '7. Room number: Room F', '8. Room number: Room G', '9. Room number: Room H', '10. Room number: Room I']
答案 3 :(得分:0)
这是一种可视化的简单方法;解压缩你拥有的单例并将其分解为其组件。进行正常追加for循环,并根据需要重新创建单例。
#! /usr/bin/env python3
from pprint import PrettyPrinter
pp = PrettyPrinter(indent=2)
rooms_list = [
["Room A", "ok"],
["Room B", "ok"],
["Room C", "ok"],
["Room D", "ok"],
["Room E", "ok"],
["Room F", "ok"],
["Room G", "ok"],
["Room H", "ok"],
["Room I", "ok"]
]
print("Rooms Lists")
pp.pprint(rooms_list)
new_rooms_list = []
for (index, (room, status)) in enumerate(rooms_list):
new_rooms_list.append("{}. Room number: {}".format(
index if index < 5 else index + 1,
room
)
)
print("Formatted Rooms")
pp.pprint(new_rooms_list)
输出如下:
Rooms Lists
[ ['Room A', 'ok'],
['Room B', 'ok'],
['Room C', 'ok'],
['Room D', 'ok'],
['Room E', 'ok'],
['Room F', 'ok'],
['Room G', 'ok'],
['Room H', 'ok'],
['Room I', 'ok']]
Formatted Rooms
[ '0. Room number: Room A',
'1. Room number: Room B',
'2. Room number: Room C',
'3. Room number: Room D',
'4. Room number: Room E',
'6. Room number: Room F',
'7. Room number: Room G',
'8. Room number: Room H',
'9. Room number: Room I']
现在,您可以将中间部分作为单例/生成器返回:
new_rooms_list = [
("{}. Room number: {}".format(
index if index < 5 else index + 1,
room
)) for index, (room, status) in enumerate(rooms_list)
]