我是python的新手,我被困住了,我需要帮助。 给出一个列表:
^(\#[\da-f]{3}|\#[\da-f]{6}|rgba\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)(,\s*(0\.\d+|1))\)|hsla\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)(,\s*(0\.\d+|1))\)|rgb\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)|hsl\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)\))$
我想从listA中选择一个项目,从ListB中选择两个项目,并输出如下:
listA = [1,2,3,4,5]
listB = [6,7,8,9,10,11,12,13,14,15]
这应该循环,直到listA中没有其他项目
感谢。非常感谢。
答案 0 :(得分:0)
这是解决方案
listA = [1,2,3,4,5]
listB = [6,7,8,9,10,11,12,13,14,15]
j= 0
for i in listA:
print ("Item %d from listA is joined with item %d and %d from listB")%(i,listB[j],listB[j+1])
j+=2
答案 1 :(得分:0)
listA = [1,2,3,4,5]
listB = [6,7,8,9,10,11,12,13,14,15]
for a, b1, b2 in zip(listA, listB[::2], listB[1::2]):
print(('Item {} from listA is joined with item {} and {} '
'from listB').format(a, b1, b2))
如果你真的想创建一个新列表,按照你描述的方式合并,这将有效:
merged = [a for abc in zip(listA, listB[::2], listB[1::2]) for a in abc ]
print(merged) # [1, 6, 7, 2, 8, 9, 3, 10, 11, 4, 12, 13, 5, 14, 15]
将事情分开:
listB[::2] # [6, 8, 10, 12, 14] (every other element starting at 0)
listB[1::2] # [7, 9, 11, 13, 15] (every other element starting at 1)
[abc for abc in zip(listA, listB[::2], listB[1::2])] # [(1, 6, 7), (2, 8, 9), ...
然后你只需要flatten that list。
哦,你实际上并不想以这种方式合并列表,而是打印出那个字符串......可以这样做:
答案 2 :(得分:0)
listA = [1,2,3,4,5]
listB = [6,7,8,9,10,11,12,13,14,15]
start,end,step = 0 , 2 , 2
for itemA in listA:
listB_item1,listB_item2 = listB[start:end]
print('Item',itemA,'from listA is joined with item',listB_item1,'and',listB_item2,'from listB')
start = start + step
end = start + step
<强> RESULT 强>
Item 1 from listA is joined with item 6 and 7 from listB
Item 2 from listA is joined with item 8 and 9 from listB
Item 3 from listA is joined with item 10 and 11 from listB
Item 4 from listA is joined with item 12 and 13 from listB
Item 5 from listA is joined with item 14 and 15 from listB