logo
down
shadow

Python go back through list of lists to see if value exists


Python go back through list of lists to see if value exists

By : Timo Tokki
Date : October 18 2020, 06:10 PM
like below fixes the issue Lets say I have a 2D list: , I'd suggest you to use pandas for such a problem:
code :
import pandas as pd
df = pd.DataFrame(lst, columns=['0','1', 'start', 'end', 'hholiday'])

    0  1  start    end  hholiday
0   1  a  False  False     False
1   3  b  False  False     False
2   7  c  False   True     False
3   0  c  False  False      True
4  13  d   True  False     False
5  21  e  False  False     False
6  31  f  False  False     False
import numpy as np
if_true = np.where(df.end.shift().fillna(False), 
                   df['0'] - df['0'].shift(), 
                   'No end defined')
df['diff'] = np.where(df.start, if_true, df['0'] - df['0'].shift())
print(df)

    0  1  start    end  hholiday            diff
0   1  a  False  False     False             nan
1   3  b  False  False     False             2.0
2   7  c  False   True     False             4.0
3   0  c  False  False      True            -7.0
4  13  d   True  False     False  No end defined
5  21  e  False  False     False             8.0
6  31  f  False  False     False            10.0


Share : facebook icon twitter icon
In python, is there a way to convert of list of integer-lists into a list of string-lists without creating a custom func

In python, is there a way to convert of list of integer-lists into a list of string-lists without creating a custom func


By : Bonjovi14
Date : March 29 2020, 07:55 AM
seems to work fine Let's say that I have a list of integer-lists: , To make it as pythonic as possible, I would write:
code :
result = [[str(subitem) for subitem in sublist] for sublist in start]
Python: read a file with lists converted to strings and convert back to lists

Python: read a file with lists converted to strings and convert back to lists


By : GHIOTSON
Date : March 29 2020, 07:55 AM
I wish this help you I would use csv module to properly tokenize the items, then I'd transpose the rows using zip and convert the string data to python lists/values using ast.literal_eval
code :
import csv
import ast


with open("file.txt") as f:
   cr = csv.reader(f)
   items = [[ast.literal_eval(x) for x in row] for row in zip(*cr)]

print(items)
[[0.41, 0.23, 0.19], [[1.0, 0.0, 1.0], [0.0, 2.0, 2.0], [0.0, 0.0, 3.0]], [[16.4, 0.0, 0.0], [32.8, 0.0, 0.0], [92.8, 0.0, 0.0]]]
if len(items)==3:
    a,b,c = items
    print(a)
    print(b)
    print(c)
[0.41, 0.23, 0.19]
[[1.0, 0.0, 1.0], [0.0, 2.0, 2.0], [0.0, 0.0, 3.0]]
[[16.4, 0.0, 0.0], [32.8, 0.0, 0.0], [92.8, 0.0, 0.0]]
Python algorithm to search nested lists to check if a list already exists

Python algorithm to search nested lists to check if a list already exists


By : jakino
Date : March 29 2020, 07:55 AM
To fix this issue I am having some trouble trying to figure out a simple algorithm in Python to check if a new list already exists in a list before I add the new list if that makes sense? For example: , To check for existence, just use in:
code :
List1 = [['P1', 3, 'P2', 2], ['P3', 3, 'P4', 2], ['P5', 1, 'P6', 3]]
List2 = ['P1', 3, 'P2', 2]
if List2 in List1:
   do_something()
List1 = [['P1', 3, 'P2', 2], ['P3', 3, 'P4', 2], ['P5', 1, 'P6', 3]]
new_list = [i for i in List1 if i[1] >= 3]
Python - Sorting a list item by alphabet in a list of lists, and have other lists follow the swapping order

Python - Sorting a list item by alphabet in a list of lists, and have other lists follow the swapping order


By : R Randhawa
Date : March 29 2020, 07:55 AM
I hope this helps . You can transpose the list using zip, sort the transpose, and then transpose that list back into one of the correct dimensions.
code :
listOfLists = [ ['m', 'e', 'l', 't', 's'],
                ['g', 'p', 's', 'k', 't'],
                ['y', 'q', 'd', 'h', 's'] ]

