get items 2 by 2 from a list in Python

kollo

What is the most elegant way to obtain items 2 by 2 from a list ?

from:
my_list = ['I', 'swear', 'I', 'googled', 'first']

to:
res_list = ['I swear', 'swear I', 'I googled', 'googled first']
Hyperboreus

I would say a standard case for zip:

def pairs(i):
    return list(zip(i, i[1:]))

my_list = ['I', 'swear', 'I', 'googled', 'first']
res_list = pairs(my_list)
print(res_list)
# [('I', 'swear'), ('swear', 'I'), ('I', 'googled'), ('googled', 'first')]
print([' '.join(a) for a in res_list])
# ['I swear', 'swear I', 'I googled', 'googled first']

Just for completeness's sake, the same with arbitrary window width:

def window(i, n = 2):
    return list(zip(*(i[p:] for p in range(n))))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Get list of items in custom adapter

来自分类Dev

How to list all items from QListWidget

来自分类Dev

python中的list1 = [] list2 = []和list1 = list2 = []有什么区别?

来自分类Dev

How to remove 2 or more duplicates from list and maintain their initial order?

来自分类Dev

python [iter(list)] * 2是什么意思?

来自分类Dev

编码蝙蝠(Python> List-2> has22)

来自分类Dev

Python如何使myDcitionary ['key1'] ['key2'] = list()

来自分类Dev

python join list 2逐行列出1

来自分类Dev

Unable to Get VIN Number response from OBD-2 Device

来自分类Dev

Symfony2 updating items in "subform"

来自分类Dev

python中的list [1]和list [1:2]有什么区别?

来自分类Dev

在 python3 中是否有更快的替代 dict(zip(list1,list2)) ?

来自分类Dev

PHP 2的get方法

来自分类Dev

Python: Appending items to lists by iterating through list of lists

来自分类Dev

how to read items from file into a list of items and set the properties to the value in the file?

来自分类Dev

“ 2 + 2 = 5” Python版

来自分类Dev

Python deque: difference from list?

来自分类Dev

在Python中将元组('Id','row 1'),('Id','row 2')的集合转换为List ['Id',['row 1','row2']

来自分类Dev

mysql: How can list 2 different condition, ordering, and limit requirements from one table?

来自分类Dev

hash((-2,2))== hash((2,-2))返回True(Python)

来自分类Dev

hash((-2,2))== hash((2,-2))返回True(Python)

来自分类Dev

Fuseki 2 FROM子句

来自分类Dev

Any possible way to get the value from form1 to form2 before submitting form2

来自分类Dev

如何从python中的list2(与另一个list1常见)中消除值

来自分类Dev

Python Sqlite-如何选择Column1或Column2等于List中的值的位置?

来自分类Dev

Symfony2:FOSRest:允许“ / api / items”和“ api / items / {id}”的路由

来自分类Dev

How to get a value from keyvalue pair list?

来自分类Dev

included_list(List1,List2)

来自分类Dev

Gtk2多线程gdk_pixbuf_get_from_drawable问题

Related 相关文章

  1. 1

    Get list of items in custom adapter

  2. 2

    How to list all items from QListWidget

  3. 3

    python中的list1 = [] list2 = []和list1 = list2 = []有什么区别?

  4. 4

    How to remove 2 or more duplicates from list and maintain their initial order?

  5. 5

    python [iter(list)] * 2是什么意思?

  6. 6

    编码蝙蝠(Python> List-2> has22)

  7. 7

    Python如何使myDcitionary ['key1'] ['key2'] = list()

  8. 8

    python join list 2逐行列出1

  9. 9

    Unable to Get VIN Number response from OBD-2 Device

  10. 10

    Symfony2 updating items in "subform"

  11. 11

    python中的list [1]和list [1:2]有什么区别?

  12. 12

    在 python3 中是否有更快的替代 dict(zip(list1,list2)) ?

  13. 13

    PHP 2的get方法

  14. 14

    Python: Appending items to lists by iterating through list of lists

  15. 15

    how to read items from file into a list of items and set the properties to the value in the file?

  16. 16

    “ 2 + 2 = 5” Python版

  17. 17

    Python deque: difference from list?

  18. 18

    在Python中将元组('Id','row 1'),('Id','row 2')的集合转换为List ['Id',['row 1','row2']

  19. 19

    mysql: How can list 2 different condition, ordering, and limit requirements from one table?

  20. 20

    hash((-2,2))== hash((2,-2))返回True(Python)

  21. 21

    hash((-2,2))== hash((2,-2))返回True(Python)

  22. 22

    Fuseki 2 FROM子句

  23. 23

    Any possible way to get the value from form1 to form2 before submitting form2

  24. 24

    如何从python中的list2(与另一个list1常见)中消除值

  25. 25

    Python Sqlite-如何选择Column1或Column2等于List中的值的位置?

  26. 26

    Symfony2:FOSRest:允许“ / api / items”和“ api / items / {id}”的路由

  27. 27

    How to get a value from keyvalue pair list?

  28. 28

    included_list(List1,List2)

  29. 29

    Gtk2多线程gdk_pixbuf_get_from_drawable问题

热门标签

归档