Python中如何对比CSV列表相邻元素并输出1或0
Solution
First, we need to clean your dataset to remove non-numeric entries and separator rows, then perform the adjacent value comparison. Here's a step-by-step implementation:
Step 1: Clean the Data
Filter out separator rows (those with - and NaN values) and extract only numeric values from valid data rows:
import pandas as pd # Your existing dataset (h111) h111 = [['2', '1', ',', '5', '6', '9', '.', '6.1', '3'], ['2', '1', ',', 5.0, 6.0, 9.0, '.', 6.0, 3.0], ['1', '8', ',', 9.0, 5.0, 2.0, '.', 9.0, 2.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['1', '8', ',', 9.0, 4.0, 4.0, '.', 3.0, 6.0], ['1', '8', ',', 9.0, 4.0, 4.0, '.', 3.0, 6.0], ['1', '8', ',', 9.0, 4.0, 4.0, '.', 3.0, 6.0], ['1', '8', ',', 9.0, 4.0, 4.0, '.', 3.0, 6.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['1', '8', ',', 9.0, 4.0, 4.0, '.', 3.0, 6.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['2', '0', ',', 1.0, 5.0, 0.0, '.', 5.0, 3.0], ['2', '0', ',', 1.0, 5.0, 0.0, '.', 5.0, 3.0], ['2', '0', ',', 1.0, 5.0, 0.0, '.', 5.0, 3.0], ['2', '0', ',', 1.0, 5.0, 0.0, '.', 5.0, 3.0], ['2', '0', ',', 1.0, 6.0, 0.0, '.', 8.0, 6.0], ['2', '0', ',', 1.0, 8.0, 2.0, '.', 5.0, 9.0], ['2', '0', ',', 1.0, 7.0, 8.0, '.', 0.0, 4.0], ['2', '0', ',', 0.0, 6.0, 8.0, '.', 3.0, 2.0], ['2', '0', ',', 0.0, 5.0, 6.0, '.', 8.0, 6.0], ['2', '0', ',', 0.0, 5.0, 6.0, '.', 8.0, 6.0], ['2', '0', ',', 0.0, 5.0, 6.0, '.', 8.0, 6.0], ['2', '0', ',', 0.0, 5.0, 6.0, '.', 8.0, 6.0], ['2', '0', ',', 0.0, 8.0, 0.0, '.', 2.0, 1.0], ['2', '0', ',', 0.0, 3.0, 7.0, '.', 8.0, 2.0], ['1', '9', ',', 9.0, 6.0, 8.0, '.', 9.0, 6.0], ['1', '9', ',', 8.0, 4.0, 6.0, '.', 7.0, 0.0], ['1', '9', ',', 8.0, 4.0, 1.0, '.', 8.0, 0.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['1', '9', ',', 3.0, 4.0, 0.0, '.', 8.0, 4.0], ['1', '8', ',', 9.0, 1.0, 2.0, '.', 4.0, 4.0], ['1', '8', ',', 9.0, 1.0, 2.0, '.', 4.0, 4.0], ['1', '8', ',', 8.0, 8.0, 6.0, '.', 6.0, 2.0], ['1', '8', ',', 8.0, 8.0, 6.0, '.', 6.0, 2.0], ['1', '8', ',', 8.0, 4.0, 7.0, '.', 5.0, 6.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['1', '8', ',', 9.0, 9.0, 1.0, '.', 2.0, 2.0], ['1', '9', ',', 0.0, 0.0, 8.0, '.', 3.0, 3.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['1', '9', ',', 0.0, 4.0, 4.0, '.', 1.0, 0.0], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['5', '.', '4', 0.0, float('nan'), float('nan'), float('nan'), float('nan'), float('nan')], ['-', '-', float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan'), float('nan')]] # Filter separator rows and extract numeric values clean_rows = [] for row in h111: # Skip separator rows (identified by first element '-' and third element NaN) if row[0] == '-' and pd.isna(row[2]): continue # Extract numeric values from the row numeric_vals = [] for val in row: try: # Convert value to float (handles string numbers like '2' and numeric types) num = float(val) numeric_vals.append(num) except (ValueError, TypeError): # Skip non-numeric values like ',', '.' continue if numeric_vals: clean_rows.append(numeric_vals) # Flatten into a single list of numeric values flattened_numeric = [num for row in clean_rows for num in row]
Step 2: Compare Adjacent Values
Iterate through the cleaned list and generate the result (1 if current > next, 0 otherwise; equal values default to 0):
result = [] for i in range(len(flattened_numeric) - 1): current = flattened_numeric[i] next_val = flattened_numeric[i + 1] if current > next_val: result.append(1) else: # Covers both current < next and current == next result.append(0) print(result)
Notes
- If you want to handle equal values differently (e.g., output
Noneor a separate flag), modify the else clause accordingly. - If you need to compare elements row-wise (each element in a row with the corresponding element in the next row), use this alternative code instead of flattening:
row_wise_result = [] for i in range(len(clean_rows) - 1): row1 = clean_rows[i] row2 = clean_rows[i + 1] # Compare up to the length of the shorter row min_length = min(len(row1), len(row2)) row_result = [] for j in range(min_length): val1 = row1[j] val2 = row2[j] row_result.append(1 if val1 > val2 else 0) row_wise_result.append(row_result) print(row_wise_result)
内容的提问来源于stack exchange,提问作者user 456354
相关产品推荐
相关产品推荐

