How to print values of float tuple in list in integer? - python-3.x

list = [(769.0, ), (806.0, )]
In above case, I want to print only 769, 806.
How to print tuple's from list?

I think what you are looking for is how to print the first element in list of tuples?
For a list of tuples with length 2, and you do not care about the 2nd element.
for (fp, _) in list:
print(fp)
For a list of tuples with arbitrary tuple length.
for x in list:
print(x[0])

Related

How to convert a tuple list string value to integer

I have the following list:
l = [('15234', '8604'), ('15238', '8606'), ('15241', '8606'), ('15243', '8607')]
I would like to converted it such that the tuple values are integers and not string. How do I do that?
Desired output:
[(15234, 8604), (15238, 8606), (15241, 8606), (15243, 8607)]
What I tried so far?
l = [('15234', '8604'), ('15238', '8606'), ('15241', '8606'), ('15243', '8607')]
new_list = []
for i in `l:
new_list.append((int(i[0]), i[1]))
print(tuple(new_list))
This only converts the first element i.e. 15234, 15238, 15241, 15243 into int. I would like to convert all the values to int. How do I do that?
The easiest and most concise way is via a list comprehension:
>>> [tuple(map(int, item)) for item in l]
[(15234, 8604), (15238, 8606), (15241, 8606), (15243, 8607)]
This takes each tuple in l and maps the int function to each member of the tuple, then creates a new tuple out of them, and puts them all in a new list.
You can change the second numbers into integers the same way you did the first. Try this:
new_list.append((int(i[0]), int(i[1]))

How to subtract adjacent items in list with unknown length (python)?

Provided with a list of lists. Here's an example myList =[[70,83,90],[19,25,30]], return a list of lists which contains the difference between the elements. An example of the result would be[[13,7],[6,5]]. The absolute value of (70-83), (83-90), (19-25), and (25-30) is what is returned. I'm not sure how to iterate through the list to subtract adjacent elements without already knowing the length of the list. So far I have just separated the list of lists into two separate lists.
list_one = myList[0]
list_two = myList[1]
Please let me know what you would recommend, thank you!
A custom generator can return two adjacent items at a time from a sequence without knowing the length:
def two(sequence):
i = iter(sequence)
a = next(i)
for b in i:
yield a,b
a = b
original = [[70,83,90],[19,25,30]]
result = [[abs(a-b) for a,b in two(sequence)]
for sequence in original]
print(result)
[[13, 7], [6, 5]]
Well, for each list, you can simply get its number of elements like this:
res = []
for my_list in list_of_lists:
res.append([])
for i in range(len(my_list) - 1):
# Do some stuff
You can then add the results you want to res[-1].

python- list of tuples loop; check if any tuple element is in the next tuple

I have a list of tuples like:
list=[('Jim','Pam'),('Jim','Homer'),('Bart','Marge')]
I am trying to compare the elements of the current tuple with the elements of the next tuple and print "back to back".
In my list, 'Jim' appears in list element 0 and list element 1, so it should print back to back. This is not the case in list elements 1 and 2.
I've attempted:
for pair in list:
for i in range(len(list)):
if pair[0] or pair[1] in list[i+1]:
print("back to back")
You can zip list with list[1:] to get all adjacent pairs.
for a, b in zip(lst, lst[1:]):
if any(x in b for x in a):
print("back to back")

How to use re.compile within a for loop to extract substring indices

I have a list of data from which I need to extract the indices of some strings within that list:
str=['cat','monkey']
list=['a cat','a dog','a cow','a lot of monkeys']
I've been using re.compile to match (even partial match) individual elements of the str list to the list:
regex=re.compile(".*(monkey).*")
b=[m.group(0) for l in list for m in [regex.search(l)] if m]
>>> list.index(b[0])
3
However, when I try to iterate over the str list to find the indices of those elements, I obtain empty lists:
>>> for i in str:
... regex=re.compile(".*(i).*")
... b=[m.group(0) for l in list for m in [regex.search(l)] if m]
... print(b)
...
[]
[]
I imagine that the problem is with regex=re.compile(".*(i).*"), but I don't know how to pass the ith element as a string.
Any suggestion is very welcome, thanks!!
It looks like you need to use string formatting.
for i in str:
match_pattern = ".*({}).*".format(i)
regex = re.compile(match_pattern)
b = [m.group(0) for l in list for m in [regex.search(l)] if m]
print(b)

Invalid Syntax in Python 3 when creating a list of tuples

I am trying to create a list of tuples and I am getting Invalid Syntax:
def match_enzymes(strand, enzymes, sequences):
'''(str, list of str, list of str) -> list of (str, list of int) tuples
Return a list of tuples where the first item of each tuple is the name of a restriction enzyme and the second item is the list of indices of the restriction sites that the enzyme cuts.
>>>
>>>
>>>
'''
list_of_tuples = []
for i in range(len(enzymes)):
list_of_tuples.append((enzymes[i], restriction_sites(strand, sequence[i]))
return list_of_tuples
two problems:
1) you are missing the closing parentheses in:
list_of_tuples.append((enzymes[i], restriction_sites(strand, sequence[i])) #<--!!!
2) your code is not indented currently

Resources