Python 3.4: adding value to list if condition exists - list

i have a scenario like this one:
mainList = [[9,5],[17,3],[23,1],[9,2]]
secondaryList = [9,12,28,23,1,6,95]
myNewList = []
myNewList.append([[a,b] for a,b in mainList if a in secondaryList])
this, return me to me:
myNewList = [[9,5],[23,1],[9,2]]
but I need only the first occourance of "a". In other words I need to obtain:
myNewList = [[9,5],[23,1]]
How can I achieve this?

First of all:
myNewList = []
myNewList.append([[a,b] for a,b in mainList if a in secondaryList])
simply is the same as
myNewList = [[a,b] for a,b in mainList if a in secondaryList]
Then:
What you're building is functionally a python dictionary. Your two-element lists in mainList are the same as dict.items()!
So what you'd do is build a dict out of mainList (reversing it, because usually, you'd just save the last, not the first occurence):
mainDict = dict([reversed(mainList)])
Then you just make your new list:
myNewList = [ (key, mainDict[key]) for key in secondaryList ]

You can use a set to store the first elements and then check for existing the first element before adding the sub-lists :
>>> seen=set()
>>> l=[]
>>> for i,j in mainList:
... if i in secondaryList and i not in seen:
... seen.add(i)
... l.append([i,j])
...
>>> l
[[9, 5], [23, 1]]
Or you can use collections.defaultdict and deque with specifying its maxlen.But note that you need to loop over your list from end to start if you want the first occourance of a because deque will keep the last insert value :
>>> from collections import defaultdict
>>> from functools import partial
>>> d=defaultdict(partial(deque, maxlen=1))
>>> for i,j in mainList[::-1]:
... if i in secondaryList:
... d[i].append(j)
...
>>> d
defaultdict(<functools.partial object at 0x7ff672706e68>, {9: deque([5], maxlen=1), 23: deque([1], maxlen=1)})

Related

Python: Add Two Integer Lists of Different Lengths Together as One List

What I have:
first = [1,2,3]
second = [1,2,3,4]
What I want:
third = [2,4,6,4]
first + second = third
I've tryed using numpy and zips but they all stop after the smallest list is complete.
from itertools import zip_longest
first = [1,2,3]
second = [1,2,3,4]
third = []
for i,z in zip_longest(first,second):
if i != None:
third.append(i+z)
else:
a = 0
third.append(a+z)
print(third)
Here hope you like it

Shrink tuples in list of tuples in Python

I have a list of tuples. How can I drop some elements in all tuples? E.g.
[...,(23188,'Bob',1944,'Dentist','Houston'),(44512,'Charlie',1961,'Teacher','Boston'), ...]
should become
[...,('Bob',1944,'Houston'),('Charlie',1961,'Boston'), ...]
You can use like this.
>>>
>>> l1 = [(23188,'Bob',1944,'Dentist','Houston'),(44512,'Charlie',1961,'Teacher'
,'Boston')]
>>>
>>> import operator
>>> pickup = operator.itemgetter(1,2,4)
>>> map(pickup,l1)
[('Bob', 1944, 'Houston'), ('Charlie', 1961, 'Boston')]
>>>

how to get the list of the lists?

I have a problem like that:
list = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
I want to get a new list like that
new_list['a1','b1','c1','d1']
I do like this:
lst = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
for item in lst:
print(item)
result is:
a1
['b1', 2]
['c1', 2, 3]
['d1', 2, 3, 4]
But I want the first element of each result
The best answer is like this :
my_list = list()
lst = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
for element in lst:
if type(element)==type('string'):
my_list.append(element)
else:
my_list.append(element[0])
print(my_list)
Thank you!
Do it as below:
>>> my_list = list()
>>> lst = ['a1',['b1',2],['c1',2,3],['d1',2,3,4]]
>>> for element in lst:
if type(element)==type('string'):
my_list.append(element)
else:
my_list.append(element[0])
It will produce:
>>> my_list
['a1', 'b1', 'c1', 'd1']
>>>
As you see above, first I created a list (named my_list) and then checked each elements of your list. If the element was a string, I added it to my_list and otherwise (i.e. it is a list) I added the first element of it to my_list.
I would do
res = []
for x in the_list:
if x is Array:
res.append(x[0])
else:
res.append(x)

python: copying list and appending in one step

I have a strange reaction of python (using 2.7) here. I am trying to copy a list and append something to the copy at the same time. Here is the code:
myList = [1]
>>> newList = list(myList).append(2)
>>> newList
>>> print newList
None
>>> type(newList)
<type 'NoneType'>
Why is it that I get a NoneType object instead of my appended list-copy?
I stumbled over this when I tried to take a list1 copy it as many times as a list2 and append the elements of list2 to the ones in list1.
>>> list1 = [1,2]
>>> list2 = [3,4]
>>> list3 = [list(list1).append(i) for i in list2]
>>> list3
[None, None]
I expected:
>>> list3
[[1,2,3],[1,2,4]]
Why is it None,None?
Thanks a lot!
You can do this by adding a extra line:
myList=[1]
myList.append(2);newList=myList
You can also extend (append )list directly like:
list1 = [1,2]
list2 = [3,4]
list1.extend(list2);list3=list1
If u dont want to alter then try this:
list1 = [1,2]
list2 = [3,4]
list3=list1;list3.extend(list2)
And also:
myList=[1]
newList=myList;newList.append(2)
The append function modifies a list and returns None. Newlist was None because append() modifies the list directly, rather than returning the modified list.
This code will create the new list and add to it in one step.
myList = [1]
newList = myList + [2]

Python: re.sub single item in list with multiple items

I'm new to Python and trying to use re.sub or other approach to find individual items in a list and replace with multiple items. For example:
import re
list = ['abc', 'def']
tolist = []
for item in list:
a = re.sub(r'^(.)(.)(.)$', '\\1\\2', '\\2\\3', item)
tolist.append(a)
print tolist # want: ['ab', 'bc', 'de', 'ef']
The '\1\2', '\2\3' part clearly doesn't work, just there to lamely illustrate the idea.
You could pair characters without regexes:
lst = ['abc', 'def']
result = [a+b for chars in lst for a, b in zip(chars, chars[1:])]
print(result)
# -> ['ab', 'bc', 'de', 'ef']
Here's a rather generic approach where you have a list of tuples for all the substitutions you want to do with each item:
In [1]: import re
In [2]: subs = [(r'^(.)(.)(.)$', r'\1\2'), (r'^(.)(.)(.)$', r'\2\3')]
In [3]: inlist = ['abc', 'def']
In [4]: [re.sub(*sub, string=s) for s in inlist for sub in subs]
Out[4]: ['ab', 'bc', 'de', 'ef']
The second element in each tuple can also be a function, because re.sub allows it. I renamed your initial list because list is a built-in type name and shouldn't be used for variables.
>>> res = []
>>> m = re.compile('(..)')
>>> for items in list:
... for p in range(0,len(items)):
... r = m.search(items[p:])
... if r != None:
... res.append(r.group())
make a regexp that matches two characters and groups them
first for loop, iterate the list
second for loop, character indexes in each list item
search for the character pairs starting at further on offsets
store anything that's found