如何在 Python 脚本中使用文件?
How to use file in Python script?
我正在编写 Python 脚本,但似乎无法理解它的最后一部分。这是代码:
def aggregate(data):
data.sort()
i = 0
while i < len(data) - 1:
while i < len(data) - 1 and data[i][1] >= data[i+1][0]:
data[i] = (data[i][0], max(data[i][1], data[i+1][1]))
data.pop(i+1)
i += 1
if __name__ == '__main__':
itervals = [(1,4), (2,2222), (2,3), (4,7), (8,15), (16,31), (32,63), (64,127), (128,255), (256,511), (512,1023), (1024,2047), (2048,4095), (4096,8191), (8192,16383), (16384,32767), (32768,65535), (65536,131071), (131072,262143), (262144,524287), (524288,1048575), (1048576,2097151), (2097152,4194303), (4194304,8388607), (8388608,16777215)]
formatted = lambda vals: '[{0}]'.format(', '.join('({0}-{1})'.format(
iterval[0], iterval[1])
for iterval in sorted(vals)))
print(formatted(itervals))
aggregate(itervals)
print(formatted(itervals))
现在我被迫手动输入数字范围,正如您在这一行中看到的那样:
itervals = [(1,4), (2,2222), (2,3), (4,7), (8,15), (16,31), (32,63), (64,127), (128,255), (256,511), (512,1023), (1024,2047), (2048,4095), (4096,8191), (8192,16383), (16384,32767), (32768,65535), (65536,131071), (131072,262143), (262144,524287), (524288,1048575), (1048576,2097151), (2097152,4194303), (4194304,8388607), (8388608,16777215)]
相反,我想打开文件 intervals.txt
并使用其中的内容,即:
1,4
2,2222
2,3
4,7
8,15
16,31
32,63
64,127
128,255
256,511
512,1023
1024,2047
2048,4095
4096,8191
8192,16383
16384,32767
32768,65535
65536,131071
131072,262143
262144,524287
524288,1048575
1048576,2097151
2097152,4194303
4194304,8388607
8388608,16777215
如何打开 intervals.txt
文件并改用其内容?其中没有任何括号,所以我不确定这是否会成为问题。此外,范围由换行符而不是逗号分隔(如上所示)。
作为对@sideeffect 的回应,这是您的代码输出的内容:
[(1-4
), (1024-2047
), (1048576-2097151
), (128-255
), (131072-262143
), (16-31
), (16384-32767
), (2-2222
), (2-3
), (2048-4095
), (2097152-4194303
), (256-511
), (262144-524287
), (32-63
), (32768-65535
), (4-7
), (4096-8191
), (4194304-8388607
), (512-1023
), (524288-1048575
), (64-127
), (65536-131071
), (8-15
), (8192-16383
), (8388608-16777215)]
[(8388608-16777215), (1-8388607
)]
这是应该输出的内容:
[(1-4), (2-3), (2-2222), (4-7), (8-15), (16-31), (32-63), (64-127), (128-255), (256-511), (512-1023), (1024-2047), (2048-4095), (4096-8191), (8192-16383), (16384-32767), (32768-65535), (65536-131071), (131072-262143), (262144-524287), (524288-1048575), (1048576-2097151), (2097152-4194303), (4194304-8388607), (8388608-16777215)]
[(1-4095), (4096-8191), (8192-16383), (16384-32767), (32768-65535), (65536-131071), (131072-262143), (262144-524287), (524288-1048575), (1048576-2097151), (2097152-4194303), (4194304-8388607), (8388608-16777215)]
检查 here 了解如何打开文件
问题 1:如何打开 intervals.txt 文件并改用其内容?
itervals = []
# you can use `open` function to read from file
with open("intervals.txt") as f:
for line in f:
# read line by line & append to make the list
# NOTE: a space is also read in line, try strip function to remove it
# to lazy check, uncomment below code, it will print length of line string
# print len(line)
itervals.append(line.split(','))
Q2.There 里面没有任何括号,这是个问题吗?
不,python 不会读取文本文件作为其数据结构,括号中的意思是元组,它是您的脚本,需要修改以进行进一步的数据转换,在上面的代码中,我希望您得到一个逐行读取文件的想法,从那里您可以使用可用的 python 函数将字符串处理为您需要的格式
我认为这将是满足您要求的更好解决方案
data = [line.strip() for line in open("sample.txt", 'r')]
splits=[line.split(",") for line in data]
x=[int(i[0]) for i in splits]
y=[int(i[1]) for i in splits]
final=[tuple([i,j]) for i,j in zip(x,y)]
print final
你会得到这样的结果:
[(1, 4), (2, 2222), (2, 3), (4, 7), (8, 15), (16, 31), (32, 63), (64, 127), (128, 255), (256, 511), (512, 1023), (1024, 2047), (2048, 4095), (4096, 8191), (8192, 16383), (16384, 32767), (32768, 65535), (65536, 131071), (131072, 262143), (262144, 524287), (524288, 1048575), (1048576, 2097151), (2097152, 4194303), (4194304, 8388607), (8388608, 16777215)]
我正在编写 Python 脚本,但似乎无法理解它的最后一部分。这是代码:
def aggregate(data):
data.sort()
i = 0
while i < len(data) - 1:
while i < len(data) - 1 and data[i][1] >= data[i+1][0]:
data[i] = (data[i][0], max(data[i][1], data[i+1][1]))
data.pop(i+1)
i += 1
if __name__ == '__main__':
itervals = [(1,4), (2,2222), (2,3), (4,7), (8,15), (16,31), (32,63), (64,127), (128,255), (256,511), (512,1023), (1024,2047), (2048,4095), (4096,8191), (8192,16383), (16384,32767), (32768,65535), (65536,131071), (131072,262143), (262144,524287), (524288,1048575), (1048576,2097151), (2097152,4194303), (4194304,8388607), (8388608,16777215)]
formatted = lambda vals: '[{0}]'.format(', '.join('({0}-{1})'.format(
iterval[0], iterval[1])
for iterval in sorted(vals)))
print(formatted(itervals))
aggregate(itervals)
print(formatted(itervals))
现在我被迫手动输入数字范围,正如您在这一行中看到的那样:
itervals = [(1,4), (2,2222), (2,3), (4,7), (8,15), (16,31), (32,63), (64,127), (128,255), (256,511), (512,1023), (1024,2047), (2048,4095), (4096,8191), (8192,16383), (16384,32767), (32768,65535), (65536,131071), (131072,262143), (262144,524287), (524288,1048575), (1048576,2097151), (2097152,4194303), (4194304,8388607), (8388608,16777215)]
相反,我想打开文件 intervals.txt
并使用其中的内容,即:
1,4
2,2222
2,3
4,7
8,15
16,31
32,63
64,127
128,255
256,511
512,1023
1024,2047
2048,4095
4096,8191
8192,16383
16384,32767
32768,65535
65536,131071
131072,262143
262144,524287
524288,1048575
1048576,2097151
2097152,4194303
4194304,8388607
8388608,16777215
如何打开 intervals.txt
文件并改用其内容?其中没有任何括号,所以我不确定这是否会成为问题。此外,范围由换行符而不是逗号分隔(如上所示)。
作为对@sideeffect 的回应,这是您的代码输出的内容:
[(1-4
), (1024-2047
), (1048576-2097151
), (128-255
), (131072-262143
), (16-31
), (16384-32767
), (2-2222
), (2-3
), (2048-4095
), (2097152-4194303
), (256-511
), (262144-524287
), (32-63
), (32768-65535
), (4-7
), (4096-8191
), (4194304-8388607
), (512-1023
), (524288-1048575
), (64-127
), (65536-131071
), (8-15
), (8192-16383
), (8388608-16777215)]
[(8388608-16777215), (1-8388607
)]
这是应该输出的内容:
[(1-4), (2-3), (2-2222), (4-7), (8-15), (16-31), (32-63), (64-127), (128-255), (256-511), (512-1023), (1024-2047), (2048-4095), (4096-8191), (8192-16383), (16384-32767), (32768-65535), (65536-131071), (131072-262143), (262144-524287), (524288-1048575), (1048576-2097151), (2097152-4194303), (4194304-8388607), (8388608-16777215)]
[(1-4095), (4096-8191), (8192-16383), (16384-32767), (32768-65535), (65536-131071), (131072-262143), (262144-524287), (524288-1048575), (1048576-2097151), (2097152-4194303), (4194304-8388607), (8388608-16777215)]
检查 here 了解如何打开文件
问题 1:如何打开 intervals.txt 文件并改用其内容?
itervals = []
# you can use `open` function to read from file
with open("intervals.txt") as f:
for line in f:
# read line by line & append to make the list
# NOTE: a space is also read in line, try strip function to remove it
# to lazy check, uncomment below code, it will print length of line string
# print len(line)
itervals.append(line.split(','))
Q2.There 里面没有任何括号,这是个问题吗?
不,python 不会读取文本文件作为其数据结构,括号中的意思是元组,它是您的脚本,需要修改以进行进一步的数据转换,在上面的代码中,我希望您得到一个逐行读取文件的想法,从那里您可以使用可用的 python 函数将字符串处理为您需要的格式
我认为这将是满足您要求的更好解决方案
data = [line.strip() for line in open("sample.txt", 'r')]
splits=[line.split(",") for line in data]
x=[int(i[0]) for i in splits]
y=[int(i[1]) for i in splits]
final=[tuple([i,j]) for i,j in zip(x,y)]
print final
你会得到这样的结果:
[(1, 4), (2, 2222), (2, 3), (4, 7), (8, 15), (16, 31), (32, 63), (64, 127), (128, 255), (256, 511), (512, 1023), (1024, 2047), (2048, 4095), (4096, 8191), (8192, 16383), (16384, 32767), (32768, 65535), (65536, 131071), (131072, 262143), (262144, 524287), (524288, 1048575), (1048576, 2097151), (2097152, 4194303), (4194304, 8388607), (8388608, 16777215)]