I have the following sublist format:
x = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
y = [[11, 22, 33, 44], [55, 66, 77, 88], [99,100, 111, 122]...]
Above is a sublist containing the information of the "x" and "y" coordinates (the length can be changed accordingly).
Now I like take two sets and make pair of coordinates as following:
x_y = [[(1,11),(2,22),(3,33),(4,44)],[(5,55),(6,66),(7,77),(8,88)],[(9,99),(10,100),(11,111),(12,122)...]
any help would be appreciated. Thanks.
You could use zip twice:
x_y = [zip(a, b) for a, b in zip(x, y)]
print(x_y)
Output:
[[(1, 11), (2, 22), (3, 33), (4, 44)], [(5, 55), (6, 66), (7, 77), (8, 88)], [(9, 99), (10, 100), (11, 111), (12, 122)]]
I have the following sublist format:
mylist_x_y = [[1, 2, 3], [10, 20, 30]]
the [1, 2, 3] are X coordinates and the [10, 20, 30] Y coordinates.
Now I like them to be the following tuple format:
mylist_x_y_new = [(1, 10), (2, 20), (3, 30)]
Use zip with the list's elements:
mylist_x_y_new = zip(*mylist_x_y)
print(mylist_x_y_new)
Output:
[(1, 10), (2, 20), (3, 30)]
How can I convert for example
List(2, 5, 24, 7, 34, 8)
into
List(2, List(5, List(24, List(7, List(34, List(8))))))
I guess it is about folding right but couldn't figure it out.
x.foldRight(List[Any]())((a,b) => List(a,b)) will produce (2, (5, (24, (7, (34, (8, Nil)))))). If you really need the (34, 8) at the end, you can change the folding function to use some pattern matching.
Please how do i print pairs of X and t in PYTHON?
X = [8, 10, 1]
t = [10, 8, 1]
Something like
[[8, 10], [10, 8], [1,1]]
(Assuming you don't insist on getting a list of lists) you can use zip():
list(zip(X,t))
Result:
[(8, 10), (10, 8), (1, 1)]
I have multiple lists to work with. What I'm trying to do is to take a certain index for every list(in this case index 1,2,and 3), in a vertical column. And add those vertical numbers to an empty list.
line1=[1,2,3,4,5,5,6]
line2=[3,5,7,8,9,6,4]
line3=[5,6,3,7,8,3,7]
vlist1=[]
vlist2=[]
vlist3=[]
expected output
Vlist1=[1,3,5]
Vlist2=[2,5,6]
Vlist3=[3,7,3]
Having variables with numbers in them is often a design mistake. Instead, you should probably have a nested data structure. If you do that with your line1, line2 and line3 lists, you'd get a nested list:
lines = [[1,2,3,4,5,5,6],
[3,5,7,8,9,6,4],
[5,6,3,7,8,3,7]]
You can then "transpose" this list of lists with zip:
vlist = list(zip(*lines)) # note the list call is not needed in Python 2
Now you can access the inner lists (which in are actually tuples this now) by indexing or slicing into the transposed list.
first_three_vlists = vlist[:3]
in python 3 zip returns a generator object, you need to treat it like one:
from itertools import islice
vlist1,vlist2,vlist3 = islice(zip(line1,line2,line3),3)
But really you should keep your data out of your variable names. Use a list-of-lists data structure, and if you need to transpose it just do:
list(zip(*nested_list))
Out[13]: [(1, 3, 5), (2, 5, 6), (3, 7, 3), (4, 8, 7), (5, 9, 8), (5, 6, 3), (6, 4, 7)]
Use pythons zip() function, index accordingly.
>>> line1=[1,2,3,4,5,5,6]
>>> line2=[3,5,7,8,9,6,4]
>>> line3=[5,6,3,7,8,3,7]
>>> zip(line1,line2,line3)
[(1, 3, 5), (2, 5, 6), (3, 7, 3), (4, 8, 7), (5, 9, 8), (5, 6, 3), (6, 4, 7)]
Put your input lists into a list. Then to create the ith vlist, do something like this:
vlist[i] = [];
for l in list_of_lists:
vlist[i].append(l[i])