我有一个列表 [T20,T5,T10,T1,T2,T8,T16,T17,T9,T4,T12,T13,T18]
我已经去除了T,将其转换为整数类型,并对列表进行排序以获取此信息:
sorted_ids = [1、2、4、5、8、9、10、12、13、16、17、18、20]
我正在遍历列表,并检查当前数字的下一个数字是否按数字顺序。如果不是,我想在其位置插入“ V” 。
所以最终列表应如下所示: [1、2,V,4、5,V,V,8、9、10,V,12、13,V,V,16、17、18 ,V,20]
但是,我无法在正确的位置插入 V的确切编号。
def arrange_tickets(tickets_list):
ids=[]
for item in tickets_list:
new_str=item.strip("T")
ids.append(int(new_str))
sorted_ids = sorted(ids)
temp_ids = []
print("Sorted: ",sorted_ids)
#size = len(sorted_ids)
for i in range(len(sorted_ids)-1):
temp_ids.append(sorted_ids[i])
if sorted_ids[i]+1 != sorted_ids[i+1] :
temp_ids.insert(i+1,"V")
print(temp_ids)
#print(sorted_ids)
tickets_list = ['T20', 'T5', 'T10', 'T1', 'T2', 'T8', 'T16', 'T17', 'T9', 'T4', 'T12', 'T13', 'T18']
print("Ticket ids of all the available students :")
print(tickets_list)
result=arrange_tickets(tickets_list)
实际结果: [1、2,'V',4,'V',5、8,'V',9,'V',10、12,'V',13、16 17,18]
预期结果: [T1,T2,V,T4,T5,V,V,T8,T9,T10,V,T12,T13,V,V,T16,T17,T18,V,T20]
答案 0 :(得分:3)
这是一个解决方案:
sorted_ids=[1, 2, 4, 5, 8, 9, 10, 12, 13, 16, 17, 18, 20]
def arrange(inList):
newList = []
newList.append('T'+str(inList[0]))
for i in range(1,len(inList)):
diff = inList[i] - inList[i-1]
if diff > 1:
for d in range(diff-1):
newList.append('V')
newList.append('T'+str(inList[i]))
else:
newList.append('T'+str(inList[i]))
return newList
print(arrange(sorted_ids))
输出:
['T1', 'T2', 'V', 'T4', 'T5', 'V', 'V', 'T8', 'T9', 'T10', 'V', 'T12', 'T13', 'V', 'V', 'T16', 'T17', 'T18', 'V', 'T20']
答案 1 :(得分:3)
这是值得考虑的另一个解决方案:
TableA(B_NUM)
结果:
sorted_ids=[1, 2, 4, 5, 8, 9, 10, 12, 13, 16, 17, 18, 20]
for i in range(min(sorted_ids), max(sorted_ids)):
if sorted_ids[i] != i + 1:
sorted_ids.insert(i, 'V')
final_list = [ "T" + str(x) if isinstance(x, int) else x for x in sorted_ids]
答案 2 :(得分:3)
以下是列表理解功能,可让您获得所需的内容:
sorted_ids=[1, 2, 4, 5, 8, 9, 10, 12, 13, 16, 17, 18, 20]
a = sorted_ids[0]
b = sorted_ids[-1]
nums = set(sorted_ids)
expected = ["T" + str(i) if i in nums else 'V' for i in range(a,b+1)]
print(expected)
输出:
['T1', 'T2', 'V', 'T4', 'T5', 'V', 'V', 'T8', 'T9', 'T10', 'V', 'T12', 'T13', 'V', 'V', 'T16', 'T17', 'T18', 'V', 'T20']
答案 3 :(得分:2)
temp_ids.insert(i+1,"V")
这是麻烦的声明。 通过以下方式更新代码
temp_ids=[]
for i in range(len(sorted_ids)-1):
temp_ids.append(sorted_ids[i])
if sorted_ids[i]+1 != sorted_ids[i+1] :
for i in range(sorted_ids[i+1]-sorted_ids[i]-1):
temp_ids.append("V") # appends as many V's as required
temp_ids.append(sorted_ids[-1]) # appends last element
这应该有效 假设排序数组是[1,2,6]
因此,我们的期望输出应为[1,2,'V','V','V',6]。所以每次
sorted_ids[i]+1 != sorted_ids[i+1]
条件成立,我们将不得不追加几个V。现在确定要附加的V数,看到在2和6之间,将附加3个V。因此,通常我们会附加(sorted_ids [i + 1]-sorted [i] -1)V。
现在看到此行
for i in range(len(sorted_ids)-1):
由于这一行,我们的列表仅在[1,2,6]中以[1,2]运行,并且我们从不在For循环中附加6,因此在退出For Loop之后将其附加。
答案 4 :(得分:1)
首先考虑列表中应该包含哪些ID,假设它们从1开始并以最大的ID结束。然后检查每个期望的id是否确实存在,如果没有,请在其中放置“ V”。作为副作用,这也会对列表进行排序。
def arrange_tickets(tickets_list):
ids = [int(ticket[1:]) for ticket in tickets_list]
expected_ids = range(1, max(ids) + 1)
return ["T%d" % n if n in ids else "V" for n in expected_ids]
tickets_list = ['T20', 'T5', 'T10', 'T1', 'T2', 'T8', 'T16', 'T17', 'T9', 'T4', 'T12', 'T13', 'T18']
print("Ticket ids of all the available students :")
print(tickets_list)
result=arrange_tickets(tickets_list)
print(result)
结果:
Ticket ids of all the available students :
['T20', 'T5', 'T10', 'T1', 'T2', 'T8', 'T16', 'T17', 'T9', 'T4', 'T12', 'T13', 'T18']
['T1', 'T2', 'V', 'T4', 'T5', 'V', 'V', 'T8', 'T9', 'T10', 'V', 'T12', 'T13', 'V', 'V', 'T16', 'T17', 'T18', 'V', 'T20']
答案 5 :(得分:1)
您可以使用以下itertools recipe来对连续的数字进行分组:
from itertools import groupby
from operator import itemgetter
def groupby_consecutive(lst):
for _, g in groupby(enumerate(lst), lambda x: x[0] - x[1]):
yield list(map(itemgetter(1), g))
sorted_ids = [1, 2, 4, 5, 8, 9, 10, 12, 13, 16, 17, 18, 20]
print(list(groupby_consecutive(lst=sorted_ids)))
# [[1, 2], [4, 5], [8, 9, 10], [12, 13], [16, 17, 18], [20]]
然后,您可以创建一个函数,以从先前的分组中获取散布的V值:
def interperse(lst):
for x, y in zip(lst, lst[1:]):
yield ["V"] * (y[0] - x[-1] - 1)
groups = list(groupby_consecutive(lst))
print(list(interperse(groups)))
# [['V'], ['V', 'V'], ['V'], ['V', 'V'], ['V']]
然后,您最终可以将上述结果压缩在一起:
def add_prefix(lst, prefix):
return [prefix + str(x) for x in lst]
def create_sequences(lst, prefix='T'):
groups = list(groupby_consecutive(lst))
between = list(interperse(groups))
result = add_prefix(groups[0], prefix)
for x, y in zip(between, groups[1:]):
result.extend(x + add_prefix(y, prefix))
return result
sorted_ids = [1, 2, 4, 5, 8, 9, 10, 12, 13, 16, 17, 18, 20]
print(create_sequences(lst=sorted_ids))
# ['T1', 'T2', 'V', 'T4', 'T5', 'V', 'V', 'T8', 'T9', 'T10', 'V', 'T12', 'T13', 'V', 'V', 'T16', 'T17', 'T18', 'V', 'T20']
答案 6 :(得分:1)
一枪直接形成原始阵列
array = ['T20', 'T5', 'T10', 'T1', 'T2', 'T8', 'T16', 'T17', 'T9', 'T4', 'T12', 'T13', 'T18']
您可以定义一个完成所有工作的方法:
def add_vs_between_not_cons(array):
iterable = sorted(array, key= lambda x: int(x[1:]))
i, size = 0, len(iterable)
while i < size:
delta = int(iterable[i][1:]) - int(iterable[i-1][1:])
for _ in range(delta-1):
yield "V"
yield iterable[i]
i += 1
因此,您可以致电:
print(list(add_vs_between_not_cons(array)))
#=> ['T1', 'T2', 'V', 'T4', 'T5', 'V', 'V', 'T8', 'T9', 'T10', 'V', 'T12', 'T13', 'V', 'V', 'T16', 'T17', 'T18', 'V', 'T20']
答案 7 :(得分:1)
我对infytq查询的解决方案:
def arrange_tickets(tickets_list):
ids = [int(ticket[1:]) for ticket in tickets_list]
expected_ids = range(1, max(ids) + 1)
listt=["T%d" % n if n in ids else "V" for n in expected_ids]
list1=listt[0:10]
list2=listt[11:]
for i in range(10):
if 'V' in list2:
list2.remove('V')
for j in range(0,len(list2)):
for n, i in enumerate(list1):
if i == 'V':
list1[n] = list2[j]
j+=1
return list1
tickets_list = ['T5','T7','T1','T2','T8','T15','T17','T19','T6','T12','T13']
print("Ticket ids of all the available students :")
print(tickets_list)
result=arrange_tickets(tickets_list)
print()
print("Ticket ids of the ten students in Group-1:")
print(result[0:10])