简体   繁体   English

Python:将一个列表中的一个项目合并到另一个列表中的两个项目

[英]Python: Merge an item from one list to two items another

I am new to python, I am stuck, and I need help. 我是python的新手,被困住了,需要帮助。 Given a list as this: 给出如下列表:

listA = [1,2,3,4,5]
listB = [6,7,8,9,10,11,12,13,14,15]

I want to pick one Item from listA, Pick two Items from ListB, and have an output like this: 我想从listA中选择一个项目,从ListB中选择两个项目,并具有如下输出:

Item 1 from listA is joined with item 6 and 7 from listB

This should in a loop till there are no more items in listA 这应该一直循环,直到listA中没有更多项目为止

Thanks. 谢谢。 all help appreciated. 所有帮助表示赞赏。

Here is the solution 这是解决方案

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

you could try this: 您可以尝试以下方法:

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))

if you actually wanted to create a new list, merged in the way you described, this would work: 如果您确实想创建一个新列表,并按照您描述的方式进行合并,则可以这样做:

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]

taking the things apart: 拆开东西:

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), ...

and then you just need to flatten that list . 然后您只需要弄平那个列表

oh, you do not actually want to merge the list that way, but print out that string... that could be done like this: 哦,您实际上并不希望以这种方式合并列表,而是打印出该字符串...可以这样进行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:将列表中的一个项目转换为两个项目 - Python: turn one item in list into two items Python合并两个列表中的项目 - Python merge items from two list 如何将两个不同的 python 列表中的两个项目列为新列表中的一个项目? - How to list two items from two different python lists as one item on a new list? Python:传递列表2中的项目以及与列表1中的某些项目相关的项目? - Python : pass item from list two and that related to some items from list one? 通过放置一个列表中的每个第n个项目和另一个列表中的其他项目来合并Python中的列表? - Merge lists in Python by placing every nth item from one list and others from another? 将一个列表中的项目添加到另一个列表中的 n 个项目 - add item from one list to n number of items to another list 如何从列表中一次比较一项和两项 - How to compare one item with two items at a time from a list 如何将两个列表项附加到一个里面有两个项目的项目? (Python) - How can I append two list items to one item with two items inside? (Python) 如果一个项目包含两个单独的字符串,则从列表中获取项目 python - Get items from a list if an item contains two separate strings python python:用另一个列表中的项目替换列表中的项目 - python: replace item in a list with items in a another list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM