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

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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
  Randomly assign values in List 1 to a value in List 2 Seneca260 1 76 3 hours ago
Last Post: buran
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 86 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  item = index position - list of list RavCOder 9 143 Dec-02-2019, 05:24 PM
Last Post: ThomasL
  List Comprehension - Creating a list of the length of an item help paul41 2 163 Nov-18-2019, 10:21 AM
Last Post: perfringo
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 171 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 205 Oct-17-2019, 03:04 PM
Last Post: baquerik
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 143 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Creating a list from a comprehension using a list rm197 6 184 Sep-30-2019, 07:02 PM
Last Post: rm197
  Converting List into list of tuples ARV 4 365 Sep-28-2019, 04:58 AM
Last Post: perfringo
  addition for elements in lists of list ridgerunnersjw 3 215 Sep-15-2019, 07:11 AM
Last Post: perfringo

Forum Jump:


Users browsing this thread: 1 Guest(s)