如何在python中为数组赋值

2024-04-19 20:36:47 发布

您现在位置:Python中文网/ 问答频道 /正文

x1 contains 10008 random value (10008 rows, 1 columns)

file=open('random.txt','r')
x1=file.readlines()

now i want to assign 1-5000 random values from x1 to x e.g:

x=x1(5000,1)

now half other 5000-10009 values assign to other variable from x1 to xx e.g:

xx=x1(10008,5000)

i did'nt understand how to write this in python.


Tags: columnstofromvaluerandomopennowfile
2条回答

试试这个:

file = open('random.txt', 'r')
x = []
xx = []
cnt = 0
for line in file:
    if cnt <= 5000:
        x.append(line.strip())
    else:
        xx.append(line.strip())
    cnt += 1

我认为这里没有必要进行柜台核对。只需使用slice检索目标列表。你知道吗

with open('random.txt','r') as f:
    x1 = file.read().splitline()
x = x1[:5000]
xx = x1[5000:]  

相关问题 更多 >