I have this array:
lst = ['A', 'B', 'C']
How could I append a string 'D' to each element and convert every set as a tuple:
lst2= [('A', 'D'),
('B', 'D'),
('C', 'D')]
Like this, using a list comprehension:
lst = ['A', 'B', 'C']
lst2 = [(x, 'D') for x in lst]
lst2
=> [('A', 'D'), ('B', 'D'), ('C', 'D')]
By the way, it's a bad idea to call a variable list, that clashes with a built-in function. I renamed it.
alternative solution is use zip_longest
from itertools import zip_longest
list(zip_longest(['A', 'B', 'C'], [], fillvalue='D'))
the result wiil be:
[('A', 'D'), ('B', 'D'), ('C', 'D')]
list2 = [(i, 'D') for i in list]
(apart from the fact that list is a very bad variable name)
Another option using zip:
x = ['A', 'B', 'C']
res = list(zip(x,'D'*len(x)))
list1 = ['A', 'B', 'C']
list2 = []
for i in list1:
list2.append((i, 'D'))
print(list2)
You can use the function product():
from itertools import product
lst = ['A', 'B', 'C']
list(product(lst, 'D'))
# [('A', 'D'), ('B', 'D'), ('C', 'D')]
Related
My goal is to create 3 lists.
The 1st one is the input: choose 3 from ABCD to create AAA, ABC...etc
The 2nd one is the output: change the middle letter of each input and create a new list. eg: for AAA -> ABA,ACA,ADA. So 3 times the length of the input.
The third one is the Change: I want to name each change as c_i, for example, AAA->ABA is C1.
For Input,
>>> lis = ["A","B","C","D"]
>>> import itertools as it
>>> inp = list(it.product(lis, repeat = 3))
>>> print(inp)
[('A', 'A', 'A'), ('A', 'A', 'B'), ... ('D', 'D', 'C'), ('D', 'D', 'D')]
>>> len(inp)
64
But I am stuck on how to create the output list. Any idea is appreciated!
Thanks
You can use list comprehension:
import itertools
lst = ['A', 'B', 'C', 'D']
lst_input = list(itertools.product(lst, repeat=3))
lst_output = [(tup[0], x, tup[2]) for tup in lst_input for x in lst if tup[1] is not x]
lst_change = [f'C{i}' for i in range(1, len(lst_output) + 1)]
print(len(lst_input), len(lst_output), len(lst_change))
print(lst_input[:5])
print(lst_output[:5])
print(lst_change[:5])
# 64 192 192
# [('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'C'), ('A', 'A', 'D'), ('A', 'B', 'A')]
# [('A', 'B', 'A'), ('A', 'C', 'A'), ('A', 'D', 'A'), ('A', 'B', 'B'), ('A', 'C', 'B')]
# ['C1', 'C2', 'C3', 'C4', 'C5']
For each tuple in lst_input, the middle item is replaced by all the candidate characters, but the replacement is thrown out if that replacement character is the same as the original character (if tup[1] is not x).
Can someone help me in sorting below tuple in Python?
({'b', 'c', 'a'}, {('b', 'c'), ('a', 'b')})
Expected output:
({'a', 'b', 'c'}, {('a', 'b'), ('b', 'c')})
Your title and example are conflicting. You should consult the python documentation on set, tuple, and list
Some Examples:
a_list = ['b', 'c', 'a']
a_tuple = ('b', 'c', 'a')
a_set = {'b', 'c', 'a'}
a_list_of_tuples = [('b', 'c', 'a'), ('b', 'c'), ('a', 'b')]
a_list_of_tuples_and_lists = [('b', 'c', 'a'), [('b', 'c'), ('a', 'b')]]
This example works for both a list of lists/tuples and a tuple of sets/tuples, however it returns an actual list of tuples not a tuple of sets as provided in your expected output example.
my_list = [('b', 'c', 'a'), [('b', 'c'), ('a', 'b')]]
print(tuple((sorted(item) for item in my_list)))
-> (['a', 'b', 'c'], [('a', 'b'), ('b', 'c')])
my_tuple = ({'b', 'c', 'a'}, {('b', 'c'), ('a', 'b')})
print(tuple((sorted(item) for item in my_tuple)))
-> (['a', 'b', 'c'], [('a', 'b'), ('b', 'c')])
I'm assuming you're talking about a list of lists and that you want to first sort each list and then the whole list of lists.
You can do it as follows:
arr = [['b','c','a'],['b','c'],['a','b']]
for i in arr:
i.sort()
arr.sort(key=lambda x:x[0])
print(arr)
[['a', 'b', 'c'], ['a', 'b'], ['b', 'c']]
Remember the solution will be totally different if you have a tuple of sets or vice versa.
For example, I have a list like ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
In this list, I want to take elements that form () with 'A' elements.
This means, I want to take elements ('A', 'B') and ('A', 'C')
Not 'A', 'B' or ('C', 'D')
How can I get this?
l = ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
[x for x in l if type(x) == tuple and 'A' in x]
I would use a conditional list comprehension. I check if 'A' is in the list and if it is and the content isn't only A (hence the len check), then add it to the list.
temp = ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
results = [i for i in temp if 'A' in i and len(i) > 1]
print(results)
You can loop through like this:
new_lst = []
lst = ('A', 'B', 'C', 'D', ('A', 'B'), ('A', 'C'), ('C', 'D'))
for i in lst:
if len(i) > 1 and i[0] == "A":
new_lst.append(i)
print new_lst
I have a list like this:
list_input = [(a,b), (a,c), (a,d), (z,b), (z,e)]
I want to extract b, c and d when start it with "a" not with "z" and put in a list
I could not figure out how to do it, any advice?
Filter your list items on the first value, collecting the second:
[second for first, second in list_input if first == 'a']
Demo:
>>> list_input = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('z', 'b'), ('z', 'e')]
>>> [second for first, second in list_input if first == 'a']
['b', 'c', 'd']
You could also do it explicitly:
In [8]: [list_input[i][1] for i in xrange(len(list_input)) if list_input[i][0] =='a']
Out[8]: ['b', 'c', 'd']
Or;
list_input = [("a","b"), ("a","c"), ("a","d"), ("z","b"), ("z","e")]
print ([x[1] for x in list_input if x[0]=="a"])
>>>
['b', 'c', 'd']
>>>
Manipulate it with indices. You can display that specific pairs too;
print ([(x,x[1]) for x in list_input if x[0]=="a"])
output;
>>>
[(('a', 'b'), 'b'), (('a', 'c'), 'c'), (('a', 'd'), 'd')]
>>>
How to turn multiple lists into one list of sublists, where each sublist is made up of the items at the same index across the original lists?
lsta = ['a','b','c','d']
lstb = ['a','b','c','d']
lstc = ['a','b','c','d']
Desired_List = [['a','a','a'],['b','b','b'],['c','c','c'],['d','d','d']]
I can't seem to use zip here, so how would I do this?
List of list will give like this:
>>> [list(x) for x in zip(lsta, lstb, lstc)]
[['a', 'a', 'a'], ['b', 'b', 'b'], ['c', 'c', 'c'], ['d', 'd', 'd']]
>>>
Using zip, under duress:
>>> zip(lsta, lstb, lstc)
[('a', 'a', 'a'), ('b', 'b', 'b'), ('c', 'c', 'c'), ('d', 'd', 'd')]
If Python 3, you'll need to convert the zip to a list:
>>> list(zip(lsta, lstb, lstc))
[('a', 'a', 'a'), ('b', 'b', 'b'), ('c', 'c', 'c'), ('d', 'd', 'd')]