5

How to return the list index to Python

 3 years ago
source link: https://www.codesd.com/item/how-to-return-the-list-index-to-python.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to return the list index to Python

advertisements

This question already has an answer here:

  • Accessing the index in Python 'for' loops 12 answers

I have a list U of lists such that U=[a[1], a[2], a[3]] and where a = [[0,1,2], [0,0,0],[1,1,1]]. I am trying to change U in a way that it will only contain lists without 2. I know that if I want U = [a[2], a[3]] I have to do it like this:

for j in range (0,2):
    U= [f for f in U if f[j]!=2]

But now I want it to return only the index of the lists, I mean at the end U should be [2,3]. Is it possible to do it ?

Thanks for the help !


Use enumerate:

>>> a = [[0,1,2],[0,0,0],[1,1,1]]
>>> [i for i, x in enumerate(a, 1) if 2 not in x]
[2, 3]

Related Articles

How to return the list that corresponds to a value in a list of lists

Let's say I have this list here: lista = [["a", "b", "c"], ["a", 2, 3], ["a", 4, 5], ["f", "g", "h"]] If I were to find "a" in lista, would it be possible to return

How to return the single index value of several selected items in the list

I have a listbox in VB.NET that allows users to select multiple categories. I need to put these categories into a database and need the index value from the listbox of the categories as the database works off IDs. SelectedItems (with an s) does not w

How to return the list of integers as JSON objects in ASP.net MVC 5

I am learning ASP.Net MVC5. And I have a case where I need to return a JSON result. Currently, I have list of integer. But I want to return it as json objects. Below is what I have [HttpGet] public ActionResult hello() { var x = SMS_DAL._DalColl.lstO

NodeJS asynchronous callback. How to return the list, from an asynchronous callback?

So basically I am making a database query, to get all posts with a certain id, then add them to a list, so I can return. But the list is returned, before the callback has finished. How do I prevent it from being returned before callback has finished?

How to return the child index in Layout?

I have the following situation - HorizontalScrollView (HSV), inside HSV - LinearLayout and inside it number of buttons - the code is here: <HorizontalScrollView android:id="@+id/Footer" android:orientation="horizontal" android:layou

How to return the position (Index) of an element after the formatting from 1D to 2D numpy array?

I have a 1d numpy array like p=array([ 1,2,3,4,5,6,7,8,9,10,11,12]) In my work, I need to change it to 2d array like p_reshape = array([[1,2,3], [4,5,6], [6,7,8], [9,10,11]]) I have other function which returns the position of the element i in p matr

Place access to the list index in Python

I'd like to be able to access some values of a python object using array-like syntax, ie: obj = MyClass() zeroth = obj[0] first = obj[1] Is this possible? If so, how do you implement this in the python class in question?You need to write or override

How to return the list of names using the Excel formula?

Data is: -|-A--|-B-| 1|BAT | 5 | 2|CAT | 2 | 3|RAT | 4 | I want formula that gives name list of animal if value in columns B > 3. From example Result is BAT,RAT.Here is one of the approach using you can get what you looking for. BUT you need to use V

How to sort the list data into python with several parameters while giving a weight to the parameters?

ListData= sorted(ListData, key=lambda x: ((x['AvailableOffers'] ['ValidTill'], x['AvailableOffers']['Amount'])), reverse=True)[:10] Giving a weight to the params, I mean for example the statement above has two conditions for sorting, VALID TILL AND A

How to return a list at the end of a recursive function in Python

I was playing with the fibonacci sequence in Python (I know the implementation with yield, but wanted to implement it with recursion) and ended up with the following code snippet: def fib(start, leng): """ Recursive Fibbo"""

How to get the last index of a list?

Suppose I've the following list: list1 = [1, 2, 33, 51] ^ | indices 0 1 2 3 How do I obtain the last index, which in this case would be 3, of that list?len(list1)-1 is definitely the way to go, but if you absolutely need a list that has a function th

How to return the table list in the lua program?

I want to return the list of records from retrieving data from database in lua program. local function getListFromDatabase() local select_stmt = db:prepare("SELECT * FROM list") for row in select_stmt:nrows() do print(row.id, row.name, row.icon)

How to convert the list to an array in a function - python?

I defined a function(procedure) to read a file. I want it returns arrays with data I want to read from the file, as it follows: import csv import numpy as np import matplotlib.pyplot as plt # Subroutine to read the day, Ta,Tp from a file and convert

How to write the query linq (for different tables) which returns the list

What I need The logged in user needs a list of all bugs that are linked to the projects the user is linked to/enrolled in. What I try I work in three layer (presentation - BLL - DALL) from the presentation layer I can send the pk_user_id. I try to wr

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK