How can I create a 2d array with labels, without numpy or pandas, from two lists for Python?

Multi tool use
Multi tool use


How can I create a 2d array with labels, without numpy or pandas, from two lists for Python?



Let's say I have scores for five Rock, Paper, Scissors games that look like this:


player_score = [1,0,1,1,0]
cpu_score = [0,1,0,0,1]



I want to make a 2d array (without numpy or pandas) that keeps a record of the games played(Like a scoreboard). So the final output would look 'something' resembled to this:


G1 G2 G3 G4 G5

Player 1 0 1 1 0
CPU 0 1 0 0 1



Any suggestions?





Does my answer work?
– Abhishek
Jul 2 at 1:09





No (I updated my question, forgot about pandas) I got: Traceback (most recent call last): File "************************", line 11, in <module> import pandas as pd ModuleNotFoundError: No module named 'pandas'
– Lotus Alice
Jul 2 at 1:14





you didn't specify anything. what are the labels? what is your desired behavior? the output you showed doesn't answer those questions. as is, your question is unclear/too broad
– bobrobbob
Jul 2 at 9:34





2 Answers
2



Numpy and pandas are powerful tools in Python programming, but trying working without them helps to learn better. They provide a diversity of useful functions and object classes, to work without which means that you may have to define your own.



To display the scoreboard that you wish, I'd recommend that you define your own scoreboard class. I'll give you an example like this:


class ScoreBoard:
def __init__(self):
self.player_score = [1,0,1,1,0]
self.cpu_score = [0,1,0,0,1]

def __str__(self):
string = 't'
for i in range(len(self.player_score)):
string += 'G%d '%(i+1)
string = string + 'nnPlayert'
for i in range(len(self.player_score)):
string += '%d '%self.player_score[i]
string = string + 'nCPUt'
for i in range(len(self.cpu_score)):
string += '%d '%self.cpu_score[i]
return string

def record_score(self, player_score, cpu_score):
self.player_score.append(player_score)
self.cpu_score.append(cpu_score)



and if you


a = ScoreBoard()
print(a)
a.record_score(1, 0)
print(a)



, this would be shown:


G1 G2 G3 G4 G5

Player 1 0 1 1 0
CPU 0 1 0 0 1
G1 G2 G3 G4 G5 G6

Player 1 0 1 1 0 1
CPU 0 1 0 0 1 0



Obviously, more should be done to make this class work well.



1) The initial player_score and cpu_score have to be



2) What if the scoreboard is empty? When it's empty, would you decorate the string output?



3) Is there a possibility that it's a draw, like Rock vs. Rock? If not, cpu_score can be calculated when you have player_score. In that case, one of these two is not necessary.



4) Perhaps it can become a scoreboard for more games where there are more players?



5) Perhaps you can even define your own labeled 2D arrays, like pandas.DataFrame?



If you haven't learned about Object-Oreinted-Programming in Python, you're free to ask. You can also learn more in Python Programming Tutorials.



I think what you're looking for is a DataFrame?


import pandas as pd
player_score = [1,0,1,1,0]
cpu_score = [0,1,0,0,1]

df = pd.DataFrame([player_score, cpu_score])
df.columns = ["G1", "G2", "G3", "G4", "G5"]
df.index = ['Player', 'CPU']

print(df)



gives


G1 G2 G3 G4 G5
Player 1 0 1 1 0
CPU 0 1 0 0 1






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

DaBm0r74A4FS PsV,xoEig48xs4OJ2P0d M 5repWJlR0swc1Bk,xhxnHQ
9otrDPo WkU94ieDk 22EVEXNE,dVg7 f3pdA

Popular posts from this blog

Rothschild family

Cinema of Italy