简体   繁体   English

将整数的 .txt 拆分为列表 Python

[英]Splitting .txt of integers into list Python


I am considering doing the Google Hash Code , but am running into some issues on the practice problems !我正在考虑做谷歌哈希码,但在实践问题遇到了一些问题 The problem is order a number of pizza slices without going over the limit.问题是在不超过限制的情况下订购许多披萨片。 The input gives you the different quantities of slices for each type.输入为您提供每种类型的不同切片数量。 This is the c_medium.in input file:这是 c_medium.in 输入文件:

4500 50
7 12 12 13 14 28 29 29 30 32 32 34 41 45 46 56 61 61 62 63 65 68 76 77 77 92 93 94 97 103 113 114 114 120 135 145 145 149 156 157 160 169 172 179 184 185 189 194 195 195

To determine my options for sizes, I am using this code:为了确定我的尺寸选项,我使用了以下代码:

file = open('c_medium.in','r')
raw_pizza_types = file.readline(2)
pizza_types = raw_pizza_types.split()
print(pizza_types)
max = file.readline(1)
def solution() -> None:
  #pizza_types = [int(i) for i in pizza_types] # will loop through strings and convert them to ints 
  pass

This code should print out a list with the number of slices on different pies, but instead simply prints out ['45'] .这段代码应该打印出一个包含不同馅饼上切片数量的列表,而只是简单地打印出['45'] Can anyone help me fix this?谁能帮我解决这个问题?

the parameter in readline() indicates size to be read, not the number of lines to be read. readline()的参数表示要读取的大小,而不是要读取的行数。 So you're telling it to read in only the first two characters, which are 45 and then stop.所以你告诉它只读取前两个字符,即 45,然后停止。

What you want to be doing is using the command readlines() , which by default reads in all lines as a list.您想要做的是使用命令readlines() ,默认情况下,该命令将所有行作为列表读取。 You would then just have to process the data out of the list.然后,您只需处理列表中的数据。 I would recommend something along the lines of:我会推荐一些类似的东西:

file = open('filename', 'r')
raw_pizzas = file.readlines()
slices = []
for p in raw_pizzas:
    for s in p.split():
        slices.append(s)
print(slices)

please note that this is meant as more of pseudocode, I have not tested to make sure it works as written.请注意,这意味着更多的伪代码,我没有测试以确保它按编写的方式工作。

The readline method's parameter is size and does not read the second line, which I'm assuming is what you want to do. readline方法的参数是size并且不读取第二行,我假设这是您想要做的。 File-handles are iterators, and can't go back to a previous line unless you seek .文件句柄是迭代器,除非您seek . So I would read in your variables in the order that they appear in the file:所以我会按照它们出现在文件中的顺序读入你的变量:

# the with statement is the pythonic way to open files
# since you don't need to remember to close them
with open('c_medium.in','r') as fh:
    # read the first line to max, but max itself is a function
    # so we will name it something else
    maximum_slices = [int(x) for x in next(fh).split()]

    # this will split the second line on any whitespace character
    pizza_types = next(fh).split()

Your list comprehension should be perfectly sufficient after that.之后你的列表理解应该是完全足够的。 I am also assuming that the maximum_slices should also be a list of integers我还假设maximum_slices也应该是一个整数列表

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

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