Python Forum
Understand list comprehension and draw
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understand list comprehension and draw
#7
(Apr-19-2020, 06:44 PM)PUP280 Wrote:
my_example = [(x,v,w) for x in list(range(6)) for y in list(range(7)) for z in list(range(8))]
Here, I have three different ranges (6, 7 and 8). If I use itertools, I can use only one, no ?

True, but it's pretty simple to trim out what you don't need. For instance, if I generate all combinations of 0-4 with 3 digits in increasing order:
Output:
>>> list(combinations(range(5), 3)) [(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
But I don't want any where the middle digit is 2.
Output:
>>> [ x for x in combinations(range(5), 3) if x[1] != 2] [(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 3, 4), (1, 3, 4), (2, 3, 4)]
Quote:With this code, for example, I have as result :

Output:
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (1, 0, 0) [...]

I don't understand. What code does that? The my_example code you have above doesn't run, but if you change the (x,v,w) to (x,y,z), then it prints out something that looks reasonable.
Output:
>>> [(x,y,z) for x in range(6) for y in range(7) for z in range(8)] [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 0, 5), (0, 0, 6), (0, 0, 7), (0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 1, 5), (0, 1, 6), (0, 1, 7), (0, 2, 0), (0, 2, 1), (0, 2, 2), (0, 2, 3), (0, 2, 4), (0, 2, 5) ... (5, 5, 0), (5, 5, 1), (5, 5, 2), (5, 5, 3), (5, 5, 4), (5, 5, 5), (5, 5, 6), (5, 5, 7), (5, 6, 0), (5, 6, 1), (5, 6, 2), (5, 6, 3), (5, 6, 4), (5, 6, 5), (5, 6, 6), (5, 6, 7)]
Quote:My wish is to know :
1/ how can I generate only one time (0,0,0) with no repetition ?

It looks like you've done that above with your my_example code. Can you run it and see how you want it to be different?

Quote:2/ in the same way, how can I generate them with only three different digits (ie (1,2,3)) ?
I think you need to be more explicit. combinations() will give you sets without repetition. Can you give an example list and detail how it differs from something like combinations(range(8), 3)?
Reply


Messages In This Thread
Understand list comprehension and draw - by PUP280 - Apr-18-2020, 05:07 PM
RE: Understand list comprehension and draw - by bowlofred - Apr-19-2020, 09:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 643 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 533 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,354 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,490 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,794 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,951 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,294 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,284 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,992 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Using recursion instead of for loops / list comprehension Drone4four 4 3,217 Oct-10-2020, 05:53 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020