print(list(zip(*sorted(zip(*listOfLists)))))
# [('e', 'l', 'm', 's', 't'), ('p', 's', 'g', 't', 'k'), ('q', 'd', 'y', 's', 'h')]
from operator import itemgetter
list(map(list, zip(*sorted(zip(*listOfLists), key=itemgetter(0)))))
Python: Lists within lists - function that adds string to item in parent list without affecting child list?

Python: Lists within lists - function that adds string to item in parent list without affecting child list?


By : Ryan Larkyn
Date : March 29 2020, 07:55 AM
I hope this helps you . I have generated a list of lists using: , You can use this as an easier way to generate your matrix:
Related Posts Related Posts :
  • Why does my else statement run when my if statement is true?
  • Data access layer in Python
  • Sum of array in python
  • How to upgrade sqlite 3.8.2 to >= 3.8.3
  • Python child class with more methods
  • Remove duplicates from list of strings
  • How to remove lowercase sentence fragments from text?
  • How can I use Python with Mechanize for posting multipart/form-data?
  • How to print a range with decimal points in Python?
  • Where to find a list of all the possible HTML tags in Python?
  • Set serial port pin high using python
  • How to recovery source python code (.py) from .pyo file?
  • Python Script to backup a directory
  • return to first function() at the end of last function()
  • Why am I getting output for -2//4 as -1?
  • I want output None instead of 0
  • Issues running python scripts in Command Prompt (Specifically with command line arguments)?
  • How would I start integrating pyflakes with Hudson
  • Name some non-trivial sites written using IronPython & Silverlight
  • How do I do advanced Python hash autovivification?
  • What is the paste deploy uri syntax?
  • Removing duplicates (within a given tolerance) from a Numpy array of vectors
  • How do constructors and destructors work?
  • QTableWidget signal cellChanged(): distinguish between user input and change by routines
  • Reverse Search Best Practices?
  • Any downsides to UPX-ing my 32-bit Python 2.6.4 development environment EXE/PYD/DLL files?
  • Unit Test Won't Run Tests
  • Use two for loops simultaneously
  • can we display glass bar chart in python with google app engine
  • Scapy install issues. Nothing seems to actually be installed?
  • Why do people write the #!/usr/bin/env python shebang on the first line of a Python script?
  • What does s() mean?
  • ROC AUC value is 0
  • Why is this the value?
  • Best practices for logging in django project
  • Is there a python openid apps-discovery library to get appengine apps onto the apps marketplace
  • Cannot fetch a web site with python urllib.urlopen() or any web browser other than Shiretoko
  • Similar to ``tabnanny``, how can I check that all the python code is using 4 spaces as an indent?
  • Python: object identity question?
  • Multiple For loops, print else only once if condition is not met
  • Select one item from Series and keep the index
  • __repr__ method appears can't be invoked automatically for Exception class
  • Problem with list value (ValueError) in python 3
  • How to get TouchSensor nested under joint in Webots (Python API)
  • How to specify kernel while executing a Jupyter notebook using Papermill's Python client?
  • How to hide password in Database Connection?
  • How to get a list of dictionaries from the following code?
  • 'How to find out noun to which pronoun is referring to' in python
  • Removing a character (^) from each row of panda Dataframe and get unique words in each row
  • Changing a static variable of inherited classes
  • Django Query result comparison with if statement
  • Python: how to merge two dataframe based only on different columns?
  • Filter data by last 3 months and by ID
  • Inplace arithmetic operation versus normal arithmetic operation in PyTorch Tensors
  • How can I add custom signs to spaCy's punctuation functionality?
  • Ensure positive difference of two numbers
  • i keep getting an error that my list index is out of range
  • Is there a way to create gantt charts in python?
  • How to view network weights and bias during training
  • How can I force SAS to wait for a command to fully execute?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk