Ok, I like doing examples instead of explaining theory. That will come later.
We want to generate a list with list comprehension, right? So let's do it with this example: I want to generate a list of cards like this:
1 hearts, 1 diamonds.... etc.
This is what the code will look like:
------------- code ------------------
cards = [str(i) + x for i in range(2,11) for x in ['♦','♣','♠','♥']]
cards += [i + x for i in ["J ","Q ","K ","A "] for x in ['♦','♣','♠','♥']]
print(cards)
--------------------------------------
and this is the result:
['2♦','2♣','2♠','2♥','3♦','3♣','3♠','3♥','4♦','4♣','4♠','4♥','5♦','5♣','5♠','5♥','6♦','6♣','6♠','6♥','7♦','7♣','7♠','7♥','8♦','8♣','8♠','8♥','9♦','9♣','9♠','9♥','10♦','10♣','10♠','10♥','J♦','J♣','J♠','J♥','Q♦','Q♣','Q♠','Q♥','K♦','K♣','K♠','K♥','A♦','A♣','A♠','A♥']
Isn't that nice? I think it is.
Now, if we want to show our card, we could shuffle them
----------- code ---------------
from random import shuffle
shuffle(cards)
print(cards)
---------------------------------
output:
['A♦','3♥','4♣','6♥','2♠','9♥','J♥','Q♦','3♦','7♣','10♠','K♦','7♠','Q♠','8♥','J♠','J♣','10♥','5♠','5♦','2♦','6♠','9♦','3♣','6♣','4♠','A♠','4♥','Q♣','10♣','8♣','2♥','9♣','7♦','5♣','A♥','9♠','K♥','8♠','K♠','Q♥','J♦','10♦','6♦','8♦','5♥','3♠','2♣','4♦','K♣','A♣','7♥']
and then show them
-------- code -------------
for card in cards:
print(card)
---------------------------
You can imagine the output
PS: if you don't want to install python, you could go to
http://brython.info/tests/console.html?lang=en
and have some fun within your browser
Do you want to pick up a card from cards randomly?
--------- code ----------------
from random import choice
choice(card)
--------------------------------
output:
'5♦'
PS: the card is still in the cards list.
But, if you shuffled the cards, you don't need to pick up a random cards... you could just
------- code ----------
cards.pop()
------------------------
output:
'3♥'
In this way you will pick up the last card and that card is not in the list cards anymore.
This could be a start for a card game.
We want to generate a list with list comprehension, right? So let's do it with this example: I want to generate a list of cards like this:
1 hearts, 1 diamonds.... etc.
This is what the code will look like:
------------- code ------------------
cards = [str(i) + x for i in range(2,11) for x in ['♦','♣','♠','♥']]
cards += [i + x for i in ["J ","Q ","K ","A "] for x in ['♦','♣','♠','♥']]
print(cards)
--------------------------------------
and this is the result:
['2♦','2♣','2♠','2♥','3♦','3♣','3♠','3♥','4♦','4♣','4♠','4♥','5♦','5♣','5♠','5♥','6♦','6♣','6♠','6♥','7♦','7♣','7♠','7♥','8♦','8♣','8♠','8♥','9♦','9♣','9♠','9♥','10♦','10♣','10♠','10♥','J♦','J♣','J♠','J♥','Q♦','Q♣','Q♠','Q♥','K♦','K♣','K♠','K♥','A♦','A♣','A♠','A♥']
Isn't that nice? I think it is.
Now, if we want to show our card, we could shuffle them
----------- code ---------------
from random import shuffle
shuffle(cards)
print(cards)
---------------------------------
output:
['A♦','3♥','4♣','6♥','2♠','9♥','J♥','Q♦','3♦','7♣','10♠','K♦','7♠','Q♠','8♥','J♠','J♣','10♥','5♠','5♦','2♦','6♠','9♦','3♣','6♣','4♠','A♠','4♥','Q♣','10♣','8♣','2♥','9♣','7♦','5♣','A♥','9♠','K♥','8♠','K♠','Q♥','J♦','10♦','6♦','8♦','5♥','3♠','2♣','4♦','K♣','A♣','7♥']
and then show them
-------- code -------------
for card in cards:
print(card)
---------------------------
You can imagine the output
PS: if you don't want to install python, you could go to
http://brython.info/tests/console.html?lang=en
and have some fun within your browser
Do you want to pick up a card from cards randomly?
--------- code ----------------
from random import choice
choice(card)
--------------------------------
output:
'5♦'
PS: the card is still in the cards list.
But, if you shuffled the cards, you don't need to pick up a random cards... you could just
------- code ----------
cards.pop()
------------------------
output:
'3♥'
In this way you will pick up the last card and that card is not in the list cards anymore.
This could be a start for a card game.
A little example of use of list comprehension in #python #list #comprehension
ReplyDelete