How to display nested lists in Python without using brackets, commas, or quotes

Recently, I started learning python and I am looking to print data in a specific format utilizing loops. Here is the desired output:

Table X:
    Y   Z
Y   INF INF
Z   INF INF

Table Y:
    X   Z
X   INF INF
Z   INF INF

Table Z:
    X   Y
X   INF INF
Y   INF INF
Currently, I have a 3d array that is being printed out as follows:

[[['X', 'Y', 'Z'], ['Y', 'INF', 'INF'], ['Z', 'INF', 'INF']], [['Y', 'X', 'Z'], ['X', 'INF', 'INF'], ['Z', 'INF', 'INF']], [['Z', 'X', 'Y'], ['X', 'INF', 'INF'], ['Y', 'INF', 'INF']]]

I attempted to use

print("".join(map(str, list)))
, but it only removed one set of outer brackets. Additionally, I tried the following approach:

for item in arr:
    print("\n".join("\t".join(map(str, item))))

Answer №1

Perhaps not the most straightforward method, but definitely tidy:

y = [[['A', 'B', 'C'], ['B', 'DEF', 'DEF'], ['C', 'DEF', 'DEF']], [['B', 'A', 'C'], ['A', 'DEF', 'DEF'], ['C', 'DEF', 'DEF']], [['C', 'A', 'B'], ['A', 'DEF', 'DEF'], ['B', 'DEF', 'DEF']]]
d = [e for f in y for g in f for e in g]
print(" ".join(d))
A B C B DEF DEF C DEF DEF B A C A DEF DEF C DEF DEF C A B A DEF DEF B DEF DEF

Answer №2

Recent Update!

Using the DataFrame module from pandas is essential for effectively displaying structured data, be sure to refer to the official documentation.

from pandas import DataFrame

for item in dataset:

    print("Table {}: ".format(item[0][0]))
    
    df = DataFrame(item)
    
    df.columns = [""] + item[0][1:]
    df = df.iloc[1:]
    df = df.to_string(index=False)

    print(df, "\n")


Running this code snippet will generate the desired output.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

`an issue arises when attempting to run a local command using fabric2`

Take a look at the code below: from fabric2 import Connection cbis = Connection.local() with cbis.cd('/home/bussiere/Workspace/Stack/Event/'): cbis.run('git add .') Unfortunately, I encountered this error: TypeError: local() mi ...

Generating a compilation from a variety of JSON documents

I have a goal to compile a corpus containing the content of various articles stored in JSON format. These articles are spread across different files named after the year they were published; for instance: with open('Scot_2005.json') as f: da ...

Pd.DF is removing rows from hourly datetime data

I am encountering a particular issue, most likely a simple one as I am in the process of learning. The data I have is structured with a time interval of 15 minutes and spans across multiple days. https://i.stack.imgur.com/2oQ6a.png I am seeking assistan ...

Why is the upload handler not aligning with GAE Blobstore?

Following the documentation provided by GAE, I have implemented an upload handler to upload blobstore. However, when I select a file on my computer and click the Submit button on the HTML page, it displays 'The url "/upload" does not match any handler ...

Selenium code refuses to shut down

Why is the following code not terminating? import selenium from selenium.webdriver.common.keys import Keys browser = selenium.webdriver.Firefox() browser.get("http://www.quora.com/physics") element = browser.find_element_by_class_name("cancel") #ele=el ...

Utilize Pandas to back fill values from a different column in a DataFrame

Looking at a dataframe like this: val1 val2 9 3 2 . 9 4 1 . 5 1 I'm trying to fill in the missing values in 'val2' by referencing 'val1', resulting in: val1 val2 9 3 2 9 9 4 1 3 5 1 Essentially, I wa ...

What is the best way to access a dropdown menu by clicking?

IMAGE code driver.find_element((By.XPATH, "//li[contains(@class,'ellipsis1')][1]")).click() error selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string if bug code change t ...

Encountering a "File Not Found" error when attempting to load and read a JSON file within a Jupyter notebook in

I am currently working within the IBM Cloud Watson Studio environment. In my project, I have uploaded a JSON file containing updated population data per municipality in Norway. However, when attempting to call and read this file using "with open" ...

Accessing the value of a specific index in Python by reading a .txt file into an array

I have a large .txt data file presented in the following format (composed of all numbers): 0 1.2 2 3.1 20 21.2 22 23 30 31 32 33.01 The goal is to extract the values from the second column of this matrix and store them into a variable. I wrote the code b ...

The Chromedriver Manager fails to download after being converted to a .exe file using PyInstaller

Hello everyone at Stackoverflow, I have been using Selenium and Python to automate a task successfully in myscript.py. In an attempt to convert it to myscript.exe, I used the following command with PyInstaller: PyInstaller --onefile --windowed --icon=&quo ...

When working with Selenium and Python, I am unable to successfully close a dialog box

Currently, I am utilizing Selenium to gather data. However, there is an issue that arises when a message window appears. This window serves as just a manual, and I would like to disable it. Despite attempting various methods such as using find element by i ...

What is the best way to access a Python file from both within the same folder and from outside of it

I am facing an issue with the file hierarchy in my project. Here is how the files are structured: folder | classes | class1.py class2.py test_class2.py main.py Apologies, I am unable to format text properly. The main.py and classes fold ...

Scraping Data from a Table Using Selenium and Python 3.6 on Windows 10

Attempting to scrape data from a website and write it to a CSV file has been quite challenging. After successfully writing to the file, there seems to be an issue with only extracting data from the first row of the webpage. It's clear that something i ...

Accessing new data added with ForeignKey in Django Admin Area

Let's consider a scenario where we have the following models: # models.py from django.db import models class Category(models.Model): title = models.CharField(max_length=60) class CategoryDescription(models.Model) category = models.Foreign ...

Creating a process to build a fresh dataframe by selecting certain columns from an existing dataframe without the need for any user-defined variables

Solving the Problem: I am working on creating a method that can connect specific columns in a dataframe to generate a new dataframe without specifying the exact columns initially. The intention is to stack the columns side by side for easier data analysis. ...

Exploring data in Python: Reading CSV files column-wise

I have a python program that needs to manipulate data from a csv file. The file contains rows of varying lengths, like the example below: 1,B,T,P,B,B,B,P,P,P,T,P,P,P,P,T,B,P,P,B,P,P,B,B,P,P 2,T,P,B,P,B,B,P,P,B,B,T,P,B,B,T,P,P,B,B,B,B,P,T,B,T,B,B,B,P 3,P,P ...

Utilizing Django's FileDescriptor within configuration settings

I'm working on a Django project and I want to utilize the MaxMind db file to gather information about IP addresses. The file is quite large, around 90 megabytes. With an expected rate of about 50 requests per second, and the application being hosted o ...

Python code to create a table from a string input

Suppose I have a table like this: greeting = ["hello","world"] Then, I convert it into a string: greetingstring = str(greeting) Now the challenge is to change it back into a table. What would be the approach for accomplishing this tas ...

Exception Encountered - Restrict output to error message exclusively

I have the following code snippet and am interested in printing only the error message (No data found, symbol may be delisted). Can anyone suggest a cleaner implementation? import yahoo_fin.stock_info as si import sys try: quoteinfo = si.get_data(&quo ...

LibGDX's latest project featuring a vibrant yellow warning triangle

Recently, I embarked on a new endeavor with LibGDX. While normally I overlook the presence of yellow warning triangles in my projects, this time it has become somewhat bothersome. Is there a way to address this issue? Here is a visual representation: ...