What is the best way to split a list within a list?

I'm trying to display the list in a matrix format, but I'm struggling with line breaks. How can I achieve this without using numpy? I attempted using join method, but it didn't work.

Here is the data provided (txt file):

ControlPoint Longitude Latitude

A 30 0

B 60 0

C 60 -30

D 30 -30
import math

Coordi = []    
with open('2432004k_coordi_new.txt') as C:
    for item in C:
        Coordi.append([i for i in item.split()])
        Coordi = Coordi.join("\n")

print ("Coordi=")
print Coordi
Coordi =

[[ 30.   0.]

 [ 60.   0.]

 [ 60. -30.]

 [ 30. -30.]]

Answer №1

If you're looking for a customized print format without relying on numpy, consider creating your own printing function. Here's an example of how it could be done:

def prettyprint(matrix):
    print("[", matrix[0])
    index = 1
    while index < len(matrix) - 1:
        print(" ", matrix[index])
        index += 1
    print(" ", matrix[index], "]")

You can then use this function to print your matrix.

For more detailed formatting within the matrix, you can iterate over each row and define how each value should be displayed. Here's a suggestion:

print("[", end="")
for value in row:
    print(value, " ", end="")  # prevent new line character from being printed
print("]", end="")

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

Guide on how to send a file to DHC-REST/HTTP API using Selenium

In an attempt to send a POST request using the Chrome extension DHC, I encountered an issue where the uploaded file's size appeared as 0 bytes. The code snippet I used is as follows: from selenium.webdriver.chrome.options import Options from selenium ...

Error: The function 'fetch' is not recognized in Selenium Console

Having some trouble with Selenium and Chrome Developer Tools. I want to open Selenium, go to a URL, and then use driver.execute_script to make a fetch request via Console in Chrome Developer Tools within the Selenium window. However, when I try to run thi ...

Error Message: 'keras' module does not have a '__version__' attribute

import keras keras.__version__ While working in a .ipynb notebook in VSCode, I attempt to import Keras. To check if Keras is imported correctly, I inquire about the version of Keras that is currently "running". However, I encounter the following error mes ...

Using numbers such as 1, 2, 3 in Python to execute various sections of code - a guide

Just starting out on stack overflow and diving into Python with version 3.8.2. Currently experimenting with coding on . Working on a math solver that uses specific formulas, including print functions, input functions, and integer variables. The goal is t ...

Automating button clicks with Python and Selenium (Updated for a different problem)

After encountering ad pop-ups on a previous mp3 converter site, I decided to try using an alternative website, h2converter.com/tr/, for my script. However, this time the web driver had trouble finding the button and the program stopped due to a timeout e ...

Is it possible for Tornado to follow a different route depending on the Content-Type header?

Is it possible to set up routing for the same REST route (e.g. /message) so that different handlers are executed depending on the value of the Content-Type header? ...

StringIO and pystache are known to produce unexpected null characters

After rendering a mustache file into a string, I attempted to process it with the csv module. To do this, I created a file-like interface using StringIO. However, when using the csv module, I encountered the following error: _csv.Error: line contains NULL ...

Two applications installed on a single server, each running on its own port. The first application is unable to communicate with the second

I have two applications running on the same VPS (Ubuntu) and domain. The first is a Node.js app running on port 80, while the second is a Python (Flask) app running under Apache on port 3000. The Python app has a simple API with one endpoint /test. When I ...

What are the steps for utilizing `pytest` in Python?

Currently, I am involved in a project that has recently transitioned to using the pytest and unittest framework. Previously, I would run my tests from Eclipse to utilize the debugger for analyzing test failures by placing breakpoints. However, this method ...

Error encountered: Element not clickable - The attempt to click on a download link was interrupted due to the element being intercepted

When using Selenium and Python, I am working on automating the process of clicking on targets.simple.csv to download a .csv file from the following page: This is the code snippet I have written: import pandas as pd import numpy as np from datetime import ...

Modifying values within inner dictionaries based on certain conditions in Python

I have dictionaries within a dictionary and I need to replace any instances where the value of inner dictionaries is None with the string 'None' so that it will display in a CSV file without appearing as a blank cell. Here's an example: {O ...

Exploring the Magic of Casting in Python

When attempting to choose a value from a dropdown using Python, the code I have looks something like this: def selectDropdown(self, locator, value): select_box = self.driver.find_element_by_id(locator) select_box.click() options = [x for x in ...

Discovering an element within the identical class as another element using Selenium in Python

If I have already located an element, is there a method to find another in the same category? For instance: <div class="day"> <span class="day_number">4</span> <span class="day_item_time" data-day-total-time="day-total-time">1 ...

Can you elaborate on the purpose and functionality of the continue statement

Could someone provide an explanation for the continue statement? I've been struggling to comprehend it, despite my best efforts. The Python docs contain a sample program that I can't seem to figure out. for num in range(2, 10): if num % 2 == ...

Issues with Selenium getting stuck while using pyvirtualdisplay

Running Selenium with Python on a server requires the ability to hide the Chrome display. Most of the time, the Python script runs smoothly, but occasionally it gets stuck when creating a new chromedriver session. It's puzzling why this happens interm ...

How to organize dates in an Excel spreadsheet using Python

I am looking to extract specific data based on date from an imported excel file in Python. My goal is to provide a start and end date, and receive the data for that particular period. I have attempted various methods to install pandas_datareader in order t ...

Python script to extract product information from Walmart search results

Attempting to gather search results from Walmart has been a challenge for me. For instance, let's navigate to the website "" Then, try to extract only the text from the element identified by the class name search-product-result, using pyth ...

Extract specified characters from a text file and store them in a separate list

I have a file containing data in the following format: polas:872147 skridd:142017 My goal is to transform this data into a list with each line represented as a sublist, like so: ['polas', '872147'] ['skridd', '142017&apo ...

How can you pull information from Finnhub.io? (incorporating JSON into a pandas data table)

As a novice in coding, I am eager to learn and explore data from stock markets. My attempt at importing data from finnhub has not been successful so far. import requests import json r = requests.get('https://finnhub.io/api/v1/stock/candle?symbol=QQQ& ...

Issue with requests due to MissingSchema exception

I have a unique person class that provides detailed information about individuals, as shown below: Datatracker.py class @dataclass class Person: resource_uri: str id: int name: str name_from_draft: str ascii: str ascii_short: Opti ...