FAQ Database Discussion Community


PYTHON: Create sublists of same values from nested list into one list

python,list,sublist
I have a nested list with lists that have two values, a word and a number (sorted by first value - the word): data=[["apple",2],["cake",5],["cake",8],["chocolate",3],["chocolate",9],["chocolate",10],["grapes",6]] How can I make it so that it groups items with the same word together as efficiently as possible? So to make the list look like...

count sublists that has a specific term in python

python,list,sublist
I'm a newbie I want to write a function that outputs the count of sublists that contain a particular element. But my function just outputs the total count of that particular term in all the sublists. My function: def count(myList): tmp = [] d = {} for item in myList:...

Sublists of a list in java

java,list,sorting,arraylist,sublist
There are N buildings in a certain one-dimensional landscape. Each building has a height given by hi,i∈[1,N]. If you join K adjacent buildings, they will form a solid rectangle of area K×min(hi,hi+1,…,hi+k−1). Given N buildings, find the greatest such solid area formed by consecutive buildings. Input Format The first line...

How to check if a list is a non-empty sublist of another list in Prolog

prolog,sublist
I am trying to create an included_list(X,Y) term that checks if X is a non-empty sublist of Y. I already use this for checking if the elements exist on the Y list check_x(X,[X|Tail]). check_x(X,[Head|Tail]):- check_x(X,Tail). And the append term append([], L, L). append([X | L1], L2, [X | L3]) :-...

Prolog Length of Evens in a List [duplicate]

list,recursion,prolog,sublist,variable-length
This question already has an answer here: How to count all even numbers in a list 2 answers I am a Prolog beginner, and I have the following length function: mylen([H|Lc],N) :- mylen(Lc,M),N is M+1. mylen([],0). I would like to know if there's an easy way to query the...

Extract the data from the sublist into dataframe

r,list,extract,dataframes,sublist
the structure of the list as follow (the list goes on with the same structure): > str(parsedData) > List of 1658 > $ :List of 2 > ..$ Date : chr "2010-08-16" > ..$ Volatility: num 11.1 > $ :List of 2 > ..$ Date : chr "2010-08-17" > ..$...

Java - Create sublist from a list based on suffix

java,arraylist,collections,iterator,sublist
I'm trying to create sublists from a list based on the suffix. public class Test { public static void main(String args[]) { List<List<String>> subList = new ArrayList<List<String>>(); List<String> myList = new ArrayList<String>(); myList.add("Text_1"); myList.add("XYZ_3"); myList.add("ABC_1"); myList.add("Text_2"); myList.add("Text_3"); myList.add("XYZ_1"); myList.add("XYZ_2"); myList.add("ABC_2"); for (String item : myList) { List<String> tempList = new...

Sort list from a specific index value in python

python,list,sorting,indexing,sublist
I want to sort a python list from a specific index. Here is what I want to achieve. Consider a list al = [1,2,4,3] Now what I want to do is, start from the last index, go back and swap the number which is smaller than the last index, i.e...

Sublists in a 2D List, Referencing

java,reference,sublist
I have a 2D List of objects. I'm trying to access a list and replace it with a sublist of itself. I made up a simple example below, I want to replace dList.get(0) with dList.get(0).subList(1,3). I use a reference variable, which updates the values in the original list, but the...

Create list of sublists [closed]

python,list,sublist
I have a list of 50,000 integers: I want to create a list of sublists from this large list. The sublist is created by finding within the list the position that has a difference with the first value of the list of 1,000,000. Below is my code: sub_pos_list = []...

python- searching dictionary sublists; converting dictionary keys to values

python,search,dictionary,sublist
Say I have the following dictionary (the one I'm using is much, much larger): dict1={1:["item", "word", "thing"], 2:["word", "item"], 3:["thing", "item", "item"]} and have every word used in the dictionary stored in a list: all_words=["item", "word", "thing"] I want to run each word of the list through the dictionary sublists,...

Sublisting list of integers into lists of same integers

algorithm,list,divide,sublist
Hi I have a simple question. I am grabbing data from an API and the way that they have their data setup is that objects have an id and I return it into a list. So I will get a list of objects like so: List = {Object1, Object2, Object3,...

Python matching-item based concatenation of list of sublists?

python,list,merge,sublist
Without getting too deep, the Input below represents the following: [geometry,name,z-coord,key-region, ... ] Any key-regions matching between sublists influence the merger of the sublists where the geometry fields are combined into a single string, and the name fields are combined into a single string. All while retaining the remainder of...

Haskell Split list into Sublist using pattern recognition

list,haskell,split,sublist
I am trying to split a Array containing I and Os, if a certain pattern occurs. lets assume i have an input, looking like this: data Bit = O | I deriving (Eq, Show) let b = [I,I,O,O,O,O,O,I,I,O,O,O,I,O] that is what i am generating, when encoding [[Bool]] -> [Bit] corresponding...

how to find max element from dictionary that has list of lists in python

python,list,dictionary,sublist
I'm a newbie to python, i was thinking of writing a function that basically outputs the largest among sublists present inside a dictionary based on a threshold value. ex: champs = create_champion_index({'a': [[0, 10], [1, 20], [2,15]], 'b': [[0, 20], [1, 15], [2, 10]]}, 2) >>champs['a'] [[1, 20], [2, 15]]...

How to create a list of sublists in Lisp without map?

list,recursion,lisp,sublist
I'm trying to make a function that creates a list of all sublists in the given list. What I mean is, when I've got a list: (4 (a g b) g (1 2 3) g (4 5 6)) I want a list: ((a g b) (1 2 3) (4 5...

To check if sublist exists in another list

list,python-3.x,sublist
coll = [[3, 3], [2, 2, 2], [2, 4], [2, 3], [2, 2]] main = [4, 3, 3, 2, 2, 2, 2, 2, 2, 2] I have 2 lists. 'coll' is a list of lists with each sublist containing integers which might have duplicates(ex- [2, 2, 2]). And main is...

Android - FATAL EXCEPTION: main when adding subList

android,listview,arraylist,sublist
I'm currently working on my first Android application. It parses JSON Data and puts it in a ListView (see below). protected void onPostExecute(String result){ try { JSONArray json = new JSONArray(result); for(int i = 0; i < json.length(); i++) { listItems.add(json.getJSONObject(i).getString(("title"))); adapter.notifyDataSetChanged(); } } catch (JSONException e) { e.printStackTrace(); }...

Python, comparison sublists and making a list

python,comparison,append,sublist
I have a list that contains a lot of sublists. i.e. mylst = [[1, 343, 407, 433, 27], [1, 344, 413, 744, 302], [1, 344, 500, 600, 100], [1, 344, 752, 1114, 363], [1, 345, 755, 922, 168], [2, 345, 188, 1093, 906], [2, 346, 4, 950, 947], [2, 346,...