Let's say I have this multi-dimensional table called "a":
[['John', 8, 'Student' ], ['Paul', 22, 'Car Dealer'], ['Nick', 30, 'Doctor' ], ['Mark', 66, 'Retired' ]]
Is there a built in function in python to count how many columns are there (4) instead of doing something like this?:
f = 0 for item in a: f = f + 1
also can I have the two above lines combined in one?
With a built-in function:
how can I search in the names, in the first column to find if a name exists?
how can I search the whole table if a value exists?
len for rows:
table=[['John', 8, 'Student' ], ['Paul', 22, 'Car Dealer'], ['Nick', 30, 'Doctor', 'this row is longer..','making 5'], ['Mark', 66, 'Retired' ]] y=len(table) # 4
And then you will have to go row by row to find the max width:
x=max(len(row) for row in table) # 5
You can use a list comprehension to get a vertical column's values:
>>> [li for li in table] ['John', 'Paul', 'Nick', 'Mark']
To find a value, you can use a generator expression with any or just test membership:
any('John' in l for l in table) # True 'Paul' in (li for li in table) # True
To find which row, use a list comprehension:
[i for i,l in enumerate(table) if 'Mark' in l] # 3