Python 如何将标签指定给列表列表中的特定项
我正在计算csv行中的最小值(我已将其中的一行转换为标准化列表),并且难以将原始csv标题/标题与该行中相应的最小值分配(为了使标准化更容易,我省略了这两个行)。以下是我的工作内容: 我的标准化列表(每个子列表是我的csv文件中的一行) 我使用以下公式计算每个子列表(行)的最小值 显然,输出将是Python 如何将标签指定给列表列表中的特定项,python,python-3.x,list,Python,Python 3.x,List,我正在计算csv行中的最小值(我已将其中的一行转换为标准化列表),并且难以将原始csv标题/标题与该行中相应的最小值分配(为了使标准化更容易,我省略了这两个行)。以下是我的工作内容: 我的标准化列表(每个子列表是我的csv文件中的一行) 我使用以下公式计算每个子列表(行)的最小值 显然,输出将是 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 但我想要的不是打印出数字来打印该数字来自哪一列(每一列都有一个字符串标题,例如,前0.0来自我的第二个最后一列[5],称为慷慨)。以及打印
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
但我想要的不是打印出数字来打印该数字来自哪一列(每一列都有一个字符串标题,例如,前0.0来自我的第二个最后一列[5],称为慷慨)。以及打印标题(位于第一列,该列第一行为阿富汗)
所以我需要计算每行的最小值,并在下面计算出来:
Australia's happiness is bound by its header4
Algeria's happiness is bound by its header5
将
标题
设为列标题,列出所有其他行(包括国家/地区列)。然后,执行以下操作:
headers = ['country', ...]
for mList in list_of_lists:
cur_min = 1000
min_index = 0
for col_index, item in enumerate(mList[1:]):
if item < cur_min:
cur_min = item
min_index = col_index
print(mList[0] + "'s happiness is bound by its " + headers[min_index + 1])
如果你可以使用熊猫,这可以很容易做到。
首先,将CSV作为数据帧导入pandas,并使用
df=pd.read_csv('filename.csv') #need to look-up other postas which will help you read your CSV into pandas as a dataframe.
然后使用下面的代码
for index, row in df.iterrows():
print (row['country,'] + ' happiness is bound by its ' + df.columns[row.values == 0][0])
我的输入是数据框,如下所示
country, header2, header3, header4, header5
0 Australia 1.000000 0.343535 0.0 0.231242
1 Algeria 0.343434 0.434343 1.0 0.000000
输出
Australia happiness is bound by its header4,
Algeria happiness is bound by its header5
我不确定我是否完全理解这个问题,但这将允许您以CSV格式阅读并输出所需内容,而无需将其放入列表中。我尽量避免像熊猫这样的东西,因为它是吉班戈斯的,也许这太过分了。不过,这些库无疑是进行更复杂工作的途径
对于类似于此结构的csv
country,header1,header2,header3,header4,header5,header6
Algeria,1,2,55,3,2,3
Australia,33,2,8,3,99,0
UnitedStates,9,8,7,6,5,4
你可以使用这个代码
import csv
with open('file.csv', newline="\n") as f:
reader = csv.DictReader(f)
for row in reader:
# do whatever normalization to row values you need to do
minval = min(([v for i,v in enumerate(row.values()) if i != 0]))
i = [v for i,v in enumerate(row.values()) if i != 0].index(minval)
h = [r for r in row.keys()][i+1]
print(f"{row['country']}'s happiness is bound by its {h}")
如果必须从列表列表中工作,可以将标题放入变量中,使用list.index
函数捕获最小值的索引,并使用我在粘贴的代码段中初始化I
的方法从该索引中引用正确的标题
headers = ['header1', 'header2', ...]
countries = ['a', 'bunch', 'o', 'countries', ...]
for ci,row in enumerate(list_of_lists):
minval = min(row)
i = row.index(minval)
h = headers[i]
print(f"{countries[ci]}'s happiness is bound by its {h}")
我希望我抓住了你的目标。祝你好运 我想你是说
header = ['country', 'header2', 'header3', 'header4', 'header5', 'header6']
[header[p.index(min(p))] for p in norm_row_list]
# ['header5', 'header5', 'header5', 'header5', 'header5', 'header5']
鉴于您在上面提供的norm\u row\u list
。您的第二个问题不清楚。请提供更多数据(包括您提到的姓名)。这将有助于提供更好的解决方案。添加了一些额外的数据。您的示例必须有7列。正当(因为你的列表有6个元素)是的,只是更新了它以反映它
import csv
with open('file.csv', newline="\n") as f:
reader = csv.DictReader(f)
for row in reader:
# do whatever normalization to row values you need to do
minval = min(([v for i,v in enumerate(row.values()) if i != 0]))
i = [v for i,v in enumerate(row.values()) if i != 0].index(minval)
h = [r for r in row.keys()][i+1]
print(f"{row['country']}'s happiness is bound by its {h}")
headers = ['header1', 'header2', ...]
countries = ['a', 'bunch', 'o', 'countries', ...]
for ci,row in enumerate(list_of_lists):
minval = min(row)
i = row.index(minval)
h = headers[i]
print(f"{countries[ci]}'s happiness is bound by its {h}")
header = ['country', 'header2', 'header3', 'header4', 'header5', 'header6']
[header[p.index(min(p))] for p in norm_row_list]
# ['header5', 'header5', 'header5', 'header5', 'header5', 'header5']