multiple lists to rows and columns

ABCD

I've been looking for answers like panda and the sort, but i dont think i'm allowed to use it, so i'm hitting a wall on my assignment, would appreciate it if anyone can help me. also any suggestions on how to make my code be able to handle string values as inputs, and negative inputs? im very new to python.

n = int(input("enter a number:"))
x = range(int(1),n)
list_1 = [x+1 for x in range(n)]
list_1.insert(0,"m")
list_2 = [x+2 for x in range(n)]
list_2.insert(0,"m+1")
list_3 = [(x+1) ** (x+2) for x in range(n)]
list_3.insert(0,"m**(m+1)")
list_of_lists = [list_1, list_2, list_3]

if n>=0:
    for a in zip(*list_of_lists):
        print(*a)


elif n<0:
    print("sorry, only positive integers please")

all it gave was

m m+1 m**(m+1)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625

where I wanted it to be something like

m    m+1    m**(m+1)
1    2      1
2    3      8
3    4      81
4    5      1024
5    6      15625
solid.py

I think this is what you are looking for:

n = int(input("enter a number:"))
# Do the check earlier.
if n>=0:
  x = range(int(1),n)
  list_1 = [x+1 for x in range(n)]
  list_1.insert(0,"m")
  list_2 = [x+2 for x in range(n)]
  list_2.insert(0,"m+1")
  list_3 = [(x+1) ** (x+2) for x in range(n)]
  list_3.insert(0,"m**(m+1)")
  list_of_lists = [list_1, list_2, list_3]

  # Print here, use a sep(arator) of \t -> tab
  # instead of the default one which is space.
  for a in zip(*list_of_lists):
        print(*a, sep = '\t', end = '\n')

elif n<0:
    print("sorry, only positive integers please")

This prints in console:

enter a number: 5
m   m+1 m**(m+1)
1   2   1
2   3   8
3   4   81
4   5   1024
5   6   15625

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

Split multiple columns into rows

BigQuery pivot multiple rows into columns

Add multiple columns and rows to a table

Split strings in multiple columns into rows

Converting multiple Columns of a table into multiple Rows with all other rows repeating

Select rows where multiple column values are in multiple lists

compare multiple specific columns of all rows

Plot multiple rows of dataframe in pandas for specific columns

Convert Rows to Multiple columns in SQL Server

Combine selected columns of multiple rows into one string

R: Shift multiple columns by different number of rows

Selecting multiple rows in a column into different columns (SQL)

Omit rows where multiple columns contain NULL

Grouping by multiple columns to find duplicate rows pandas

Computing differences accross rows with multiple index columns

Looping through columns and rows with multiple criteria

Duplicating rows sequentially based on sum of multiple columns

SQL Compare rows of a table with multiple columns

Summing multiple rows having duplicate columns pandas

Combining rows in pandas DataFrame by comparing multiple columns

Filter rows conditionally across multiple columns in R

Remove duplicates in multiple columns and rows based on rule

Merge rows of multiple columns into one (row) R

bootstrap grid creation for blocks with multiple columns and rows

looping over a nested list with multiple rows and columns

Splitting a delimited string in to multiple columns and rows

Filter rows on multiple columns in data Factory

How to convert multiple rows into multiple columns based on value

how to groupby and join multiple rows from multiple columns at a time?

TOP lista

quentelabel

Arquivo