7,156 questions
-6
votes
3
answers
135
views
Having trouble with List Comprehensions in Python
I'm taking a Coursera course on Python programming. There is a practice quiz on list comprehensions and tuples I'm failing by getting 2 questions wrong. But I don't think I got either question wrong.
...
3
votes
1
answer
145
views
How to return a list of lists as single lists?
I keep running into this weird roadblock when trying to use list comprehensions when trying to call on helper functions, and I'm unsure if I'm using the wrong tool for the job or I'm just missing a ...
-2
votes
1
answer
72
views
Tuple comprehension creates generator; List comprehension evaluates all elements right away [duplicate]
I am using a long ad hoc script for exploratory data analysis -- not for tool development. The script has gotten quite long, so I've taken to deling ephemeral variables to keep the Spyder Variable ...
1
vote
1
answer
76
views
TypeError: sequence item 0: expected str instance, int found. what should I do to show this list as a matrix form [duplicate]
matrix1=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
m2="\n".join(["\t".join([ritem for ritem in item]) for item in matrix1])
print(m2)
where am i wrong that i receive this ...
0
votes
4
answers
50
views
Eliminating list elements having string from another list
I'm trying to a get a subset of elements of L1, which do not match with any element of another list
>>> L1 = ["apple", "banana", "cherry", "date"]
>&...
-3
votes
3
answers
96
views
map from a list comprehension?
I came across this scraping problem which itself is irrelevant but want to know if it is possible to perform map() from a list comprehension meaning the iterable be a list comprehension?
The following ...
0
votes
2
answers
107
views
Python list comprehension with two loops and two conditions
I have the following data, and what I'm want to do is bring into a new list of dicts those from task_resources that match on both the tasks_id and the qos_level:
task_resources = [
{
"...
1
vote
2
answers
242
views
A more pythonic way to filter out even numbers from a list and calculate the sum?
I have a list of numbers in Python, and I’m trying to get the sum of only the odd numbers. I know I can use a for loop, but is there a more Pythonic way to do this?
Here’s what I have so far, but it ...
0
votes
2
answers
455
views
is there a quicker way to save csv files as polars dataframes than using list comprehension?
I have 2 folders each containing 1507 csv files, i am using the following code to save each file as a polars dataframe using list comprehension:
bdsim=[pl.read_csv(x, schema_overrides = {"X0.6&...
-3
votes
2
answers
74
views
list comprehension if keyword in string
I am cleaning columns in a dataframe, and would like to split a "description" column based on certain keywords. I wrote a one liner but for some reason in still returns np.nan, even when the ...
0
votes
3
answers
117
views
How do I join filter strings into a url string?
I am building a URL query, that allows the filtering of multiple fields. The filtering (e.g. from a form) is stored as pair values: column and FILTER. I am storing the filters as a list of pairs.
I ...
0
votes
2
answers
76
views
List comprehension only outputs 1 line instead of multiple lines in sequence of triangular numbers
tri_num = []
for i in range(1, 10):
tri_num.append(i*(i+1)//2)
print(tri_num)
Hi, I'm taking a course in Python and we've been asked to write a list comprehension that creates ...
-4
votes
3
answers
398
views
Python comprehension with different number of elements
The general problem
I have a case where I'm generating an element comprehension with a different cardinality to the input source. This cardinality should not be a multiple of the original (data-driven)...
-1
votes
3
answers
162
views
update dictionaries in the list comprehension
There is a dictionary:
d = [{"a":1, "b":2},{"a":3, "b":4},{"a":5, "b":6}]
I'd like to update values of keys b.
d = [{**m}.update({"b&...
0
votes
3
answers
81
views
Moving corner points of a rectangle in Python: is there a more elegant/compact solution?
I need to move the corner points of a rectangle by a given amount. I'm given an list of 4 3D points, each of which is a list of coordinates ([x,y,z]).
I have this, and it works OK, but feels awkward (...
1
vote
2
answers
108
views
Use list comprehension with if, else, and for-loop while only keeping list items that meet a condition
I use list comprehension to load only the images from a folder that meet a certain condition.
In the same operation, I would also like to keep track of those that do not meet the condition. This is ...
0
votes
2
answers
109
views
Why are all rows of my matrix the same as the last row?
Can someone please help me find fault in my code? If I create a blank 2D matrix with static initial value, it returns the correct transposed matrix. Whereas if I create a blank matrix using a for loop,...
3
votes
3
answers
175
views
How to get the name of column with second highest value in pyspark dataframe
I have a PySpark dataframe looking like this:
id
trx_holiday
trx_takeout
trx_pet
max_value
MAX
1
12.5
5.5
9.5
12.5
trx_holiday
2
3.0
14.0
6.7
14.0
trx_takeout
I want to create a second column MAX_2 ...
-1
votes
3
answers
71
views
Trying to get dict[k] from a dict in a list of dicts for k in list or string
I have a couple dictionaries with differing values for the same keys.
I have a list of these dicts and I want to return the values from only 1 of the dicts based on the matched keys inside a different ...
-1
votes
1
answer
103
views
Using list comprehension while creating a pandas pipeline throws function object not iterable error
I have to create a Pandas dataframe from a csv file using a pipeline.
The src csv file may contain any number of columns with header/name containing the string 'SLA'. Sample data below:
While ...
1
vote
1
answer
383
views
How to create vector elements in pairs in a FOR loop in OpenSCAD
My aim is to create 2D vector elements in pairs, using a FOR loop. Consider the following example (not my real example - just something simplified to describe the problem):
step1 = [ for (i=[1:5])...
1
vote
3
answers
72
views
replace() and if/else in list comprehension to create a new list
The quiz problem is supposed to replace the file name .hpp with .h while keeping the rest using list comprehension. Some posts suggest that I must put in [do_something else if condition for x in range]...
-1
votes
1
answer
37
views
search for elements of a list as substring in another list python
I have 2 lists. I want to find the elements in ls2 where any element of ls1 is a substring. I would like to return a list of ls2 elements along with the substring that was searched and found from ls1
...
0
votes
1
answer
120
views
Create deck of multiple decks of cards using list comprehension [duplicate]
I've created a Card object:
class Card:
def __init__(self, rank, suit, d=0):
self.rank = rank
self.suit = suit
self.deck_index = d
I can make a list of Cards that ...
-1
votes
1
answer
122
views
How to efficiently filter a large list in Python using multiple conditions?
I'm working on a Python project where I need to filter a large list of dictionaries based on multiple conditions.
Here's a simplified example of the data structure I'm dealing with:
data = [
{&...