I don't understand this code.I want to split him up - python

I don't quite understand how this paragraph is written.
The source code is as follows.
line = [cell.value for cell in col if cell.value != None]
I want to understand how to write this code.
I tried to use loops, but the results were different.
for cell in col:
if cell.value != None:
line = cell.value

You are quite close. FYI, the one-line syntax is called a list comprehension. Here is the equivalent.
line = list()
for cell in col:
if cell.value != None:
line.append(cell.value)

You're keep overriding the line variable while it should be a list:
line = []
for cell in col:
if cell.value != None:
line.append(cell.value)
As you see, the one-liner has two square brackets around it, so it becomes a list.

You are going in right direction but here line will be an array and each value is appended in the array
so code will look like following
line = []
for cell in col:
if cell.value != None:
line.append(cell.value)

line = [cell.value for cell in col if cell.value != None]
print(line)
line = []
for cell in col:
if cell.value != None:
line.append(cell.value)
print(line)
line = list()
for cell in col:
if cell.value != None:
line.append(cell.value)
print(line)
Translate to an empty list and write the contents as you did and add them to your list by append. I put here print line from me, you can ignore it.

Related

Trying to check if tuple item is nan

I have the below for loop and am try to check first if the tuple(row) item in position 10 is Nan
i=0
for row in df.iterrows():
if row[1][10] != None:
names = row[1][10].split(',')
for name in names:
df2.loc[i,:] = row[1][:]
i=i+1
else:
i=i+1
I thought I could use if row[1][10] != None: but it doesnt seem to work, anyone know the solution?
Can use pd.isnull(row[1][10]) instead of if row[1][10] != None.
Example:
i=0
for row in df.iterrows():
if pd.isnull(row[1][10]):
df2.loc[i,:] = row[1][:]
i=i+1
else:
names = row[1][10].split(',')
for name in names:
df2.loc[i,:] = row[1][:]
df2.loc[i,'name'] = name
i=i+1
Also please do give feedback about this solution.

Creating a nested list from an imported single column file

I am wanting to open a text file where the first line gives the number of rows in the grid and the second line gives the number of columns. All values after that should be added to the nested list one row at a time. Not sure how to do this, any help is appreciated!
with open(filename, 'r') as gridFile:
grid = []
for line in gridFile:
if line == 0:
rows = int(line)
if line == 1:
col = int(line)
else:
# add the values row by row
Try the following code.
with open(filename, 'r') as gridFile:
lines = gridFile.readlines()
rows = int(lines[0])
cols = int(lines[1])
grid = [[None for _ in range(cols)] for _ in range(rows)]
cursor_row, cursor_col = 0, 0
for line in lines[2:]:
grid[cursor_row][cursor_col] = line.strip()
if cursor_col + 1 < cols:
cursor_col += 1
else:
if cursor_row + 1 < rows:
cursor_row += 1
cursor_col = 0
else:
raise IndexError
You need to use enumerate to find the line number you are reading.
Use this to process the first few lines.
with open('yourfile.txt', 'r') as gridFile:
grid = []
for i, line in enumerate(gridFile):
line = line.rstrip('\n')
if i == 0:
rows = int(line)
elif i == 1:
col = int(line)
else:
grid.append(line)
# add the values row by row
print (rows, col, grid)
Here's the input file data I had:
8
2
Good,Morning
Happy,Friday
Python,Programming
Here's the output it printed out:
8 2 ['Good,Morning', 'Happy,Friday', 'Python,Programming']
Now that you know how many rows and columns the file will have, you can expand the above program to store the right number of rows and columns.
The best part is that python can do that for you without you needing to figure it out. If the data is comma separated, you can just use split(',') to split them into columns.
For example, changing the last line to
grid.append(line.split(','))
your output will be different for grid.
Instead of giving you a list like this:
['Good,Morning', 'Happy,Friday', 'Python,Programming']
It will give you a list like this (list of lists with two values (or 2 columns) in each 'row' of the list:
[['Good', 'Morning'], ['Happy', 'Friday'], ['Python', 'Programming']]

Find index of duplicate rows in Openpyxl

I want to find the index of all duplicate rows in an excel file and add them to a list which will be handled later.
unwantedRows = []
Row = []
item = ""
for index, row in enumerate(ws1.iter_rows(max_col = 50), start = 1):
for cell in row:
if cell.value:
item += cell.value
if item in Row:
unwantedRows.append(index)
else:
Row.append(item)
However this fails to work. It only indexes rows that are completely empty. How do I fix this?
unwantedRows = []
Rows = []
for index, row in enumerate(ws1.iter_rows(max_col = 50), start = 1):
sublist = []
for cell in row:
sublist.append(cell.value)
if sublist not in Rows:
Rows.append((sublist))
else:
unwantedRows.append(index)
Without a tuple:
row_numbers_to_delete = []
rows_to_keep = []
for row in ws.rows:
working_list = []
for cell in row:
working_list.append(cell.value)
if working_list not in rows_to_keep:
rows_to_keep.append(working_list)
else:
row_numbers_to_delete.append(cell.row)
for row in row_numbers_to_delete:
ws.delete_rows(
idx=row,
amount=1
)

Error in Python: "IndexError: list index out of range"

I am trying to make a list out of data from an excel file in python, but I receive this whenever I run my code
row[1] = int(row[1])
IndexError: list index out of range
>>>
This is the code I have that sorts it (by minimum, maximum, and average)
f = open("Class 2.csv", "r")
csvread = csv.reader(f)
nlist = []
for row in csvread:
filter(lambda x: 3 > 0, row)
row[0] = int(row[0])
row[1] = int(row[1])
row[2] = int(row[2])
row[3] = int(row[3])
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
nlist.append(row[0:4])
print(nlist)
Row[0] in my excel file is a name as well, so I also get an error that tells me that int(row[0]) cannot work because I is not an integer. I don't know how to change it so that I don't get this error.
Iteration row has no second element. Check it before sending:
If all values in list are numbers, use map for str to int values:
int_values = map(int, str_list)
or
int_values = map(int, str_list[0:4])
But this will not solve your problem, because in the list of values.

python xlrd string match

I couldnt find anything in the API. Is there a way to return the row number or coordinate of a cell based on a string match? For instance: You give the script a string and it scans through the .xls file and when it finds a cell with the matching string, it returns the coordinate or row number.
for i in range(sheet.nrows):
row = sheet.row_values(i)
for j in range(len(row)):
if row[j] == search_value:
return i,j
return None
something like that... just a basic search
You could try the following function, thank you Joran
def look4_xlrd (search_value, sheet) :
lines = []
columns = []
for i in range (sheet.nrows) :
row = sheet.row_values(i)
for j in range(len(row)) :
if row[j] == search_value :
lines.append(i)
columns.append(j)
del row
return lines, columns

Categories