Reputation: 85
Recently I developed a program that takes a list of numbers and finds the biggest number out of them that is divisible by 3. It work however I don't understand how you change it to a normal list. Here is an example: I have
[(3, 3, 4), (3, 4, 3), (3, 3, 4)]
I would like to have
[334,343,334]
Thanks for the help
Following on from the great help I am receiving an unusual error to do with tuples Once again thanks for the help here it is;
import intercools
list1 = []
stuff = [1, 2, 3]
for L in range(0, len(stuff+1):
for subset in itertools.combinations(stuff, L):
list1.append(subset)
print(list1)
sep = [map(str,l)for l in list1]
nl = [int(''.join(s)) for s in sep]
print(nl)
Upvotes: 0
Views: 2199
Reputation: 9257
You can do it with literal_eval
from ast
module like this way:
from ast import literal_eval
a = [(3, 3, 4), (3, 4, 3), (3, 3, 4)]
b = [literal_eval("".join(str(j) for j in k)) for k in a]
print(b)
Output:
[334, 343, 334]
Also you can do it like this way:
a = [(3, 3, 4), (3, 4, 3), (3, 3, 4)]
b = [int("".join(str(j) for j in k)) for k in a]
Also, you can use map
method to do it:
a = [(3, 3, 4), (3, 4, 3), (3, 3, 4)]
# Or using literal_eval
b = [int("".join(map(str, k))) for k in a]
Upvotes: 0
Reputation: 103814
Given:
>>> LoT=[(3, 3, 4), (3, 4, 3), (3, 3, 4)]
You can use a string to join the tuple elements together then recast to an int:
>>> [int(''.join(map(str, t))) for t in LoT]
[334, 343, 334]
Upvotes: 1
Reputation: 8047
And yet another:
ml = [(3, 3, 4), (3, 4, 3), (3, 3, 4)]
sep = [map(str,l) for l in ml]
nl = [int(''.join(s)) for s in sep]
print(nl) # [334, 343, 334]
Upvotes: 1
Reputation: 6360
List comprehensions in Python
are your friend.
t = [(3, 3, 4), (3, 4, 3), (3, 3, 4)]
str_tuple = [(str(x), str(y), str(y)) for (x, y, z) in t]
final = [int(''.join(x)) for x in str_tuple]
# [334, 343, 334]
Upvotes: 0