Create list of tuples from two lists - python

I have two lists of coordinates:
[37.773972, -122.431297]
[37.773972, -122.45]
I want to create a list of tuples like so:
[(37.773972, -122.431297), (37.773972, -122.45)]
I've tried using zip but that merges the two.
thanks

>>> lst1 = [37.773972, -122.431297]
>>> lst2 = [37.773972, -122.45]
>>> res = [tuple(lst1), tuple(lst2)]
>>> res
[(37.773972, -122.431297), (37.773972, -122.45)]

How about this:
l1 = [37.773972, -122.431297]
l2 = [37.773972, -122.45]
merged = [tuple(l1), tuple(l2)]
print(merged) # [(37.773972, -122.431297), (37.773972, -122.45)]

list1 = [37.773972, -122.431297]
list2 = [37.773972, -122.45]
tup=[tuple(list1),tuple(list2)]
print(tup)

You can convert a list into tuple using tuple(list name).
Like:
def convert(list):
return tuple(list)
list = [a, c, b, d]
print(convert(list))
Here I define a function which is stored as a list and then I return it as a tuple. I give the input for list and finally when I print the defined function, It displays a tuple format of the list given.

Related

Merge two lists of tuples. Desired list of tuples with two elements

I have two lists of tuples:
result = [(10002,), (10003,), (10004,), (10005,)...]
result1 = [('PL42222941176247135539240187',), ('PL81786645034401957047621964',), ('PL61827884040081351674977449',)...]
I want merge lists.
DESIRED OUPUT:
joined_result = [('PL42222941176247135539240187', 10002,), ('PL81786645034401957047621964', 10003,),('PL61827884040081351674977449', 10004,)...]
I think about zip, but is litte wrong.
[(('PL42222941176247135539240187',), (10002,)), (('PL81786645034401957047621964',), (10003,)), (('PL61827884040081351674977449',), (10004,))...]
How get desired output?
Just destructure the single-element tuples when you iterate using zip:
joined_result = [(x, y) for ((x,), (y,)) in zip(result1, result)]
It seems zip fails because you have a list of tuples not only a list.
In fact it creates a list of tuples of tuples.
Convert your list of tuples before passing in zip:
result = [x[0] for x in result]
result1 = [x[0] for x in result1]
joined_result = list(zip(result1, result))
or in one line:
joined_result = list(zip([x[0] for x in result1],[x[0] for x in result]))
Use:
result = [(10002,), (10003,), (10004,)]
result1 = [('PL42222941176247135539240187',), ('PL81786645034401957047621964',), ('PL61827884040081351674977449',)]
newResult = []
for i in range(len(result)):
result1[i] += result[i]
newResult.append(result1[i])
Warning!: it will break result1, so do result2 = result1 somewhere before the loop

Join characters from list of strings by index

For example, I have the following list.
list=['abc', 'def','ghi','jkl','mn']
I want to make a new list as:
newList=['adgjm','behkn','cfil']
picking every first character of each element forming a new string then appending into the new list, and then with the second character of every element and so on:
Thanks for the help.
One way is zipping the strings in the list, which will interleave the characters from each string in the specified fashion, and join them back with str.join:
l = ['abc', 'def','ghi','jkl']
list(map(''.join, zip(*l)))
# ['adgj', 'behk', 'cfil']
For strings with different length, use zip_longest, and fill with an empty string:
from itertools import zip_longest
l = ['abcZ', 'def','ghi','jkl']
list(map(''.join, zip_longest(*l, fillvalue='')))
# ['adgj', 'behk', 'cfil', 'Z']
You can try this way:
>>> list1 =['abc', 'def','ghi','jkl']
>>> newlist = []
>>> for args in zip(*list1):
... newlist.append(''.join(args))
...
>>> newlist
['adgj', 'behk', 'cfil']
Or using list comprehension:
>>> newlist = [''.join(args) for args in zip(*list1)]
>>> newlist
['adgj', 'behk', 'cfil']
You can try this:
list=['abc', 'def','ghi','jkl']
n = len(list[0])
newList = []
i = 0
for i in range(n):
newword = ''
for word in list:
newword += word[i]
newList.append(newword)
print(newList)

