Nerdcubed
Badges: 13
Rep:
?
#1
Report Thread starter 2 years ago
#1
Hello, I have a question about splitting elements in a list in Python. I am creating a poker game and the cards are represented with a number followed by a letter (to represent the suit). For example, "2C" would be the 2 of clubs. Here's my problem. I want to be able to split this element into its number and suit but I can't do that. What do you suggest?
0
reply
jfrench74
Badges: 13
Rep:
?
#2
Report 2 years ago
#2
Strings?
https://www.tutorialspoint.com/pytho...on_strings.htm
0
reply
Nerdcubed
Badges: 13
Rep:
?
#3
Report Thread starter 2 years ago
#3
I was thinking more in a list format.

I'll have a list containing, say:
["2C", "3C", "4C", "5C", "6C"]
and I'll want to separate the numbers from the suit.
How do I do this when they are stored in the same element?
0
reply
HertsExRep
Badges: 13
Rep:
?
#4
Report 2 years ago
#4
(Original post by Nerdcubed)
I was thinking more in a list format.

I'll have a list containing, say:
["2C", "3C", "4C", "5C", "6C"]
and I'll want to separate the numbers from the suit.
How do I do this when they are stored in the same element?
I'm not quite sure what you'll be doing, so I don't know how helpful this is, but you could try a dictionary, so that by calling upon a key, Python will return the item coded to that key.
0
reply
Nerdcubed
Badges: 13
Rep:
?
#5
Report Thread starter 2 years ago
#5
(Original post by Michelle Bieger)
I'm not quite sure what you'll be doing, so I don't know how helpful this is, but you could try a dictionary, so that by calling upon a key, Python will return the item coded to that key.
I don't think I can rely on dictionaries - I'm constructing an algorithm about 100 lines long that calculates poker hands based on lists. Thanks for your help though.
0
reply
uponthyhorse
Badges: 15
Rep:
?
#6
Report 2 years ago
#6
(Original post by Nerdcubed)
I was thinking more in a list format.

I'll have a list containing, say:
["2C", "3C", "4C", "5C", "6C"]
and I'll want to separate the numbers from the suit.
How do I do this when they are stored in the same element?
Can you not just iterate over every element and then get the first character of the string and store it in a new list? And do the same for the second character, i.e. the suit.
0
reply
Nerdcubed
Badges: 13
Rep:
?
#7
Report Thread starter 2 years ago
#7
(Original post by uponthyhorse)
Can you not just iterate over every element and then get the first character of the string and store it in a new list? And do the same for the second character, i.e. the suit.
That's what I'm trying to do. I'm not sure how to go about doing that though.
0
reply
Daniel00
Badges: 12
Rep:
?
#8
Report 2 years ago
#8
Create an Array with your card details aka 2C.
Card = ["2C", "..."] etc


Matrix = [ ] (2D Array)
Create a loop, same length as cards.

for i in range(len(loop)):

In this loop, We we select every element in the array, slice it, and append it to a 2D array.

Inside the loop, you will need something like

letter, number = str(card[i]).split()
(you could also use [ ] on the string part to split it. So to get the number you could do [-1] to get the number or [0] to get the letter provided that its only 1 letter and 1 number)

this should seperate the number and didget, havent tested just thinking from the top of my head. Now we append into our new 2D array.

matrix.append(letter, number)

now everything is split. To get values from a 2D array you do something like

matrix[0][0 or 1]

The first 0 selects the first item in the array
The 0 or 1 can select either the card or the number.
0 being the card. 1 Being the number.

Hope this helps if I understood your q correctly.
0
reply
winterscoming
Badges: 19
Rep:
?
#9
Report 2 years ago
#9
Have you considered using List Comprehension? You can usually use this to make life much easier doing all kinds of things with lists

There's a good overview here on how they work and how to use them here:
http://www.pythonforbeginners.com/ba...ions-in-python

A few very quick examples using list comprehension with those cards represented by a list of strings such as "2C", "3C", etc.
Code:
# A list of cards 
cards = [ "2C", "3C", "4C", "3D", "4D", "5D", "2S", "7S" ] 

 # Example No. 1 - create a list of cards which start with "2" 
twos = [c for c in cards if c.startswith( "2" )]
 print('All Twos: '  + ' '.join(twos)) 

# Example No. 2 - create a list of cards which end with "D" 
diamonds = [c for c in cards if c.endswith( "D" )]
print('All Diamonds: ' + ' '.join(diamonds)) 

 # Example No. 3 - Create a list of the rank of each card in the diamonds list 
diamondValues = [c[:1] for c in diamonds] 
print('Diamond Card Ranks: ' + ' '.join(diamondValues))

# Example No. 4 - Get the first card starting with 2 
firstTwo = next(c for c in twos)
print('First Two Card: ' + firstTwo)
(Code running in an online python editor: https://repl.it/repls/GrowlingMisguidedDeviltasmanian )

Output from the above program:
Code:
All Twos: 2C 2S
 All Diamonds: 3D 4D 5D 
Diamond Card Ranks: 3 4 5 
First Two Card: 2C
If you're unfamiliar with list comprehension, then it's worth spending time getting used to it - the syntax can look strange at first, but it's essentially a short-hand way of doing the kinds of things you might be used to doing to a list in a for loop, but without all the extra clutter.
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Should there be a new university admissions system that ditches predicted grades?

No, I think predicted grades should still be used to make offers (627)
33.66%
Yes, I like the idea of applying to uni after I received my grades (PQA) (784)
42.08%
Yes, I like the idea of receiving offers only after I receive my grades (PQO) (368)
19.75%
I think there is a better option than the ones suggested (let us know in the thread!) (84)
4.51%

Watched Threads

View All