Python Forum
Understand list comprehension and draw
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understand list comprehension and draw
#1
Hello,

I'm a beginner and I try a lot of things with list comprehension to understand it. Considering that, I don't understand some things.

First, how can I do only one draw by combination ? I explain, if I wrote this :


mylist = [1,2,3,4]
[(x,y,z) for x in mylist for y in mylist for z in mylist if x != y if y != z if x != z]
I got this :

Output:
[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]
But (1,2,3), (1,3,2), (2,1,3), ... are the same combination in my example.
How can I write my code to have only one draw with these three different numbers ?

I've searched a solution with itertool but I've found nothing. Maybe dropwhile but I have not succeed with.

Otherwise, if I write this :

[(x,y) for x in list(range(10)) for y in list(range(20)]
How can I manage it to have all combinations except if x and y are between 0 and 4 ? In other words, if x is in [0:5], y can not.

I’ve thought about :

[(x,y) for x in list(range(10)) for y in list(range(20)) if x in [0:5] then not y]
But I idn’t find how can I put a ‘’then’’ in a list comprehension. Is it possible ?

I have the same doubt with pass, continue and break.

Rather than do without understand, is there a website where I can find tutorials about list comprehension ? I did not find it on Python documentations, but maybe I'm wrong.
Reply
#2
https://www.youtube.com/watch?v=3dt4OGnU5sM

This channel has many tutorials that you might find useful.
Reply
#3
Making combinations from a list is difficult enough that it is included in the itertools library. Look at the itertools.combinations() function. They also have a permutations function.
Reply
#4
You can generate combinations with itertools.

from itertools import combinations
print(list(combinations(range(1,5), 3)))
I don't understand what you're trying to do in your question about if/then in the list comprehension. It is possible, you just have to order it properly.

>>> [x if x % 2 else x**2 for x in range(10)]
[0, 1, 4, 3, 16, 5, 36, 7, 64, 9]
But if you wanted tuples with items from 1 to 5, but where both aren't bigger than 3, there's no else statement. You could do it like:
>>> [(x, y) for x in range(5) for y in range(5) if x < 3 or y < 3]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2)]
Reply
#5
Hello,

Thanks everyone. I notice I wasn’t clear, sorry.
I know itertool.combinations but, except if I am wrong, I believe that we can not have multiple iterators with it. I explain :

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 ?

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) [...]
My wish is to know :
1/ how can I generate only one time (0,0,0) with no repetition ?
2/ in the same way, how can I generate them with only three different digits (ie (1,2,3)) ?

I’ve watched the youtube video. Very useful, interesting. A great thank to you ! But I didn’t understand how it could help me to solve this problem. By using set function maybe ?
I’ll watch the other videos, but there a lot (more than 200 !) so it will take time.

I hope I was more clear, I’ll go step by step, sorry.
Reply
#6
How to make list of all unique combinations of [0..6, 0..7, 0..8]
things = []
for x in range(0, 7):
    for y in range(x, 8):
        for z in range(y, 9):
            things.append((x, y, z))
Same list but no repetitions allowed:
things = []
for x in range(0, 7):
    for y in range(x+1, 8):
        for z in range(y+1, 9):
            things.append((x, y, z))
This last one as a comprehension
things = [(x,y,z) for x in range(0,7) for y in range(x+1,8) for z in range(y+1,9)]
Reply
#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
#8
Thank you for your patience and this lot of informations.

I discover this possibility :

things = []
for x in range(0, 7):
    for y in range(x+1, 8):
        for z in range(y+1, 9):
            things.append((x, y, z))
Can you explain how this code works ?
I’ve tested it, am I right if I deduce it means that y is always superior to x and grows up while y < 8 ? (because y stopped at 7 and x at 6)

Quote: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?

I am sorry, you are right. And I need to be more businesslike too.

If I took again my example :

[(x,y,z) for x in range(6) for y in range(7) for z in range(8)]
Let’s look all the draws with the numbers 1, 2 and 3 inside, six possibilities listed in the output :

Output:
(1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1)
What can I do if I want python considers that these six possibilities are equals and gives me only one time the (1,2,3) possibility in output ?

In other words, instead of give me six possibilities with the same digits, Python would give me only one result considering (1,2,3) is the same thing that (2,3,1) that (3,2,1) that…
Reply
#9
Quote:Can you explain how this code works ?
I’ve tested it, am I right if I deduce it means that y is always superior to x and grows up while y < 8 ? (because y stopped at 7 and x at 6)

The outer loop is simple. It just loops from 0 to 5. But the limits of the inner loops are determined when they fire. So instead of starting at 0, it starts at whatever x+1 evaluates to. Yes, this means that the numbers will alway be increasing within a tuple. x < y < z.

In this code
[(x,y,z) for x in range(6) for y in range(7) for z in range(8)]
there are no such restrictions. x, y, and z can be larger, smaller, or equal, so you get all the permutations, not all the combinations

Quote:What can I do if I want python considers that these six possibilities are equals and gives me only one time the (1,2,3) possibility in output ?

Ask for all the combinations.

>>> from itertools import combinations
>>> list(combinations([1,2,3], 3))
[(1, 2, 3)]
>>> list(combinations([1,2,3,4], 3))
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
Reply
#10
Hello,

Thank you very much ! I have other questions but I've learned a lot with your help. First, I need to practice with these techniques.

Maybe see you soon, anyway thanks again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 440 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 427 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  Using list comprehension with 'yield' in function tester_V 5 1,175 Apr-02-2023, 06:31 PM
Last Post: tester_V
  list comprehension 3lnyn0 4 1,360 Jul-12-2022, 09:49 AM
Last Post: DeaD_EyE
  List comprehension used differently coder_sw99 3 1,679 Oct-03-2021, 04:12 PM
Last Post: coder_sw99
  How to invoke a function with return statement in list comprehension? maiya 4 2,750 Jul-17-2021, 04:30 PM
Last Post: maiya
  List comprehension and Lambda cametan 2 2,196 Jun-08-2021, 08:29 AM
Last Post: cametan
  What is the difference between a generator and a list comprehension? Pedroski55 2 2,176 Jan-02-2021, 04:24 AM
Last Post: Pedroski55
  For Loop with List Comprehension muzikman 25 6,408 Dec-18-2020, 10:45 PM
Last Post: muzikman
  Using recursion instead of for loops / list comprehension Drone4four 4 3,072 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