how to separate list with the length of nested list python?

I have a nested list like this:
a = [[0.00069053395], [0.7278625], [0.8591849, 0.86290157, 0.8703022], [0.9041279, 0.9102304, 0.91197634], [0.93096334, 0.93327045], [0.9456768, 0.95339334], [0.98164046, 0.9836741]]
and another list like below:
b = ['/home/shape/13.jpg', '/home/shape/5.jpg', '/home/shape/6.jpg', '/home/shape/0.jpg', '/home/shape/2.jpg', '/home/shape/1.jpg', '/home/shape/7.jpg', '/home/shape/11.jpg', '/home/cuts/shape/10.jpg', '/home/shape/4.jpg', '/home/shape/14.jpg', '/home/shape/12.jpg', '/home/shape/16.jpg', '/home/shape/8.jpg']
I want to make the list b similar to nested list a, with its length of its elements.
Expected output:
c = [['/home/shape/13.jpg'],['/home/shape/5.jpg'],['/home/shape/6.jpg', '/home/shape/0.jpg', '/home/shape/2.jpg'],['/home/shape/1.jpg', '/home/shape/7.jpg', '/home/shape/11.jpg'],['/home/cuts/shape/10.jpg', '/home/shape/4.jpg'],['/home/shape/14.jpg', '/home/shape/12.jpg'],['/home/shape/16.jpg', '/home/shape/8.jpg']]
Any suggestions would be helpful!
You need:
c = []
count = 0
for i in a:
c.append(b[count:count+len(i)])
count = count+len(i)
print(c)
Output:
[['/home/shape/13.jpg'], ['/home/shape/5.jpg'], ['/home/shape/6.jpg',
'/home/shape/0.jpg', '/home/shape/2.jpg'], ['/home/shape/1.jpg',
'/home/shape/7.jpg', '/home/shape/11.jpg'], ['/home/cuts/shape/10.jpg',
'/home/shape/4.jpg'], ['/home/shape/14.jpg', '/home/shape/12.jpg'],
['/home/shape/16.jpg', '/home/shape/8.jpg']]
you could use the iter built-in function with list comprehension:
it = iter(b)
[[next(it) for _ in l] for l in a]
output:
[['/home/shape/13.jpg'],
['/home/shape/5.jpg'],
['/home/shape/6.jpg', '/home/shape/0.jpg', '/home/shape/2.jpg'],
['/home/shape/1.jpg', '/home/shape/7.jpg', '/home/shape/11.jpg'],
['/home/cuts/shape/10.jpg', '/home/shape/4.jpg'],
['/home/shape/14.jpg', '/home/shape/12.jpg'],
['/home/shape/16.jpg', '/home/shape/8.jpg']]

Create a list of consequential alphanumeric elements

