简体   繁体   English

如何将包含字符串的列表转换为包含整数的列表(Python)

[英]How to turn a list containing strings into a list containing integers (Python)

I am optimizing PyRay ( https://github.com/oscr/PyRay ) to be a usable Python ray-casting engine, and I am working on a feature that takes a text file and turns it into a list (PyRay uses as a map).我正在优化 PyRay ( https://github.com/oscr/PyRay ) 以使其成为一个可用的 Python 光线投射引擎,并且我正在开发一个将文本文件转换为列表的功能(PyRay 用作地图)。 But when I use the file as a list, it turns the contents into strings, therefore not usable by PyRay.但是当我将文件用作列表时,它会将内容转换为字符串,因此 PyRay 无法使用。 So my question is: How do I convert a list of strings into integers?所以我的问题是:如何将字符串列表转换为整数? Here is my code so far.到目前为止,这是我的代码。 (I commented the actual code so I can test this) (我评论了实际的代码,所以我可以测试一下)

print("What map file to open?")
mapopen = input(">")

mapload = open(mapopen, "r")
worldMap = [line.split(',') for line in mapload.readlines()]
print(worldMap)

The map file: map 文件:

1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,2,0,0,3,0,0,0,0,0,0,0,2,3,2,3,0,0,2,
2,0,3,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,3,1,0,0,2,0,0,0,2,3,2,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,0,1,0,0,1,2,0,0,0,2,
2,0,0,0,0,0,0,0,0,2,0,2,0,0,2,1,0,0,0,1,
1,0,0,0,0,0,0,0,0,1,3,1,0,0,0,0,0,0,0,2,
2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,2,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,3,0,0,2,0,0,0,0,0,0,0,2,3,2,1,2,0,1,
1,0,0,0,0,3,0,0,0,0,0,0,0,1,0,0,2,0,0,2,
2,3,1,0,0,2,0,0,2,1,3,2,0,2,0,0,3,0,3,1,
1,0,0,0,0,0,0,0,0,3,0,0,0,1,0,0,2,0,0,2,
2,0,0,0,0,0,0,0,0,2,0,0,0,2,3,0,1,2,0,1,
1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,3,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,1,
2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,

Please help me, I have been searching all about and I can't find anything.请帮助我,我一直在搜索,但我找不到任何东西。

try this: Did you want a list of lists?试试这个:你想要一个列表列表吗? or just one big list?还是只是一个大清单?

with open(filename, "r") as txtr:
    data = txtr.read()
data = txtr.split("/n")  # split into list of strings
data = [ list(map(int, x.split(","))) for x in data]

fourth line splits string into list by removing comma, then appliea int() on each element then turns it into a list.第四行通过删除逗号将字符串拆分为列表,然后在每个元素上应用 int(),然后将其转换为列表。 It does this for every element in data.它对数据中的每个元素执行此操作。 I hope it helps.我希望它有所帮助。

Here is for just one large list.这里只是一个大列表。

with open(filename, "r") as txtr:
    data = txtr.readlines()  # remove empty lines in your file!
data = ",".join(data)  # turns it into a large string
data = data.split(",")  # now you have a list of strings
data = list(map(int, data))  # applies int() to each element in data.

Look into the map built-in function in python.查看 python 中的map内置 function。

L=['1', '2', '3']
map = map(int, L)
for el in map:
    print(el)

>>> 1
... 2
... 3

As per you question, please find below a way you can change list of strings to list of integers (or integers if you use list index to get the integer value).根据您的问题,请在下面找到一种可以将字符串列表更改为整数列表(或整数,如果您使用列表索引来获取 integer 值)的方法。 Hope this helps.希望这可以帮助。

myStrList = ["1","2","\n","3"]

global myNewIntList 

myNewIntList = []

for x in myStrList:
    if(x != "\n"):
        y = int(x)
        myNewIntList.append(y)


print(myNewIntList)

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

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