All Questions
2,834
questions
177
votes
4
answers
420k
views
Apply function to each element of a list [duplicate]
Suppose I have a list like:
mylis = ['this is test', 'another test']
How do I apply a function to each element in the list? For example, how do I apply str.upper to get:
['THIS IS TEST', 'ANOTHER ...
142
votes
5
answers
263k
views
Iterating Over Dictionary Key Values Corresponding to List in Python
Working in Python 2.7. I have a dictionary with team names as the keys and the amount of runs scored and allowed for each team as the value list:
NL_East = {'Phillies': [645, 469], 'Braves': [599, ...
128
votes
9
answers
378k
views
Check if all values in list are greater than a certain number
my_list1 = [30,34,56]
my_list2 = [29,500,43]
How to I check if all values in list are >= 30? my_list1 should work and my_list2 should not.
The only thing I could think of doing was:
boolean = 0
def ...
127
votes
8
answers
99k
views
Return the index of the first element of a list which makes a passed function true
The list.index(x) function returns the index in the list of the first item whose value is x.
Is there a function, list_func_index(), similar to the index() function that has a function, f(), as a ...
58
votes
5
answers
99k
views
How can I combine multiple rows into a comma-delimited list in SQL Server 2005?
Right now, I have a SQL Query like this one:
SELECT X, Y FROM POINTS
It returns results like so:
X Y
----------
12 3
15 2
18 12
20 29
I'd like to return results all in one row, like ...
53
votes
8
answers
382k
views
Python return list from function
I have a function that parses a file into a list. I'm trying to return that list so I can use it in other functions.
def splitNet():
network = []
for line in open("/home/tom/Dropbox/CN/...
52
votes
6
answers
46k
views
Handle either a list or single integer as an argument
A function should select rows in a table based on the row name (column 2 in this case). It should be able to take either a single name or a list of names as arguments and handle them correctly.
This ...
41
votes
4
answers
76k
views
call list of function using list comprehension
can I call a list of functions and use list comprehension?
def func1():
return 1
def func2():
return 2
def func3():
return 3
fl = [func1, func2, func3]
fl[0]()
fl[1]()
fl[2]()
I know ...
38
votes
4
answers
50k
views
Grouping elements of a list into sublists (maybe by using guava)
I want to group elements of a list. I'm currently doing it this way:
public static <E> List<List<E>> group(final List<E> list, final GroupFunction<E> groupFunction) {
...
37
votes
2
answers
59k
views
Sort python list by function
I have a function that takes an object as an argument and gives me a number. I wish to use this number as the key to sort my list.
If I were to iterate over the list I would do something like:
...
27
votes
3
answers
90k
views
Make all values in range show up on x axis
Could someone please guide me on how should I make sure that all ticks (or maybe a better way to specify will be to say all elements in the list passed to plot function) are displayed on the x axis ...
26
votes
9
answers
97k
views
Get max value from a list with lists?
So I have a list that contains several list which all have three strings first, then one float number, like:
resultlist = [["1", "1", "a", 8.3931], ["1", "2", "b", 6.3231], ["2", "1", "c", 9.1931]]
...
25
votes
4
answers
89k
views
Python passing list as argument [duplicate]
If i were to run this code:
def function(y):
y.append('yes')
return y
example = list()
function(example)
print(example)
Why would it return ['yes'] even though i am not directly changing ...
23
votes
2
answers
27k
views
Python: converting list of lists to tuples of tuples
A Python newbie! I need help converting a list of lists tuples of tuples.
I want to call the append_as_tuples function, but every time I return it, it says
it can only concatenate lists (not tuples) ...
20
votes
2
answers
24k
views
Visual Studio Code - Classes and method lists?
is there any function list or function outline available in "Visual Studio Code"?
I need it for JavaScript and PHP.
Thanks for any help!
20
votes
14
answers
23k
views
How do I reverse a sublist in a list in place?
I'm supposed to create a function, which input is a list and two numbers, the function reverses the sublist which its place is indicated by the two numbers.
for example this is what it's supposed to ...
18
votes
7
answers
22k
views
Overloading Java function with List<> parameter
I have 2 classes
public class Customer{
...
public String getCustomerNumber();
...
}
public class Applicant{
....
private Customer c;
public Customer getCustomer(){ return c; }
...
...
18
votes
4
answers
19k
views
Python function to merge unique values form multiple lists to one list
I am pretty new to Python. I am trying to write a function that will merge unique values in separate lists into one list. I keep getting a result of a tuple of lists. Ultimately I would like to have ...
18
votes
3
answers
39k
views
Store functions in list and call them later [duplicate]
I want to store functions in a list and then later in a program call those functions from that list.
This works for me, however, I don't know if it is correct :
#example functions, my functions ...
17
votes
14
answers
100k
views
Using Python, reverse an integer, and tell if palindrome
Using Python, reverse an integer and determine if it is a palindrome. Here is my definition of reverse and palindrome. Do I have a correct logic?
def reverse(num):
s=len(num)
newnum=[None]*...
17
votes
10
answers
4k
views
Python function that identifies if the numbers in a list or array are closer to 0 or 1
I have a numpy array of numbers. Below is an example:
[[-2.10044520e-04 1.72314372e-04 1.77235336e-04 -1.06613465e-04
6.76617611e-07 2.71623057e-03 -3.32789944e-05 1.44899758e-05
5.79249863e-05 ...
17
votes
4
answers
10k
views
Python List Class __contains__ Method Functionality
Does the __contains__ method of a list class check whether an object itself is an element of a list, or does it check whether the list contains an element equivalent to the given parameter?
Could you ...
16
votes
12
answers
19k
views
Function list of php file
How to get list of functions that are declared in a php file
16
votes
2
answers
366
views
Nested function application
It's quite easy to define an operator like
(@) :: [x -> y] -> [x] -> [y]
which takes a list of functions and a list of inputs and returns a list of outputs. There are two obvious ways to ...
14
votes
3
answers
63k
views
how to update global variable in python
In python, i have a function that returns a list of the latest links(to folders) on a website. I also have another function that downloads the latest files from those folders. I plan to run this ...
13
votes
7
answers
94k
views
How to pass a list as an input of a function in Python
I am using Python, and I have a function which takes a list as the argument. For example, I am using the following syntax,
def square(x,result= []):
for y in x:
result.append=math.pow(y,2....
12
votes
2
answers
127k
views
In C#, How do I call a function that is returning a list?
In C#, How do I call a function that is returning a list?
static void Main(string[] args)
{
List<string> range = new List<string>();
range.ForEach(item =&...
12
votes
1
answer
10k
views
Apply a function to all pairwise combinations of list elements in R
I want to apply a function to all pairwise combinations of list elements.
Each element is a vector with the same length. I would like the output in a n x n matrix format, n being the number of ...
11
votes
4
answers
43k
views
Finding 3d distances using an inbuilt function in python
I have 6 lists storing x,y,z coordinates of two sets of positions (3 lists each). I want to calculate the distance between each point in both sets. I have written my own distance function but it is ...
10
votes
3
answers
23k
views
haskell - turning list of lists into one list
If I have a list of lists, say [[1,2,3],[1,2,3],[1,2,3]], is there any way in Haskell to turn this into just 1 list, like [1,2,3,1,2,3,1,2,3]?
Thanks in advance!
10
votes
4
answers
11k
views
Haskell: Map function onto a list of lists
How do you map a function to operate on lists within a list?
The following is just what I'm trying to do as an example, but I was just asking as a general question. Thanks in advance!
Right now, I'm ...
10
votes
4
answers
11k
views
How can I write Erlang's list concatenate without using the lists module?
The book I'm reading about Erlang has exercises in the back of it and one is to re-create the lists:append function.
I could do this simply using the ++ operator, but isn't this really slow? And I ...
10
votes
3
answers
24k
views
Given a list of values remove first occurrence
def drop dest(routes,location):
for i in range(len(routes)):
if routes[i] == location:
routes.remove(routes[i])
return routes
I am using a function definition given a ...
9
votes
7
answers
46k
views
Trying to count words in a string
I'm trying to analyze the contents of a string. If it has a punctuation mixed in the word I want to replace them with spaces.
For example, If Johnny.Appleseed!is:a*good&farmer is entered as an ...
9
votes
5
answers
22k
views
How to apply a function to each sublist of a list in python?
Lets say I have a list like this:
list_of_lists = [['how to apply'],['a function'],['to each list?']]
And I have a function let's say I want to apply the F function to each sublist of the F function ...
9
votes
2
answers
636
views
Unsequence Monad function within Haskell
I'm having some real trouble designing the counterfunction of Haskell's sequence function, which Hoogle tells me doesn't yet exist. This is how it behaves:
ghci> sequence [Just 7, Just 8, Just 9]
...
8
votes
7
answers
97k
views
Why can't I use `sum` for a list created by splitting the user input? [duplicate]
I am doing my homework and it requirers me to use a sum () and len () functions to find the mean of an input number list, when I tried to use sum () to get the sum of the list, I got an error ...
8
votes
2
answers
5k
views
Intersection of variable number of lists
I define intersection of two lists as follows:
def intersect(a, b):
return list(set(a) & set(b))
For three arguments it would look like:
def intersect(a, b, c):
return (list(set(a) & ...
8
votes
2
answers
2k
views
Is `list()` considered a function?
list is obviously a built-in type in Python. I saw a comment under this question which calls list() a built-in function. And when we check the documentation, it is, indeed, included in Built-in ...
8
votes
2
answers
3k
views
How does scanr work? Haskell
I have been messing with some Haskell functions, some I have understand and some don't.
For example if we do: scanl (+) 0 [1..3] my understanding is the following:
1. the accumulator is 0 ...
7
votes
7
answers
19k
views
Function changes list values and not variable values in Python [duplicate]
Let's take a simple code:
y = [1,2,3]
def plusOne(y):
for x in range(len(y)):
y[x] += 1
return y
print plusOne(y), y
a = 2
def plusOne2(a):
a += 1
return a
print plusOne2(...
7
votes
3
answers
18k
views
Is there a more pythonic way of exploding a list over a function's arguments?
def foo(a, b, c):
print a+b+c
i = [1,2,3]
Is there a way to call foo(i) without explicit indexing on i?
Trying to avoid foo(i[0], i[1], i[2])
7
votes
5
answers
2k
views
Haskell function from (a -> [b]) -> [a -> b]
I have a function seperateFuncs such that
seperateFuncs :: [a -> b] -> (a -> [b])
seperateFuncs xs = \x -> map ($ x) xs
I was wondering whether the converse existed, i.e. is there a ...
7
votes
5
answers
1k
views
Python standard library function for rearranging a list
I am wondering if there is a standard library function in Python which will rearrange the elements of a list like below:
a = [1,2,3,4,5,6,7]
function(a)
print a
a = [1,7,2,6,3,5,4]
It should get ...
7
votes
5
answers
14k
views
How to add a function call to a list?
I have a Python code that uses the following functions:
def func1(arguments a, b, c):
def func2(arguments d, e, f):
def func3(arguments g, h, i):
Each of the above functions configures a CLI ...
7
votes
2
answers
355
views
Convert a list of things into a list of sublists
I want to write a function that converts a list of things into a list of sublists, each containing elements of the same value, which when concatenated together give the same list.
So runs [1,2,2,1,3,3,...
7
votes
2
answers
148
views
Definining a function from a list
Say I have a list of strings: obj = ['One','Two','Three'], how would I be able to turn each value in this list into a function where they all carry out very similar functions? For example:
def one():
...
7
votes
2
answers
243
views
Extract operator `$( )` and non-syntactic names
Say I have the following list (note the usage of non-syntactic names)
list <- list(A = c(1,2,3),
`2` = c(7,8,9))
So the following two way of parsing the list works:
`$`(list,A)
## [1] ...
7
votes
3
answers
598
views
How to specify index of specific elements in every sublist of nested list?
i have several nested lists, which are all permutations of each other within sublists:
x = [
['a', [['b', 'c', [['e', 'd']]]]],
['a', [['b', [['e', 'd']], 'c']]],
[[['b', 'c', [['e', 'd']]]], 'a'],
['...
7
votes
2
answers
1k
views
How is generic list manipulation function written?
I am a beginner in programming, please go easy on me and I am finding difficult to get the answer for my question. I can't get my head around the complex codes. Can some one please explain me with ...