使用FOR循环打印相应的CSV值。

huangapple go评论62阅读模式
英文:

Printing corresponding CSV values using FOR loop

问题

这是我的第一篇帖子,在这里我有点迷茫,因为我对循环不太熟悉。我被要求编写一个程序,接受一个整数输入,并使用该输入打印出CSV/Excel列表上的相应值。我已经做到了在输入的基础上打印出名称和值,但格式不是我需要的方式,而且我的脚本有点笨重。

具体来说,我必须打印出各州和人口。如果我输入5,它将从0到5打印出名称和人口。

我不能使用Pandas,因为我还是个初学者,而且这是为了一个课程。

这是我的代码:

print('给我一个介于0和50之间的数字:')
user_num = int(input())
for name in state_names:
    if name == state_names[user_num]:
        names = state_names[0:user_num]
for pop in state_populations:
    if pop == state_populations[user_num]:
        pops = state_populations[0:user_num]
print(f'{names}, {pops}')

如果输入是3,输出如下:

['Michigan', 'Georgia', 'Ohio'], [9986857, 10617423, 11689100]

而不是我需要的:

Michigan   9986857
Georgia   10617423
Ohio      11689100

我应该如何缩短代码并使其产生所需的输出?

英文:

This is my first post here, and I'm a bit lost on this scenario since I'm new to loops. I'm being asked to write a program that takes an integer input, and uses that input to print out the corresponding values on a CSV/Excel list. I was able to get to a point where it printed both the names and values based on the input, but the formatting is not the right way I need it to be, and my script is a bit clunky.

Specifically, I have to print states and populations. If I put in 5, it will print out both the names and populations from 0 to 5.

I can't use Pandas, as I'm still a beginner, and this is for a course.

This is where I am.


    print('Give me a number thats 0 - 50:')
    user_num = int(input())
    for name in state_names:
        if name == state_names[user_num]:
            names = state_names[0:user_num]
    for pop in state_populations:
        if pop == state_populations[user_num]:
            pops = state_populations[0:user_num]
    print(f'{names}, {pops}')

If the input was 3, the output comes out to:

['Michigan', 'Georgia', 'Ohio'], [9986857, 10617423, 11689100] 

instead of

Michigan   9986857
Georgia   10617423
Ohio      11689100

which is the output I need.

What can I do to shorten it and make it produce that output?

答案1

得分: 2

如果我理解正确,您可以使用 zip() 函数:

state_names = ['Michigan', 'Georgia', 'Ohio', 'New York']
state_populations = [9986857, 10617423, 11689100, 99999]

print('Give me a number:')
user_num = int(input())

for n, p in zip(state_names[:user_num], state_populations[:user_num]):
    print(f'{n:<10} {p:>10}')

打印输出:

Give me a number:
3
Michigan      9986857
Georgia      10617423
Ohio         11689100
英文:

If I understand you correctly you can use zip() function:

state_names = [&#39;Michigan&#39;, &#39;Georgia&#39;, &#39;Ohio&#39;, &#39;New York&#39;]
state_populations = [9986857, 10617423, 11689100, 99999]

print(&#39;Give me a number:&#39;)
user_num = int(input())

for n, p in zip(state_names[:user_num], state_populations[:user_num]):
    print(f&#39;{n:&lt;10} {p:&gt;10}&#39;)

Prints:

Give me a number:
3
Michigan      9986857
Georgia      10617423
Ohio         11689100

huangapple
  • 本文由 发表于 2023年3月8日 18:49:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定