Home > Net >  The result list is in a row and not in a column, how to change?
The result list is in a row and not in a column, how to change?

Time:04-13

The result is in a row and not in a column, how to change ? my_5_shuffles = [pick_4_cards1() for x in range(4)] print (my_5_shuffles) [['7', '9', 'J', 'Q'], ['7', '7', '9', '9'], ['10', '9', '9', 'Q'], ['7', '9', 'J', 'K']] Thanks

import pandas as pd
import random
df = pd.read_csv('3.csv')
df.drop(['q','w'],inplace=True, axis=1)
n=0
z=n 6
df=df.iloc[n:z]

def pick_4_cards(df=df):
    card = []
    for card_number in range(4):
        picked_card = random.choice(df['a'])
        card.append(picked_card)
    return sorted(card)

my_5_shuffles = [pick_4_cards() for x in range(3)]
print (my_5_shuffles)

[['7', '8', 'K', 'Q'], ['K', 'K', 'K', 'Q'], ['7', '8', '8', 'Q']]

CodePudding user response:

You're getting a list, because you used card=[]. The pick_4_cards(df=df) function returns that list (sorted). Then you print out out 4 of those random shuffles in my_5_shuffles that was appended in a list, inside of a list as well. That's why you have a list in a list.

You could iterate over the key values and print out every value with a for loop.

Something like this can help:

my_5_shuffles= [['7', '8', 'K', 'Q'], ['K', 'K', 'K', 'Q'], ['7', '8', '8', 'Q']]

for x in range(len(my_5_shuffles)):
    print(my_5_shuffles[x])

    #output: ['7', '8', 'K', 'Q']
    #        ['K', 'K', 'K', 'Q']
    #        ['7', '8', '8', 'Q']

Take a look at this for more information

  • Related