I have
char=str('DOTR')
and
a=range(0,18)
How could I combine them to create a list with:
mylist=['DOTR00','DOTR01',...,'DOTR17']
If I combine them in a for loop then I lose the leading zero.
Use zfill:
>>> string = "DOTR"
>>> for i in range(0, 18):
... print("DOTR{}".format(str(i).zfill(2)))
...
DOTR00
DOTR01
DOTR02
DOTR03
DOTR04
DOTR05
DOTR06
DOTR07
DOTR08
DOTR09
DOTR10
DOTR11
DOTR12
DOTR13
DOTR14
DOTR15
DOTR16
DOTR17
>>>
And if you want a list:
>>> my_list = ["DOTR{}".format(str(i).zfill(2)) for i in range(18)]
>>> my_list
['DOTR00', 'DOTR01', 'DOTR02', 'DOTR03', 'DOTR04', 'DOTR05', 'DOTR06', 'DOTR07', 'DOTR08', 'DOTR09', 'DOTR10', 'DOTR11', 'DOTR12', 'DOTR13', 'DOTR14', 'DOTR15', 'DOTR16', 'DOTR17']
>>>
You can do it using a list comprehension like so:
>>> mylist = [char+'{0:02}'.format(i) for i in a]
>>> mylist
['DOTR00', 'DOTR01', 'DOTR02', 'DOTR03', 'DOTR04', 'DOTR05', 'DOTR06', 'DOTR07', 'DOTR08', 'DOTR09', 'DOTR10', 'DOTR11', 'DOTR12', 'DOTR13', 'DOTR14', 'DOTR15', 'DOTR16', 'DOTR17']
Simply use list comprehension and format:
mylist = ['DOTR%02d'%i for i in range(18)]
Or given that char and a are variable:
mylist = ['%s%02d'%(char,i) for i in a]
You can, as #juanpa.arrivillaga also specify it as:
mylist = ['{}{:02d}'.format(char,i) for i in a]
List comprehension is a concept where you write an expression:
[<expr> for <var> in <iterable>]
Python iterates over the <iterable> and unifies it with <var> (here i), next it calls the <expr> and the result is appended to the list until the <iterable> is exhausted.
can do like this
char = str('DOTR')
a=range(0,18)
b = []
for i in a:
b.append(char + str(i).zfill(2))
print(b)

Converting Strings to two lists in Python

(This is probably really simple, but) Say I have this input as a string:
"280.2,259.8 323.1,122.5 135.8,149.5 142.9,403.5"
and I want to separate each coordinate point onto separate lists, for each x and y value, so they'd end up looking like this:
listX = [280.2, 323.1, 135.8, 142.9]
listY = [259.8, 122.5, 149.5, 403.5]
I'd need this to be able to start out with any size string, thanks in advance!
Copy and paste this and it should work:
s_input = "280.2,259.8 323.1,122.5 135.8,149.5 142.9,403.5"
listX = [float(x.split(',')[0]) for x in s_input.split()]
listY = [float(y.split(',')[1]) for y in s_input.split()]
This would work.
my_string="280.2,259.8 323.1,122.5 135.8,149.5 142.9,403.5"
listX =[item.split(",")[0] for item in my_string.split()]
listY=[item.split(",")[1] for item in my_string.split()]
or
X_list=[]
Y_list=[]
for val in [item.split(",") for item in my_string.split()]:
X_list.append(val[0])
Y_list.append(val[1])
Which version to use would probably depend on your personal preference and the length of your string.
Have a look at the split method of strings. It should get you started.
You can do the following:
>>> a ="280.2,259.8 323.1,122.5 135.8,149.5 142.9,403.5"
>>> b = a.split(" ")
>>> b
['280.2,259.8', '323.1,122.5', '135.8,149.5', '142.9,403.5']
>>> c = [ x.split(',') for x in b]
>>> c
[['280.2', '259.8'], ['323.1', '122.5'], ['135.8', '149.5'], ['142.9', '403.5']]
>>> X = [ d[0] for d in c]
>>> X
['280.2', '323.1', '135.8', '142.9']
>>> Y = [ d[1] for d in c]
>>> Y
['259.8', '122.5', '149.5', '403.5']
There's a magical method call str.split, which given a string, splits by a delimiter.
Assume we have the string in a variable s.
To split by the spaces and make a list, we would do
coords = s.split()
At this point, the most straightforward method of putting it into the lists would be to do
listX = [float(sub.split(",")[0]) for sub in coords]
listY = [float(sub.split(",")[1]) for sub in coords]
You can use a a combination of zip and split with a list comprehension:
s = "280.2,259.8 323.1,122.5 135.8,149.5 142.9,403.5"
l = zip(*[a.split(',') for a in s.split()])
This will return a list of 2 tuples.
To get lists instead, use map on it.
l = map(list, zip(*[a.split(',') for a in s.split()]))
l[0] and l[1] will have your lists.
if your list is huge, consider using itertools.izip()

Categories

Resources