Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表列表中的字符串拆分为位于同一列表中的两个元素_Python_Python 3.x_List_Int_Tuples - Fatal编程技术网

Python 将列表列表中的字符串拆分为位于同一列表中的两个元素

Python 将列表列表中的字符串拆分为位于同一列表中的两个元素,python,python-3.x,list,int,tuples,Python,Python 3.x,List,Int,Tuples,这是一个文本文件中的数字链,我导入该文件并希望将其转换为特定列表 3 04,24 4 04,75 4 05,11 4 05,47 4 05,78 4 06,80 3 07,25 3 07,92 3 08,23 2 09,76 实际上,通过我的代码,我达到了这一点: [['3 04,24'], ['4 04,75'], ['4 05,11'], ['4 05,47'], ['4 05,78'], ['4 06,80'], ['3 07,25'], ['3 07,92'], ['3 08,23']

这是一个文本文件中的数字链,我导入该文件并希望将其转换为特定列表

3 04,24
4 04,75
4 05,11
4 05,47
4 05,78
4 06,80
3 07,25
3 07,92
3 08,23
2 09,76
实际上,通过我的代码,我达到了这一点:

[['3 04,24'], ['4 04,75'], ['4 05,11'], ['4 05,47'], ['4 05,78'], ['4 06,80'], ['3 07,25'], ['3 07,92'], ['3 08,23'], ['2 09,76']]
但我想将元组中的元素一分为二,得到如下结果:

[['3','04,24'], ['4','04,75']] etc...
但是经过许多研究,我找不到解决方案,如果你能告诉我如何将这些元素从string转换为int,那将非常有用

这是我的密码:

with open("myfile.txt") as f:
    mylist = [line.rstrip('\n') for line in f]
    mylist = [mylist[x:x+1] for x in range(0, len(mylist), 1)]
    print(mylist)

谢谢

这是一个使用标准库中的
csv
模块的解决方案:

import csv

with open('myfile.txt', 'r') as f:
    reader = csv.reader(f, delimiter=' ')
    res = list(reader)
以您的数据为例:-

from io import StringIO
import csv

mystr = StringIO("""3 04,24
4 04,75
4 05,11
4 05,47
4 05,78
4 06,80
3 07,25
3 07,92
3 08,23
2 09,76""")

with mystr as f:
    reader = csv.reader(f, delimiter=' ')
    res = list(reader)

print(res)

# [['3', '04,24'],
#  ['4', '04,75'],
#  ['4', '05,11'],
#  ['4', '05,47'],
#  ['4', '05,78'],
#  ['4', '06,80'],
#  ['3', '07,25'],
#  ['3', '07,92'],
#  ['3', '08,23'],
#  ['2', '09,76']]
或者,如果需要将数据转换为数字:

with mystr as f:
    reader = csv.reader(f, delimiter=' ')
    res = [[int(i), float(j.replace(',', '.'))] for i, j in reader]

print(res)

[[3, 4.24],
 [4, 4.75],
 [4, 5.11],
 ...

使用列表理解:

>>> lst = [['3 04.24'], ['4 04.75'], ['4 05.11'], ['4 05.47'], ['4 05.78'], ['4 06.80'], ['3 07.25'], ['3 07.92'], ['3 08.23'], ['2 09.76']]
>>> [x[0].split() for x in lst]
产出:

[['3', '04.24'], 
 ['4', '04.75'], 
 ['4', '05.11'], 
 ['4', '05.47'], 
 ['4', '05.78'], 
 ['4', '06.80'], 
 ['3', '07.25'], 
 ['3', '07.92'], 
 ['3', '08.23'], 
 ['2', '09.76']]            
要将字符串转换为整数,请执行以下操作:

[[int(i) if not '.' in i else float(i) for i in x[0].split()] for x in lst]
使用
str.split()
方法:

with open("myfile.txt") as f:
    mylist = [line.rstrip('\n') for line in f]
    my_structured_list = [line.split(" ") for line in mylist]

    print(my_structured_list)
关于将元素转换为int的问题的第二部分,您可以再次使用
str.split()
,并将生成的元素转换为
int

with open("myfile.txt") as f:
    mylist = [line.rstrip('\n') for line in f]
    my_structured_list = [line.split(" ") for line in mylist]

    my_structured_int_list = []
    for line_tuple in my_structured_list:
        input_first_element = line_tuple[0]
        input_second_element, input_third_element = line_tuple[1].split(",")

        output_first_half = int(input_first_element)
        output_second_half = int(input_second_element), int(input_third_element)

        my_structured_int_list.append((output_first_half, output_second_half))

    print(my_structured_int_list)

简单的解决方案如下

with open(file,'r') as f:
    print([each.split() for each in f])

它也可以工作,但我如何将这些字符串链转换为int?我已经将每秒钟的数字转换为一个点,我想这会更容易。谢谢,所以我将使用不同的列表,一个是第一个数字,另一个是第二个数字。我想要的看起来像jpp的解决方案,但你也真的很有帮助,泰!好的,没关系。只是提醒一下:这里没有标题所说的元组。这是一个列表列表。