logo
down
shadow

Changing a certain index of boolean list of lists change others, too


Changing a certain index of boolean list of lists change others, too

Content Index :

Changing a certain index of boolean list of lists change others, too
Tag : python , By : Scott Everts
Date : November 27 2020, 03:01 PM

wish helps you so I have a boolean list of lists and I changed a certain index from True to False, and it affects some others element in list of lists too. Why is it happening? is there any alternative? , What you want to do is :
test = [[True for i in range(cols)] for j in range(rows)]
#OR
test = [[True]*cols for j in range(rows)]
test = [[True]*9]*9
test = [ [True, True, True, True, True, True, True, True, True] repeated 9 times ]
test = [[True for i in range(9)] for j in range(9)]
test[0][1] = False
print(test)
[[True, False, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True], 
[True, True, True, True, True, True, True, True, True]]

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

I have two lists containing the same objects. How do I change one list without changing the other?


Tag : java , By : Tonci Grgin
Date : March 29 2020, 07:55 AM
I wish this help you The two lists are referencing the same object. So if you change the first, the second will change also.
The solution is to clone the object (Creating a copy of it into a new instance) before adding it into the second list.
class HistoricRate {
  private String field;

  public HistoricRate (HistoricRate another) {
    this.field= another.field; // you can access  
  }
}
for (HistoricRate rate : rates){
    listOfRates.add(rate);
    inverseListOfRates.add(SerializationUtils.clone(rate));
}

Compare 2 lists, based on index from 2 other lists, and save index in new list with list comprehension


Tag : python , By : user182548
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm not sure if this will provide a significant performance increase, but try this:
list5 = [y for y, val1 in enumerate(list1) for x, val2 in enumerate(list2) if val1 == val2 and list3[y] < (0.4 * list4[x])]

Splitting a list into multiple sub-lists, changing a particlular sub-list, and then rejoining all the sub-lists to form


Tag : tcl , By : Eniac
Date : March 29 2020, 07:55 AM
Hope that helps I have to write a code for doing the following steps. ,
Loading a file and saving the contents of a file into a list.
% set superList [list [list 201 1 300] [list 202 1 300] [list 203 1 300]]
{201 1 300} {202 1 300} {203 1 300}
% lset superList 2 1 0.5
{201 1 300} {202 1 300} {203 0.5 300}
% concat {*}$superList
201 1 300 202 1 300 203 0.5 300

placing a list with indices (changing length) after a hierarchy of lists to change the values


Tag : python , By : cameron
Date : March 29 2020, 07:55 AM
This might help you This is probably a bad design .... but ... you could iteratively descend into your sublists like so:
boundaries = [[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
              [[2, 1, 6, 5]], [[1, 0, 7, 6]]] 

index1 = [1, 0, 2]      # is the same as [1][0][2]  
index2 = [1, 1, 0, 1]   # is the same as [1][1][0][1]


def iterateively_descent(idx, l, val):
    # iteratively descent until the last sublist
    for k in idx[:-1]:
        l = l[k] 
    # set the value of the last sublist
    l[idx[-1]] = val

print(boundaries)

iterateively_descent(index1,boundaries,45)
print(boundaries)

iterateively_descent(index2,boundaries,145)
print(boundaries)
[[0, 1, 2, 3, 4], [[5, 6, 7, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
 [[2, 1, 6, 5]], [[1, 0, 7, 6]]]

[[0, 1, 2, 3, 4], [[5, 6, 45, 8, 9], [[0, 4, 8, 7], [4, 3, 9, 8]]],
 [[2, 1, 6, 5]], [[1, 0, 7, 6]]]

[[0, 1, 2, 3, 4], [[5, 6, 45, 8, 9], [[0, 145, 8, 7], [4, 3, 9, 8]]], 
 [[2, 1, 6, 5]], [[1, 0, 7, 6]]]

How to change overflow:hidden to visible in lists without changing the height of the list


Tag : jquery , By : Simon Capewell
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • trigger python events driven by selenium controlled browser
  • Passing line-edits to a contextmanager to set validators
  • Python: globals().items() iterations try to change a dict
  • Is it possible to specify starting values for each parameter (instead of bounds) for scipy's differential evolution?
  • why datetime.now() and constructed datetime using all fields(like year,month...) of now has big timedelta?
  • MySQL multiple table UPDATE query using sqlalchemy core?
  • find if a semantic version is superset of of another version python
  • Type checking against dynamically created objects
  • Struggling with simple reverse function
  • Is there a function for finding the midpoint of n points on sklearn.neighbors.NearestNeighbors?
  • How to set max number of tweets to fetch
  • PYTHON 3.7.4 NOT USING SQLITE 3.29.0
  • How to replace Nan value with zeros in a numpy array?
  • How to speed up calculating variance among sparse matrix
  • cupy code is not fast enough compared with numpy
  • How to count frequency of select values in Python pandas dataframe
  • Scrape Span Text from Google
  • Python watchdog, watch a directory and rename file on event.modification
  • Filtering rows in DataFrame with dependent conditions
  • How to check if a character is a not a part of number or URL in string?
  • Compare corresponding elements of a list
  • Python misinterprets 3 character string as UTF-8 continuation byte
  • Merge two columns in Pandas
  • Side Effect error in Python in an online compiler
  • How to convert a navigation list with depth levels to a parent-child flat list?
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com