Extract sublist from list in Python - 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]]

Related

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

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']

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.

Splitting a single item list at commas

MyList = ['a,b,c,d,e']
Is there any way to split a list (MyList) with a single item, 'a,b,c,d,e', at each comma so I end up with:
MyList = ['a','b','c','d','e']
Split the first element.
MyList = ['a,b,c,d,e']
MyList = MyList[0].split(',')
Out:
['a', 'b', 'c', 'd', 'e']
Use the split method on the string
MyList = MyList[0].split(',')
See below
lst_1 = ['a,b,c,d,e']
lst_2 = [x for x in lst_1[0] if x != ',']
print(lst_2)
output
['a', 'b', 'c', 'd', 'e']

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')

python - how to concatenate two index from a list

I have a list:
listvalue = ['charity','hospital','carrefour']
I tried to concat two index from a list:
twoconcat = [listvalue[i:i + 2] for i in range(len(listvalue))]
The output I am getting is:
[['charity', 'hospital'], ['hospital', 'carrefour'], ['carrefour']]`
I want the output to be
[['charity','hospital'],['charity','carrefour'],['hospital','charity'],['hospital','carrefour'],['carrefour','charity'],['carrefour','hospital']]
Any suggestions?
You can do this using itertools.permutations.
>>> places = ['charity','hospital','carrefour']
>>> list(itertools.permutations(places, 2))
[('charity', 'hospital'), ('charity', 'carrefour'), ('hospital', 'charity'),
('hospital', 'carrefour'), ('carrefour', 'charity'), ('carrefour', 'hospital')]
If you do not care about list comprehension. This can be helpful,
runs on python 2.x
listvalue = ['charity','hospital','carrefour']
arr_len= len(listvalue)
result=[]
for i in range(arr_len):
for j in range(arr_len):
if i !=j:
result.append([listvalue[i],listvalue[j]])
print result
result
[['charity', 'hospital'], ['charity', 'carrefour'], ['hospital', 'charity'], ['hospital', 'carrefour'], ['carrefour', 'charity'], ['carrefour', 'hospital']]
l = ['a', 'b', 'c']
[[x, y] for x in l for y in l if y != x]
Output :
[['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'c'], ['c', 'a'], ['c', 'b']]
#Alasdair is right...use itertools.
Code:
from itertools import permutations
places = ['charity','hospital','carrefour']
result = list(permutations(places, 2)
Output:
[('charity', 'hospital'), ('charity', 'carrefour'), ('hospital', 'charity'), ('hospital', 'carrefour'), ('carrefour', 'charity'), ('carrefour', 'hospital')]
Code:
from itertools import permutations
places = ['charity','hospital','carrefour']
result = [list(place) for place in list(permutations(places, 2))]
Output:
[['charity','hospital'],['charity','carrefour'],['hospital','charity'],['hospital','carrefour'],['carrefour','charity'],['carrefour','hospital']]

Categories

Resources