python from ['a','b','c','d'] to ['a', 'ab', abc', 'abcd'] - python

I have a list ['a','b','c','d'], want to make another list, like this: ['a', 'ab', abc', 'abcd']?
Thanks
Tried:
list1=['a','b','c', 'd']
for i in range(1, (len(list1)+1)):
for j in range(1, 1+i):
print(*[list1[j-1]], end = "")
print()
returns:
a
ab
abc
abcd
It does print what i want, but not sure,how to add it to a list to look like ['a', 'ab', abc', 'abcd']

Use itertools.accumulate, which by default sums up the elements for accumulation like a cummulative sum. Since addition (__add__) is defined for str and results in the concatenation of the strings
assert "a" + "b" == "ab"
we can use accumulate as is:
import itertools
list1 = ["a", "b", "c", "d"]
list2 = list(itertools.accumulate(list1)) # list() because accumulate returns an iterator
print(list2) # ['a', 'ab', 'abc', 'abcd']

Append to a second list in a loop:
list1=['a','b','c', 'd']
list2 = []
s = ''
for c in list1:
s += c
list2.append(s)
print(list2)
Output:
['a', 'ab', 'abc', 'abcd']

list1=['a','b','c', 'd']
l = []
for i in range(len(list1)):
l.append("".join(list1[:i+1]))
print(l)
Printing stuff is useless if you want to do ANYTHING else with the data you are printing. Only use it when you actually want to display something to console.

You could form a string and slice it in a list comprehension:
s = ''.join(['a', 'b', 'c', 'd'])
out = [s[:i+1] for i, _ in enumerate(s)]
print(out):
['a', 'ab', 'abc', 'abcd']

You can do this in a list comprehension:
vals = ['a', 'b', 'c', 'd']
res = [''.join(vals[:i+1]) for i, _ in enumerate(vals)]

Code:
[''.join(list1[:i+1]) for i,l in enumerate(list1)]
Output:
['a', 'ab', 'abc', 'abcd']

Related

Rearrange python list to avoid any position matches

I have 2 lists list1 and list2. How would I rearrange list1 (without rearranging list2) so that there are no matches on any positions eg:
UNDESIRED: list1 = [‘A’, ‘B’, ‘C’] list2 = [‘X’, ‘B’, ‘Z’] as you can see the B is on the same position, 1, in both lists…so I would then like to rearrange list1 to list1 = [‘B’, ‘A’, ‘C’] or any other order where there are no positional matches with list2 WITHOUT rearranging list2
You could do this with the help of the itertools module. There may be better / more efficient mechanisms. Note that the process() function will return None when there is no possible solution.
import itertools
def process(L1, L2):
for s in set(itertools.permutations(L1, len(L1))):
if all([a != b for a, b in zip(s, L2)]):
return s
return None
list1 = ['A', 'B', 'C']
list2 = ['X', 'B', 'Z']
print(process(list1, list2))
from itertools import permutations
def diff(list1, list2):
for item in permutations(list1, len(list1)):
if all(map(lambda a, b: a!=b, item, list2)):
return list(item)
return None
list1 = ['A', 'B', 'C']
list2 = ['X', 'B', 'Z']
result = diff(list1, list2)
if result:
print(result, 'vs', list2)
['A', 'C', 'B'] vs ['X', 'B', 'Z']
I solved it using this piece of code - however, as noted above, there are many possible cases where this very solution may not work. In this very case, you can try this:
import random
list1 = ['A', 'B', 'C']
list2 = ['X', 'B', 'Z']
for i in list1:
for j in list2:
if i == j in list2:
while list1.index(i) == list2.index(j):
list1.remove(i)
z = len(list1)
rand = random.choice(range(z))
list1.insert(rand, i)
print(list1, list2)
Also, note that you use quite weird apostrophes inside the list: ‘ instead of ' or ". AFAIK, Python won't be able to comprehend them correctly.

Extract sublist from list in Python

how can I extract for example [['A', '123'], ['A', '456']] from mylist if I filtered by 'A'?
mylist = [['A', '123'],
['A', '456'],
['B','847'],
['B','677']]
Here are two ways to achieve the results you want.
mylist = [['A', '123'],
['A', '456'],
['B','847'],
['B','677']]
letter = 'A'
# Using list comprehension
print([l for l in mylist if l[0] == letter])
# Using filer function
print(list(filter(lambda l: l[0] == letter, mylist)))
I made a code for you.
mylist = [['A', '123'],
['A', '456'],
['B', '847'],
['B', '677']]
output = [lst for lst in mylist if 'A' in lst]
print(output)
Or you can use this code;
output = [lst for lst in mylist if 'A' == lst[0]]

creating new string with alternating elements in python

I need to make a new list that contains alternating elements from the two list from before.
example: listA = "a","b","c"
listB= "A","B","C"
the output should be "a","A","b","B","c","C"
def one_each(lst1,lst2):
newList=[]
for i in range(len(lst2)):
newList.append(lst1[i])
newList.append(lst2[i])
return newList
you have to use small length list to reiterate so, add if condition to get your length
try this one:
def one_each(lst1,lst2):
iRange=len(lst1)
if len(lst2)<iRange:
iRange=len(lst2)
newList=[]
for i in range(iRange):
newList.append(lst1[i])
newList.append(lst2[i])
return newList
print (['a','b','c'],['A','B','C','D'])
output:
['a', 'A', 'b', 'B', 'c', 'C', 'c']
Try using a single loop over the index range of one of the two lists, then append an element from each list at each iteration.
def one_each(lst1, lst2):
lst = []
for i in range(0, len(lst1)):
lst.append(lst1[i])
lst.append(lst2[i])
return lst
lst1 = ['a', 'b', 'c']
lst2 = ['A', 'B', 'C']
output = one_each(lst1, lst2)
print(output)
This prints:
['a', 'A', 'b', 'B', 'c', 'C']
Try this
I've used zip and concate all the elements.
listA = ["a","b","c"]
listB= ["A","B","C"]
print reduce(lambda x,y:x+y,zip(listA, listB))
Result: ('a', 'A', 'b', 'B', 'c', 'C')

How to change a nested list's list into a string?

For example, if my nested list is:
[['2HC'], ['4BB'], ['4BB'], ['2HC']]
How would I have the ['2HC']'s refer to 'A' and the ['4BB']'s refer to 'B', so that my result would be:
['A', 'B', 'B', 'A']
l = [['2HC',], ['4BB'], ['4BB'], ['2HC']]
mapping = {'2HC' : 'A', '4BB': 'B'}
new_list = [mapping[i[0]] for i in l]
print(new_list)
Output:
['A', 'B', 'B', 'A']
l = [['2HC'], ['4BB'], ['4BB'], ['2HC']]
d = {'2HC' : 'A', '4BB': 'B'}
map(lambda x: d[x[0]], l)
# ['A', 'B', 'B', 'A']
l = [['2HC'], ['4BB'], ['4BB'], ['2HC']]
lst = ['A' if x[0] == '2HC' else 'B' for x in l]
print(lst)
Output
['A', 'B', 'B', 'A']
You can flatten the list to make it easier, and then use a simple translation mapping:
from itertools import chain
nested_vs = [['2HC'], ['4BB'], ['4BB'], ['2HC']]
# flatten
vs = chain.from_iterable(nested_vs)
# translate
translations = {'2HC' : 'A', '4BB': 'B'}
translated = list(map(translations.get, vs))
Now translated holds:
['A', 'B', 'B', 'A']
An itertools solution for this problem-
data = [['2HC',], ['4BB'], ['4BB'], ['2HC']]
mapping = {'2HC' : 'A', '4BB': 'B'}
l = [mapping[i] for i in itertools.chain.from_iterable(data)]
Benefit of itertools is it will convert the list data to flat list even if the inner list contains more than one element.
For example if list data was -
[['2HC', '2HC'], ['4BB'], ['4BB'], ['2HC']]
we would still get the right output.

Combinations and Permutations of characters

I am trying to come up with elegant code that creates combinations/permutations of characters from a single character:
E.g. from a single character I'd like code to create these permutations (order of the result is not important):
'a' ----> ['a', 'aa', 'A', 'AA', 'aA', 'Aa']
The not so elegant solutions I have thus far:
# this does it...
from itertools import permutations
char = 'a'
p = [char, char*2, char.upper(), char.upper()*2]
pp = [] # stores the final list of permutations
for j in range(1,3):
for i in permutations(p,j):
p2 = ''.join(i)
if len(p2) < 3:
pp.append(p2)
print pp
['a', 'aa', 'A', 'AA', 'aA', 'Aa']
#this also works...
char = 'a'
p = ['', char, char*2, char.upper(), char.upper()*2]
pp = [] # stores the final list of permutations
for i in permutations(p,2):
j = ''.join(i)
if len(j) < 3:
pp.append(j)
print list(set(pp))
['a', 'aa', 'aA', 'AA', 'Aa', 'A']
# and finally... so does this:
char = 'a'
p = ['', char, char.upper()]
pp = [] # stores the final list of permutations
for i in permutations(p,2):
pp.append(''.join(i))
print list(set(pp)) + [char*2, char.upper()*2]
['a', 'A', 'aA', 'Aa', 'aa', 'AA']
I'm not great with lambdas, and I suspect that may be where a better solution lies.
So, could you help me find the most elegant/pythonic way to the desired result?
You can simply use the itertools.product with different repeat values to get the expected result
>>> pop = ['a', 'A']
>>> from itertools import product
>>> [''.join(item) for i in range(len(pop)) for item in product(pop, repeat=i + 1)]
['a', 'A', 'aa', 'aA', 'Aa', 'AA']

Categories

Resources