简体   繁体   English

Python for loop 打印水平线

[英]Python for loop prints horizontal line

I'm quite new to python and can't really understand why sometimes for loop prints answer vertically or in a horizontal line我对 python 很陌生,我真的不明白为什么有时 for 循环打印会垂直或水平线回答

import pandas as pd

df = pd.read_csv('climate_data_Dec2017.csv')

col = ['State','9am relative humidity (%)']

hum = df[col]

grouped_by_state = hum.groupby('State')

max_hum = grouped_by_state.max()

dict = max_hum.to_dict()


for key in dict:
  print(dict[key])

It prints answer like它打印答案,如

{'NSW': 100, 'NT': 70, 'QLD': 99, 'SA': 84, 'VIC': 98, 'WA': 89}

But I want to print this vertically so I've done \n , for loop but I couldn't make it Any helps?但是我想垂直打印,所以我已经完成了\n , for 循环,但我做不到有帮助吗?

This is the answer I want这就是我想要的答案

NSW : 100.0

NT : 70.0

QLD : 99.0

... so on

If you want both the key and the value of a dictionary, you can include the key and the look up in the print statement.如果你想要字典的键和值,你可以在打印语句中包含键和查找。

As another commenter said, the "dict" variable should not be overwritten.正如另一位评论者所说,“dict”变量不应被覆盖。

Try the following尝试以下

import pandas as pd

df = pd.read_csv('climate_data_Dec2017.csv')

col = ['State','9am relative humidity (%)']

hum = df[col]

grouped_by_state = hum.groupby('State')

max_hum = grouped_by_state.max()

dictionary = max_hum.to_dict()


for key in dictionary:
  print(key, ": ", dictionary[key])

This returned the following这返回了以下内容

NSW :  100
NT :  70
QLD :  99
SA :  84
VIC :  98
WA :  89

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM