Extracting corresponding elements of sublist [duplicate] - python

This question already has answers here:
Transpose nested list in python
(4 answers)
Transpose list of lists
(14 answers)
Closed 5 years ago.
It should work like
input: list([[1,1,1],[2,2,2],[3,3,3]])
output: [[1,2,3], [1,2,3], [1,2,3]]
so far i have done this:
def list(m):
list2=[]
for i in range(0, len(m)):
list2.append([x[i] for x in m])
return(list2)
It's not working every time..
For example:
it's working for
input: list([[1,1,1],[2,2,2],[3,3,3]])
but not for
input: list([[1,3,5],[2,4,6]])

You would usually do that in Python using the zip function:
inp = list([[1,1,1],[2,2,2],[3,3,3]])
output = list(map(list, zip(*inp)))
print(output)
>>> [[1, 2, 3], [1, 2, 3], [1, 2, 3]]
The additional map call is to convert the elements returned by zip, that are tuples, into lists, but if you are okay with tuples you can just do:
inp = list([[1,1,1],[2,2,2],[3,3,3]])
output = list(zip(*inp))
print(output)
>>> [(1, 2, 3), (1, 2, 3), (1, 2, 3)]
Also, the outer list is only necessary in Python 3, where zip returns a generator, but not in Python 2.

Related

python reorder nested list from row to column [duplicate]

This question already has answers here:
Zip lists in Python
(10 answers)
Closed 2 years ago.
My input is
tbl_ports = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
And my expected output is
[[1,5,9], [2,6,10], [3,7,11], [4,8,12]]
My limit was to do the following to create the output reorder_list
reorder_list = []
for i in range(len(tbl_ports)):
for col in tbl_ports:
reorder_list.append(col[i])
reorder_list=[1, 5, 9, 2, 6, 10, 3, 7, 11]
How can I contain them in a list of 3 elements?
To fix the code that you already have, you need to create a new list every time a row is complete, such as:
reorder_list = []
for i in range(len(tbl_ports)):
reorder_list.append([])
for col in tbl_ports:
reorder_list[-1].append(col[i])
Which would yield the following result:
[[1, 5, 9], [2, 6, 10], [3, 7, 11]]
You can also use a more pythonic method of solving the problem,
list(zip(*tbl_port))
Which would yield a list of tuples:
[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]
If you want a list of lists, then you can simply just use list comprehension:
[list(e) for e in zip(*tbl_port)]
Edit:
for an explanation of why zip(*list) works, you need to know what zip does.
zip is a function in python that takes in multiple lists, and outputs a generator of lists for each element in every corresponding list. So zip([1, 2, 3], [4, 5, 6]) would yield [(1, 4), (2, 5), (3, 6)].
the * basically expands the input into multiple positional arguments, where function(*[1, 2, 3, 4]) is equivalent to function(1, 2, 3, 4)
So the code passed in the input array as a list of arguments to the zip function, which then outputs the result in the order that you want.
The only remaining problem is that zip generates a generator instead of an actual list.
To solve that problem, simply call the list function on a generator to convert it into a list, or pass it in a list comprehension to yield the desired result.
This is exactly what the zip() function is for.
list(zip([1,2,3,4],'abcd'))
You can use the unpack syntax " * " to make python unpack your lists to the zip function.
tbl_ports = [[1,2,3,4], [5,6,7,8], [9,10,11,12]]
reorder_list = list(zip(*tbl_ports))

Dictionary from list [duplicate]

This question already has answers here:
Convert [key1,val1,key2,val2] to a dict?
(12 answers)
Closed 5 years ago.
How to create a dictionary from List when my list is like below
L1 = [1,2,3,4,5,6,7,8,9]
and I want the result as a dictionary like
d = {'1':[2,3], '4':[5,6], '7':[8,9]}
Is any idea, how to implement this?
L = [1,2,3,4,5,6,7,8,9]
n = 3
dict = {}
for i in range(0, len(L), n):
#this creates chunks of size n
sub_list = L[i:i+n]
dict[sub_list[0]] = sub_list[1:]
print(dict)
To get dictionary entries of size 2, choose n=3 in this example.
one-liner:
dict((L1[i*3], [L1[i*3+1], L1[i*3+2]]) for i in xrange(len(L1)/3))
Explanation:
xrange(len(L1)/3) returns a lazy range from 0 to 2: [0, 1, 2].
Further for each element i of a range it transforms to a tuple (k, [v1, v2]]). So the final range will be [(1, [2, 3]), (4, [5, 6]), (7, [8, 9])]. And finally we use a dict constructor from list of tuples where each element considered as a key-value pair.
Final result is {1: [2, 3], 4: [5, 6], 7: [8, 9]}. To convert keys from number to string we need to replace L1[i*3] to str(L1[i*3]).

Can a set have lists as its elements? [duplicate]

This question already has answers here:
Add list to set
(13 answers)
How to make a set of lists
(4 answers)
How to convert list of lists to a set in python so I can compare to other sets?
(3 answers)
Closed 5 years ago.
I am trying to get unique lists which can consist duplicate values,can I use sets to get unique lists?
To be more specific ,here is an example:
my_list=[[1,2,1],[1,2,2],[1,2,2],[1,1,2],[2,2,2]]
what i would want is :
set_list=[[1,2,1],[1,2,2],[1,1,2],[2,2,2]]
is this possible?
Thanks in advance for your kind response :)
No, a list is not hashable. You will get the following error:
TypeError: unhashable type: 'list'
Given the list only contains hashable objects, you can however convert the list to a tuple and add the tuples. So you could do something like:
>>> my_list=[[1,2,1],[1,2,2],[1,2,2],[1,1,2],[2,2,2]]
>>> set_tuples = {tuple(a_list) for a_list in my_list}
>>> set_tuples
{(1, 2, 2), (1, 2, 1), (2, 2, 2), (1, 1, 2)}
You can then for instance construct a uniqueness filter with:
my_list=[[1,2,1],[1,2,2],[1,2,2],[1,1,2],[2,2,2]]
result = []
unique_set = set()
for sublist in my_list:
the_tuple = tuple(sublist)
if the_tuple not in unique_set:
unique_set.add(the_tuple)
result.append(sublist)
So all operations on the set are done with tuples. This gives:
>>> result
[[1, 2, 1], [1, 2, 2], [1, 1, 2], [2, 2, 2]]
Lists are not hashable, but you can have a set of tuples, :
set(map(tuple, my_list))
# {(1, 1, 2), (1, 2, 1), (1, 2, 2), (2, 2, 2)}

Python eliminate duplicates of list with unhashable elements in one line [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: removing duplicates from a list of lists
Say i have list
a=[1,2,1,2,1,3]
If all elements in a are hashable (like in that case), this would do the job:
list(set(a))
But, what if
a=[[1,2],[1,2],[1,3]]
?
Python 2
>>> from itertools import groupby
>>> a = [[1,2],[1,2],[1,3]]
>>> [k for k,v in groupby(sorted(a))]
[[1, 2], [1, 3]]
Works also in Python 3 but with caveat that all elements must be orderable types.
This set comprehension works for the List of Lists to produce a set of tuples:
>>> {(tuple(e)) for e in a}
set([(1, 2), (1, 3)])
Then use that to turn it into a list of lists again with no duplicates:
>>> [list(x) for x in {(tuple(e)) for e in a}]
[[1, 2], [1, 3]]

How to zip lists in a list [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 6 months ago.
I want to zip the following list of lists:
>>> zip([[1,2], [3,4], [5,6]])
[[1,3,5], [2,4,6]]
This could be achieved with the current zip implementation only if the list is split into individual components:
>>> zip([1,2], [3,4], [5,6])
(1, 3, 5), (2, 4, 6)]
Can't figure out how to split the list and pass the individual elements to zip. A functional solution is preferred.
Try this:
>>> zip(*[[1,2], [3,4], [5,6]])
[(1, 3, 5), (2, 4, 6)]
See Unpacking Argument Lists:
The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
>>> range(3, 6) # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> range(*args) # call with arguments unpacked from a list
[3, 4, 5]

Categories