Inserting values into a Python list - list

I am attempting to insert items into a list based on the index found in list a.
a = [2,1,0]
b = ['c','b','a']
c =[]
for (number,letter) in zip(a,b):
c.insert(number,[number,letter])
print(c)
This outputs: [[0, 'a'], [2, 'c'], [1, 'b']]
But I expected: [[0, 'a'], [1, 'b'], [2, 'c']]
Why does this happen?

Here's what actually happens inside your loop.
>>> c = []
>>> c.insert(2,[2,'c'])
>>> c
[[2, 'c']]
>>> c.insert(1,[1,'b'])
>>> c
[[2, 'c'], [1, 'b']]
>>> c.insert(0,[0,'a'])
>>> c
[[0, 'a'], [2, 'c'], [1, 'b']]
As you can see, the position your values are inserted at is relative to the contents of the list at the time of insertion. I'd recommend just sorting the zipped list.

Related

How to match elements from arrays and only print matches?

I have two lists, i am trying to match one item from the first list to another from the second list under a certain condition (for example if they share the same number in the same location). i wrote my code to match the first set ['A','B','C',4,'D'] and only print the set from list2 that has 4 in the same location. so basically my output would be:
['A','B','C',4,'D']
[1, 2, 3, 4, 5]
well i can't figure out how to print only the match
here is my code:
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],[1,2,3,2,5]]
for var in list1:
print var
for i in range(0,len(list2)):
for var1 in list2:
if list1[0][3] == list2[i][3]:
print var1
Your program would become easier, if you used izip of itertools. Assuming you just need to print the elements
from itertools import izip
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],[1,2,3,2,5]]
for item1 in list1:
for item2 in list2:
for i,j in izip(item1, item2):
if i==j:
print i
By using izip two times, it would be much easier
from itertools import izip
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],[1,2,3,2,5]]
for i in izip(list1,list2):
for item1, item2 in izip(i[0],i[1]):
if item1 == item2:
print item1
Almost. I am not sure if that is what you wanted but the following code prints all pairs which have the same number in the 4th location of the array:
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],
['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],
[1,2,3,2,5]]
for t in list1:
print t
for b in list2:
if t[3] == b[3]:
print b
Output is:
['A', 'B', 'C', 4, 'D']
[1, 2, 3, 4, 5]
['A', 'B', 'C', 9, 'D']
['A', 'B', 'C', 5, 'D']
[1, 2, 3, 5, 5]
['A', 'B', 'C', 6, 'D']
['A', 'B', 'C', 7, 'D']
Is that what you were looking for?

insert in list of python

I want insert list in list_of_words includes some value such that any list placed in position -2 of the list_of_words but the data are not ordered as I want
def h(i):
a, b = i.split(';')
list_of_words = [['z', -1],['z', len(b)]]
for key, word in enumerate(a):
if b.find(word) != -1:
list_of_words.insert(-2, [word, b.find(word)])
print list_of_words
h("XMJYAUZ;MZJAWXU")
list
list_of_words = [['z', -1], ['z', 7]]
assumed after run code
list_of_words = [['z', -1], ['X', 5], ['M', 0], ['J', 2], ['A', 3], ['U', 6], ['Z', 1], ['z', 7]]
but
list_of_words = [['X', 5], ['M', 0], ['J', 2], ['A', 3], ['U', 6], ['Z', 1], ['z', -1], ['z', 7]]
why this is happen
If you want to place an item to the -2 position in a list, you will need to use
list_of_words.insert(-1, [word, b.find(word)])
instead of
list_of_words.insert(-2, [word, b.find(word)])
because the item will be inserted before the provided position rather than after.
Full code:
def h(i):
a, b = i.split(';')
list_of_words = [['z', -1], ['z', len(b)]]
for word in a:
if word in b:
list_of_words.insert(-1, [word, b.find(word)])
print list_of_words
h("XMJYAUZ;MZJAWXU")

convert a 2d tuple into a list

i'm not very familiar with python, but i need to convert a 2d tuple into a nested list, i searched on stack i couldn't find an answer, Example:
Tuple = {(1,3),(3,5),(5,6)}
i need it to be a list:
List = [[1,3],[3,5],[5,6]]
why i need to convert a tuple, tuple wont allow me to use .replace on the content of the tuple
i tried to uselist() as stated on the internet but it didn't convert the tuple, thank you.
You can try like this,
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> [list(item) for item in Tuple]
[[5, 6], [1, 3], [3, 5]]
Or, you can use map
>>> list(map(list, Tuple))
[[5, 6], [1, 3], [3, 5]]
You can simply use map function which performs better when you want to apply a built-in function on an iterable :
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> list(map(list,Tuple))
[[5, 6], [1, 3], [3, 5]]
You can try this:
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> [list(item) for item in Tuple]
[[5, 6], [1, 3], [3, 5]]
or You can use iterloops imap for better performance
>>>import itertools
>>> Tuple = {(1,3),(3,5),(5,6)}
>>> list(itertools.imap(list, Tuple))
[[5, 6], [1, 3], [3, 5]]

Nested list remove duplicate list

In a nested list (like the one below) that is continuous,
I want to remove duplicate entry where first and third are equal values. What is the most efficient way of doing this?
[[a, 1, a], [b, 1, b], [c, 2, d],[e, 4,g]
Return
[[c, 2, d],[e, 4,g]]
>>> seq = [['a', 1, 'a'], ['b', 1, 'b'], ['c', 2, 'd'],['e', 4, 'g']]
>>> seq = [item for item in seq if item[0] != item[2]]
>>> print seq
[['c', 2, 'd'], ['e', 4, 'g']]
What you want to do is go through each sublist, and go through each item in that sublist. I there is a duplicate item in that sublist set the flag to True and ignore it, if not then append that list to a new list.
lists = [['a', 1, 'a'], ['b', 1, 'b'], ['c', 2, 'd'],['e', 4,'g']]
newLists = []
for l in lists:
if l[0] != l[len(l) - 1]:
newLists.append(l)
print newLists

sum of items in a 2d list

Im trying ti implement a function evenrow() that takes a two dimensional list of integers and returns True if each row of the table sums up to an even number and False otherwise (i.e.., if some row sums up to an odd number)
usage
>>> evenrow([[1, 3], [2, 4], [0, 6]])
True
>>> evenrow([[1, 3], [3, 4], [0, 5]])
False
This is what I got so far:
def evenrow(lst):
for i in range(len(lst)-1):
if sum(lst[i])%2==0: # here is the problem, it only iterates over the first item in the lst [1, 3] - i cant figure this out - range problem?
return True
else:
False
How do I get the loop to iterate over every item [1, 3], [2, 4], [0, 6] in the list and not just the first?
well I have gotten this far now:
def evenrow(lst):
for i in range(len(lst)-1):
if sum(lst[i]) %2 >0:
return False
else:
return True
and i get the following answer when executing different lists:
>>> evenrow([[1, 3], [2, 4], [0, 6]])
True
>>> evenrow([[1, 3], [3, 4], [0, 5]])
False
>>> evenrow([[1, 3, 2], [3, 4, 7], [0, 6, 2]])
True
>>> evenrow([[1, 3, 2], [3, 4, 7], [0, 5, 2]])
True
(the last one is not correct though - should be False) I just dont get why this is not working...
You are returning too early. You should check for all the pairs, only returning True afterwards, or return False if a odd sum is encountered.
Spoiler alert:
def evenrow(lst):
for i in range(len(lst)-1):
if sum(lst[i]) % 2 != 0: # here is the problem, it only iterates over the first item in the lst [1, 3] - i cant figure this out - range problem?
return False
return True
This will achieve the goal.