Java 中 nextInt()、hasNext() 的 Python 实现是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7395047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:46:34  来源:igfitidea点击:

What are Python implementation of nextInt(), hasNext() from Java?

javapython

提问by Tu Hoang

I have this input file

我有这个输入文件

1 2
10 2
81 3
23 6
2537857295 19
34271891003654321 1267253

I am reading the file like this

我正在阅读这样的文件

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        print line

I am wondering if I want to, for every single line, have the 1st integer stored as firstNum, the 2nd stored as secondNum. With Java I can use a scannerand do nextInt()and hasNext()to get the integers, what are the equivalent in Python?

我想知道我是否想要,对于每一行,将第一个整数存储为firstNum,第二个存储为secondNum. 在Java我可以使用scanner,做nextInt()hasNext()得到的整数,是什么在Python等价?

回答by S.Lott

Please discard the Java ideas. They will just confuse you.

请丢弃 Java 的想法。他们只会让你感到困惑。

Python is a different language, and you must learn the Pythonic approach.

Python 是一种不同的语言,您必须学习 Pythonic 方法。

Here's an example.

这是一个例子。

with open("powersearch.txt") as fileIn:
    for firstNum, secondNum in ( map(int, line.split()) for line in fileIn ):
        print firstNum, secondNum

回答by orlp

Well to parse an int from a string you just use int(s), where sis the string.

那么从你刚使用的字符串中解析一个 int ,字符串int(s)在哪里s

I think this would be the most logic way in your example:

我认为这将是您示例中最合乎逻辑的方式:

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        n1, n2 = (int(s) for s in line.split())
        print(n1, n2)

Python is a different language than Java, and in my opinion more expressive (I can do more in one line than I can in Java and still write readable code). If you try to write Java stuff in Python you'll find the language a lot less effective than it can be.

Python 是一种不同于 Java 的语言,在我看来更具表现力(我可以在一行中做的比在 Java 中做的更多,并且仍然编写可读的代码)。如果您尝试用 Python 编写 Java 内容,您会发现该语言的效率远低于它所能达到的效果。

回答by Weeble

EDIT - nightcracker's solution is pretty much equivalent now.

编辑 - nightcracker 的解决方案现在几乎是等价的。

(I'm assuming you're using Python 2.x and not 3.x from the syntax.)

(我假设您使用的是 Python 2.x 而不是语法中的 3.x。)

It's a matter of taste, but I would prefer a mix of S.Lott and nightcracker's solutions:

这是一个品味问题,但我更喜欢 S.Lott 和 nightcracker 的解决方案的混合:

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        firstNum, secondNum = [int(s) for s in line.split()]
        print firstNum, secondNum

List comprehension is a bit easier to read than map for me. Destructuring assignment lets us take the two item list and assign the element values to two different local variables at once.

对我来说,列表理解比地图更容易阅读。解构赋值让我们可以将两个项目列表同时将元素值分配给两个不同的局部变量。

回答by gomezportillo

The answers before (n1, n2 = (int(s) for s in line.split())) will only work if you are using a couple of numbers. Otherwise, you will need to use the the mapfunction.

( n1, n2 = (int(s) for s in line.split()))之前的答案仅在您使用几个数字时才有效。否则,您将需要使用该map功能。

with open("powersearch.txt") as fileIn:
    for line in fileIn:
        line_int = list(map(int, line.split()))
        print(line_int)