extract from a list of lists
Question:
How can I extract elements in a list of lists and create another one in python. So, I want to get from this:
all_list = [['1 2 3 4','2 3 4 5'],['2 4 4 5', '3 4 5 5' ]]
a new list like this:
list_of_lists = [[('3','4'),('4','5')], [('4','5'),('5','5')]]
Following is what I did, and it doesn’t work.
for i in xrange(len(all_lists)):
newlist=[]
for l in all_lists[i]:
mylist = l.split()
score1 = float(mylist[2])
score2 = mylist[3]
temp_list = (score1, score2)
newlist.append(temp_list)
list_of_lists.append(newlist)
Please help. Many thanks in advance.
Answers:
It could work almost as-is if you filled in the value for mylist
— right now its undefined.
Hint: use the split
function on the strings to break them up into their components, and you can fill mylist with the result.
Hint 2: Make sure that newlist is set back to an empty list at some point.
You could use a nested list comprehension. (This assumes you want the last two “scores” out of each string):
[[tuple(l.split()[-2:]) for l in list] for list in all_list]
Adding to eruciforms answer.
First remark, you don’t need to generate the indices for the all_list list. You can just iterate over it directly:
for list in all_lists:
for item in list:
# magic stuff
Second remark, you can make your string splitting much more succinct by splicing the list:
values = item.split()[-2:] # select last two numbers.
Reducing it further using map or a list comprehension; you can make all the items a float on the fly:
# make a list from the two end elements of the splitted list.
values = [float(n) for n in item.split()[-2:]]
And tuplify the resulting list with the tuple
built-in:
values = tuple([float(n) for n in item.split()[-2:]])
In the end you can collapse it all to one big list comprehension as sdolan shows.
Of course you can manually index into the results as well and create a tuple, but usually it’s more verbose, and harder to change.
Took some liberties with your variable names, values would tmp_list
in your example.
{'a': '#', 'Y': 'z', '.': '1', 'Q': 'K', 'i': 'w', 'g': 'j', 'P': '/', 'v': 'B', '<': 's', '2': 'E', 'n': 'M', 'G': 'e', 'H': 'U', '9': 'g', 's': '7', '3': 'S', '(': '[', 'X': 'D', 'E': 'X', 'h': 'N', 'C': 'W', 'R': 'v', '>': 'I', '*': 'x', ',': 'd', '-': 'o', 'w': 'r', 'o': 'q', 'D': '9', '@': '_', 'U': ':', 'x': '+', '!': '%', 'I': '(', '0': '}', 'z': 'H', ' ': '$', '1': ';', 'W': 'Z', '5': 'G', 'r': 'n', 'q': '4', 'N': '3', 'l': ',', '%': ' ', '7': 'm', 'p': 'A', '_': '{', '=': 'P', 'f': 'f', 't': 'Y', '$': '5', '/': 'l', 'B': '!', 'e': 'R', ')': '=', 'O': '<', 'y': '-', '[': 'F', 'Z': '8', 'c': '0', 'K': 'y', ':': 'O', ';': '@', '#': 'i', "'": 'a', 'A': '6', '6': 'V', ']': "'", 'T': 'Q', 'k': 'u', 'M': 'h', 'S': 'C', '4': 't', 'F': ']', 'V': '2', 'L': '>', 'm': 'c', 'd': 'b', '+': 'L', 'j': '*', '8': ')', 'b': 'k', 'u': 'T', '{': 'p', '}': '.'}
How can I extract elements in a list of lists and create another one in python. So, I want to get from this:
all_list = [['1 2 3 4','2 3 4 5'],['2 4 4 5', '3 4 5 5' ]]
a new list like this:
list_of_lists = [[('3','4'),('4','5')], [('4','5'),('5','5')]]
Following is what I did, and it doesn’t work.
for i in xrange(len(all_lists)):
newlist=[]
for l in all_lists[i]:
mylist = l.split()
score1 = float(mylist[2])
score2 = mylist[3]
temp_list = (score1, score2)
newlist.append(temp_list)
list_of_lists.append(newlist)
Please help. Many thanks in advance.
It could work almost as-is if you filled in the value for mylist
— right now its undefined.
Hint: use the split
function on the strings to break them up into their components, and you can fill mylist with the result.
Hint 2: Make sure that newlist is set back to an empty list at some point.
You could use a nested list comprehension. (This assumes you want the last two “scores” out of each string):
[[tuple(l.split()[-2:]) for l in list] for list in all_list]
Adding to eruciforms answer.
First remark, you don’t need to generate the indices for the all_list list. You can just iterate over it directly:
for list in all_lists:
for item in list:
# magic stuff
Second remark, you can make your string splitting much more succinct by splicing the list:
values = item.split()[-2:] # select last two numbers.
Reducing it further using map or a list comprehension; you can make all the items a float on the fly:
# make a list from the two end elements of the splitted list.
values = [float(n) for n in item.split()[-2:]]
And tuplify the resulting list with the tuple
built-in:
values = tuple([float(n) for n in item.split()[-2:]])
In the end you can collapse it all to one big list comprehension as sdolan shows.
Of course you can manually index into the results as well and create a tuple, but usually it’s more verbose, and harder to change.
Took some liberties with your variable names, values would tmp_list
in your example.
{'a': '#', 'Y': 'z', '.': '1', 'Q': 'K', 'i': 'w', 'g': 'j', 'P': '/', 'v': 'B', '<': 's', '2': 'E', 'n': 'M', 'G': 'e', 'H': 'U', '9': 'g', 's': '7', '3': 'S', '(': '[', 'X': 'D', 'E': 'X', 'h': 'N', 'C': 'W', 'R': 'v', '>': 'I', '*': 'x', ',': 'd', '-': 'o', 'w': 'r', 'o': 'q', 'D': '9', '@': '_', 'U': ':', 'x': '+', '!': '%', 'I': '(', '0': '}', 'z': 'H', ' ': '$', '1': ';', 'W': 'Z', '5': 'G', 'r': 'n', 'q': '4', 'N': '3', 'l': ',', '%': ' ', '7': 'm', 'p': 'A', '_': '{', '=': 'P', 'f': 'f', 't': 'Y', '$': '5', '/': 'l', 'B': '!', 'e': 'R', ')': '=', 'O': '<', 'y': '-', '[': 'F', 'Z': '8', 'c': '0', 'K': 'y', ':': 'O', ';': '@', '#': 'i', "'": 'a', 'A': '6', '6': 'V', ']': "'", 'T': 'Q', 'k': 'u', 'M': 'h', 'S': 'C', '4': 't', 'F': ']', 'V': '2', 'L': '>', 'm': 'c', 'd': 'b', '+': 'L', 'j': '*', '8': ')', 'b': 'k', 'u': 'T', '{': 'p', '}': '.'}