Python - 从带有整数和字符串的文本文件中查找最大值和最小值

发布于 2025-01-14 10:57:07 字数 167 浏览 2 评论 0原文

我一直在尝试从文本文件中获取最大/最小值,但我不知道如何只获取文本的整数,python 总是获取文本中的字符串并给我一个错误。 这是文本文件的示例 在此处输入图片说明 谢谢你,

I've been trying to get the maximum/minimum value from a text file, but I don't know how to only take the integer of the text, python always take the string in the text and give me an error.
Here's an exemple of the text file
enter image description here
Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2025-01-21 10:57:07

您可以使用 pandas.DataFrame.dtypes 查找文本的 int 值。这是您的数据,我假设您将其保存为 test.txt 文件:

E1    10    14    15
E2    10    13    10
E3    20    17    18
E4    13    10    11
E5    12    20    12
E6    14    16    14
E7    13    12    08
E8    14    16    15

您可以使用 Pandas 读取数据,然后查找包含 int 的列值。然后你可以搜索最大整数值:

import pandas as pd

df = pd.read_csv("test.txt" , delimiter="\s+" , header= None)

maximum = 0
for col in df.columns:
    if df.dtypes[col] == "int64":
        if max(df[col])>maximum:
            maximum = max(df[col])

然后如果你尝试print(maximum),你会得到这个:

20

所以关键部分是检查每列成员的类型如果您尝试print(df.dtypes),您将得到:

0    object
1     int64
2     int64
3     int64
dtype: object

这意味着,第一列(其中包含 E1、E2、...、E8 ) 有一个 object 类型,并且其他的具有 int64 类型(您正在寻找)。

You can find int values of your text using pandas.DataFrame.dtypes. Here is your data and I assume that you saved it as a test.txt file:

E1    10    14    15
E2    10    13    10
E3    20    17    18
E4    13    10    11
E5    12    20    12
E6    14    16    14
E7    13    12    08
E8    14    16    15

You can read the data using Pandas and then find the columns that contain int values. Then you can search for the maximum integer value:

import pandas as pd

df = pd.read_csv("test.txt" , delimiter="\s+" , header= None)

maximum = 0
for col in df.columns:
    if df.dtypes[col] == "int64":
        if max(df[col])>maximum:
            maximum = max(df[col])

Then if you try to print(maximum), you'll get this:

20

So the critical part is to check the type of members of each column of the DataFrame! If you try to print(df.dtypes), you'll get this:

0    object
1     int64
2     int64
3     int64
dtype: object

This means, the first column(which contained E1, E2, ..., E8) has an object type, and the other ones have an int64 type(which you're looking for).

诗笺 2025-01-21 10:57:07

除非需要,否则不要使用 Pandas。有一个更干净、更简单的解决方案:

# Open the file
with open("filename.txt") as file:  # edit file name here
    text = file.readlines()  # a list of strings, each representing a line


# Get the values
values_2d = [[int(j) for j in i.split()[1:]] for i in text]  # turn them into integers (ignoring the first column), but it's still two-dimensional
values = sum(values_2d, []) # turn into a 1-dimensional list
print(f"Maximum: {max(values)}, minimum: {min(values)}")  # output

它在 sum(,[]) 部分效率有些低,但这只是为了简洁而使用。

Don't use Pandas unless needed. There's a much cleaner, simpler solution:

# Open the file
with open("filename.txt") as file:  # edit file name here
    text = file.readlines()  # a list of strings, each representing a line


# Get the values
values_2d = [[int(j) for j in i.split()[1:]] for i in text]  # turn them into integers (ignoring the first column), but it's still two-dimensional
values = sum(values_2d, []) # turn into a 1-dimensional list
print(f"Maximum: {max(values)}, minimum: {min(values)}")  # output

It's somewhat inefficient in the sum(,[]) part, but that's only been used for brevity.

一袭白衣梦中忆 2025-01-21 10:57:07

您还没有标记或提及任何库,因此我假设您需要普通 Python 中的解决方案。你的文件格式有点明确,你想找到哪个最小的整数?如果您想分别获取每行中的最小整数,您可以打开文件,将行读入列表并从中提取整数:

with open("file.txt", "r") as f:
    lines = f.readlines()

for line in lines:
    # Find the first spacebar in the line, then slice
    # it from there to the end, and remove trailing newline
    temp = line[line.index(' '):].strip()

    # Loop through each number, but since there are multiple
    # spaces between numbers, skip empty strings
    # This can be done more easier with regular expressions
    numbers = temp.split(' ')
    # Look up the filter function on Python docs to learn what it does
    numbers = filter(lambda x: (not not x) or x == '0', map(int, numbers))
    print(min(numbers), max(numbers))

You haven't tagged or mentioned any libraries so I'm going to assume you want a solution in vanilla Python. Your file format is a bit unambiguous, which smallest integer do you want to find? If you want to get the smallest integer in each row separately, you can open the file, read the lines into a list and extract the integers out of it:

with open("file.txt", "r") as f:
    lines = f.readlines()

for line in lines:
    # Find the first spacebar in the line, then slice
    # it from there to the end, and remove trailing newline
    temp = line[line.index(' '):].strip()

    # Loop through each number, but since there are multiple
    # spaces between numbers, skip empty strings
    # This can be done more easier with regular expressions
    numbers = temp.split(' ')
    # Look up the filter function on Python docs to learn what it does
    numbers = filter(lambda x: (not not x) or x == '0', map(int, numbers))
    print(min(numbers), max(numbers))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文