Python: How do i use itertools?

Kaizer von Maanen

I am trying to make a list containing all possible variations of 1 and 0. like for example if I have just two digits I want a list like this:

[[0,0], [0,1], [1,0], [1,1]]

But if I decide to have 3 digits I want to have a list like this:

[[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1]]

Someone told me to use itertools, but I cannot get it to work the way I want.

>>> list(itertools.permutations((range(2))))
[(0, 1), (1, 0)]
>>> [list(itertools.product((range(2))))]
[[(0,), (1,)]]

Is there a way to do this? And question number two, how would i find documentation on modules like this? I am just flailing blindly here

TerryA

itertools.product() can take a second argument: the length. It defaults to one, as you have seen. Simply, you can add repeat=n to your function call:

>>> list(itertools.product(range(2), repeat=3))
[(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]

To find the docs, you can either use help(itertools) or just do a quick google (or whatever your search engine is) search "itertools python".

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I use external python utility files in a Django project?

분류에서Dev

How do I use MapStateListener?

분류에서Dev

How do I use OR with if statements in regards to strings

분류에서Dev

How do i use Blazor components in the code?

분류에서Dev

How do I use runit with zookeeper

분류에서Dev

How do I use Firefox cookies with Wget?

분류에서Dev

How do I use the GeometryConstraint class?

분류에서Dev

How do I make use of rotation matrices?

분류에서Dev

How do I configure ActiveJob to use Resque?

분류에서Dev

How do i use SetPixel on a new Bitmap?

분류에서Dev

How do i get cURL to use https

분류에서Dev

How do I use "<<" with my own struct?

분류에서Dev

How do I use URL directories as variables?

분류에서Dev

What is fileAPI ? How do I use it with angular?

분류에서Dev

How do I open and use phpmyadmin?

분류에서Dev

How do I configure Docker to use ZFS?

분류에서Dev

How do I use the latest GCC on Ubuntu?

분류에서Dev

How can I use Selenium (Python) to do a Google Search and then open the results of the first page in new tabs?

분류에서Dev

How do I use man pages to learn how to use commands?

분류에서Dev

How do I install and use PyCharm without having to use a terminal?

분류에서Dev

How do I install and use PyCharm without having to use a terminal?

분류에서Dev

How do i install python 3.5?

분류에서Dev

How do I output a button and a text when I hover on an image? Do I need to use JQuery?

분류에서Dev

How do I make Chromium use Flash from Google Chrome?

분류에서Dev

How do I decide which manpage to use on the Ubuntu Manuals site?

분류에서Dev

How do I use FCM with codeigniter sql webpage?

분류에서Dev

How do I use an environment in an ML Azure Pipeline

분류에서Dev

How do I use GPL and MIT licenses correctly?

분류에서Dev

How do I use the Selection tool to move things in GIMP?

Related 관련 기사

  1. 1

    How do I use external python utility files in a Django project?

  2. 2

    How do I use MapStateListener?

  3. 3

    How do I use OR with if statements in regards to strings

  4. 4

    How do i use Blazor components in the code?

  5. 5

    How do I use runit with zookeeper

  6. 6

    How do I use Firefox cookies with Wget?

  7. 7

    How do I use the GeometryConstraint class?

  8. 8

    How do I make use of rotation matrices?

  9. 9

    How do I configure ActiveJob to use Resque?

  10. 10

    How do i use SetPixel on a new Bitmap?

  11. 11

    How do i get cURL to use https

  12. 12

    How do I use "<<" with my own struct?

  13. 13

    How do I use URL directories as variables?

  14. 14

    What is fileAPI ? How do I use it with angular?

  15. 15

    How do I open and use phpmyadmin?

  16. 16

    How do I configure Docker to use ZFS?

  17. 17

    How do I use the latest GCC on Ubuntu?

  18. 18

    How can I use Selenium (Python) to do a Google Search and then open the results of the first page in new tabs?

  19. 19

    How do I use man pages to learn how to use commands?

  20. 20

    How do I install and use PyCharm without having to use a terminal?

  21. 21

    How do I install and use PyCharm without having to use a terminal?

  22. 22

    How do i install python 3.5?

  23. 23

    How do I output a button and a text when I hover on an image? Do I need to use JQuery?

  24. 24

    How do I make Chromium use Flash from Google Chrome?

  25. 25

    How do I decide which manpage to use on the Ubuntu Manuals site?

  26. 26

    How do I use FCM with codeigniter sql webpage?

  27. 27

    How do I use an environment in an ML Azure Pipeline

  28. 28

    How do I use GPL and MIT licenses correctly?

  29. 29

    How do I use the Selection tool to move things in GIMP?

뜨겁다태그

보관