Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Changing elements of a list to match another list
#1
I am trying to iterate through a list of list and increase the first element in each sublist until it matches an integer on another list...

list1=[[2,13,22,40],[8,13,22,40],[24,13,22,40]]
reference_list=[5, 10, 30]
for i in list1: 
    while i[0]!=i in reference_list:        
        i[0]=i[0]+1
print(list1)
Right now my output is:

[[2, 13, 22, 40], [8, 13, 22, 40], [24, 13, 22, 40]]
(Nothing is getting modified) My desired output is:

[[5, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]
(so that the first integer is increased until it matches an integer on the reference_list)

I have dabbled using the set function but I can't seem to get that to work.

Any ideas are appreciated!
Quote
#2
If I correctly understand the objective then no iteration is needed.

You just want to replace first element in every row in matrix with value which in reference list. As indexes of rows in matrixes and elements in reference list are the same one can just:

>>> lst = [[2,13,22,40],[8,13,22,40],[24,13,22,40]]                  
>>> reference = [5, 10, 30]                                          
>>> for i, v in enumerate(reference): 
...     lst[i][0] = v 
...
>>> lst                                                              
[[5, 13, 22, 40], [10, 13, 22, 40], [30, 13, 22, 40]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Life of Brian: Conjugate the verb, "to go" !
Quote
#3
My understanding was he wanted the first value in the sublist to be the lowest value in reference_list that is greater than or equal to the original value. So his while condition should be while i[0] not in reference_list:.

But I agree the problem is not clearly defined.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to count and order numbers in a list rachyioli 2 56 6 hours ago
Last Post: perfringo
  weird result trying to remove numbers from a list Exsul 2 68 11 hours ago
Last Post: perfringo
  How to remove multiples in a list jasper100125 6 173 Yesterday, 06:38 AM
Last Post: buran
  changing the process command line arguments in python Skaperen 3 122 Aug-19-2019, 02:40 PM
Last Post: wavic
  How can I increment a List item with in a "for in" msteffes 4 298 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  Counting number of occurrences of a single digit in a list python_newbie09 12 301 Aug-12-2019, 01:31 PM
Last Post: perfringo
  Automating to run python script 100 times by changing parameters pmt 0 104 Aug-10-2019, 02:58 PM
Last Post: pmt
  calculation with list in list gianniskampanakis 13 395 Aug-09-2019, 12:01 PM
Last Post: gianniskampanakis
  Select a part of an element of a list with Index BollerwagenIng 0 135 Aug-09-2019, 09:27 AM
Last Post: BollerwagenIng
  removing quotes from a list and keep type list evilcode1 3 135 Aug-03-2019, 11:07 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)