I have a list that contain the column indexes as follows:
list1 = [0 ,2]
Another list of list would contain the file contents of a csv file as follows:
list2=[["abc", 1, "def"], ["ghi", 2, "wxy"]]
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
ie
newList = [["abc", "def"], ["ghi", "wxy"]]
I am having a hard time creating the sublists
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)
and as a 1 liner:
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']]
您可以使用List Comprehension
: -
newList = [[each_list[i] for i in list1] for each_list in list2]
If you are working with csv files, you don't need to reinvent the wheel. Take a look at the excellent csv
module.
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!
You could put Poete Maudit's answer into one line like so:
column = np.array(list_name)[:,column_number].tolist()
You could also keep it as a numpy array by removing .tolist()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.