Python list column index

Extract specific columns from list

What can be the best way to create a new list that would only contain the values from list2 with the column numbers that are contained in list1 i.e.

6 Answers 6

You can use List Comprehension : —

newList = [[each_list[i] for i in list1] for each_list in list2] 

This doesn’t seem to be what the OP wants. Given columns [0, 2] in list1 , newList should contain those columns for each row in list2 .

If you are happy with a list of tuples, you can use operator.itemgetter

import operator list1 = [0,2] my_items = operator.itemgetter(*list1) new_list = [ my_items(x) for x in list2 ] 

(or you could use map here):

new_list = map(my_items, list2) 
new_list = map(operator.itemgetter(*list1), list2) 

operator.itemgetter probably has a slight performance advantage over nested list-comprehensions, but it’s likely to be small enough that it’s not worth worrying about.

>>> list1 = [0 ,2] >>> list2=[["abc", 1, "def"], ["ghi", 2, "wxy"]] >>> newList = [[l[i] for i in list1] for l in list2] >>> print newList [['abc', 'def'], ['ghi', 'wxy']] 

Extracting directly some columns from a python list of lists is not possible because exactly python does not perceive this list as an array (which has by definition rows and columns) but as a list of lists.

However, you can do something like this very easily without using any list comprehension by using Numpy . Specifically, you can do the following:

import numpy as np list1 = [0 , 2] list2=[["abc", 1, "def"], ["ghi", 2, "wxy"]] # Covert list2 to numpy array array2 = np.array(list2) # Extract the specific columns from array2 according to list1 newArray = array2[:, list1] # Convert the new numpy array to list of lists newList = newArray.tolist() # newList is the following list: [['abc', 'def'], ['ghi', 'wxy']] 

I hope that this helps too!

Источник

What’s the best way to access columns of an array in Python?

As @unutbu said, to achieve the same effect as array(:,2) in Matlab, use a[:, 1] , since it’s 0-based in Python.

Not sure if the question was general or with a view to use some numerical code. If it’s the latter, you should definitely look into Numpy/Scipy (perhaps the SciKits too depending on what you do). I wouldn’t try to do numerical code in Python without a library dedicated for this purpose.

Читайте также:  Read docx with python

You can group data in a two-dimensional list by column using the built-in zip() function:

>>> array=[[1,2,3],[4,5,6]] >>> zip(*array) [(1, 4), (2, 5), (3, 6)] >>> zip(*array)[1] (2, 5) 

Note that the index starts at 0, so to get the second column as in your example you use zip(*array)[1] instead of zip(*array)[2] . zip() returns tuples instead of lists, depending on how you are using it this may not be a problem, but if you need lists you can always do map(list, zip(*array)) or list(zip(*array)[1]) to do the conversion.

If you use Matlab, you probably will want to install NumPy: Using NumPy, you can do this:

In [172]: import numpy as np In [173]: arr = np.matrix('1 2 3; 4 5 6') In [174]: arr Out[174]: matrix([[1, 2, 3], [4, 5, 6]]) In [175]: arr[:,2] Out[175]: matrix([[3], [6]]) 

Since Python uses 0-based indexing (while Matlab uses 1-based indexing), to get the same slice you posted you would do:

In [176]: arr[:,1] Out[176]: matrix([[2], [5]]) 

It is easy to build numpy arrays of higher dimension as well. You could use np.dstack for instance:

In [199]: B = np.dstack( (np.eye(3), np.ones((3,3)), np.arange(9).reshape(3,3)) ) In [200]: B.shape Out[200]: (3, 3, 3) In [201]: B[. 0] Out[201]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]]) In [202]: B[. 1] Out[202]: array([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) In [203]: B[. 2] Out[203]: array([[ 0., 1., 2.], [ 3., 4., 5.], [ 6., 7., 8.]]) 

And here is the array formed from the second column from each of the 3 arrays above:

In [204]: B[:,1,:] Out[204]: array([[ 0., 1., 1.], [ 1., 1., 4.], [ 0., 1., 7.]]) 

Numpy doesn’t have a function to create magic squares, however. sniff

Источник

How to Find the Index of an Element in a List of Lists?

Be on the Right Side of Change

How to find the row and column index of the element x in the list of lists lst ?

If the element does not occur in a list, the return value should be the tuple (-1, -1) . If the element exists multiple times, the return value should be the (row, column) index of the first occurrence.

Here are three examples that demonstrate how your program should work under three important cases.

Example 1: Element Exists

Input: [[1, 2, 3], [4, 5, 6]] x = 5 Output: (1, 1)

Example 2: Element Doesn’t Exist

Input: [[1, 2, 3], [4, 5, 6]] x = 0 Output: (-1, -1)

Example 3: Element Exists Multiple Times

Input: [['Alice', 'Bob'], ['Carl', 'Dave', 'Emil'], ['Emil', 'Emil']] x = 'Emil' Output: [1, 3]

Let’s dive into the solutions next!

Читайте также:  Javascript get forms by name

Method 1: Basic Python For Loop & enumerate()

The simplest and most Pythonic way that finds the row and column indices in a general list of lists, is to use a nested for loop and the built-in enumerate() function to iterate over the elements and indices at the same time.

def find_element(x, lst): for i, row in enumerate(lst): for j, element in enumerate(row): if element == x: return (i, j) return (-1, -1)
  • The outer for loop iterates over the inner lists and their “row” indices using enumerate() . If you need a refresher on enumerate, check out my in-depth tutorial on the Finxter blog and watch the explainer video at the end of this section.
  • The inner loop iterates over each element in a given inner list, along with its “column” index.
  • As soon as you’ve found the element, return the tuple of the row and column indices (i, j) .

Let’s run our three test cases against it!

# Test Case 1: Element Exists lst = [[1, 2, 3], [4, 5, 6]] x = 5 print(find_element(x, lst)) # Test Case 2: Element Doesn't Exist lst = [[1, 2, 3], [4, 5, 6]] x = 0 print(find_element(x, lst)) # Test Case 3: Element Exists Multiple Times lst = [['Alice', 'Bob'], ['Carl', 'Dave', 'Emil'], ['Emil', 'Emil']] x = 'Emil' print(find_element(x, lst))

The output is the expected:

Before we dive into the next solution, feel free to find an in-depth explanation of the enumerate() function here:

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Читайте также:  Oracle sql developer unable launch java virtual machine

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Be on the Right Side of Change 🚀

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. 🤖
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources 🧑‍💻

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses 👑 to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Источник

Оцените статью