简体   繁体   English

使用 Python 将二维字符列表转换为字符串列表

[英]Convert a 2d character list into a list of strings using Python

I aim to convert a 2d character list into an list of strings, whose characters were the individual values of the former lists using Python.我的目标是将 2d 字符列表转换为字符串列表,其字符是使用 Python 的前列表的各个值。

Example:例子:

[['a', 'b', 'c', 'd'],
 ['e', 'f', 'g', 'h']]

Should be returned as应该返回为

['abcd', 'efgh']

This should be by joining the values of the former lists inside the original list together into one string.这应该是通过将原始列表中前一个列表的值连接到一个字符串中。

You can try:你可以试试:

>>> data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]
>>> ["".join(d) for d in data]
['abcd', 'efgh']

You can assign the 2D list (list is the term for an array used in Python):您可以分配二维列表(列表是 Python 中使用的数组的术语):

data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]

Next you can loop through each list inside the data list and assign the new value to the original value in the list:接下来,您可以遍历data列表中的每个列表,并将新值分配给列表中的原始值:

idx = 0
for x in data:
    data[idx] = "".join(x)
    idx += 1

This will return the required output.这将返回所需的输出。

Full Code:完整代码:

data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]
idx = 0
for x in data:
    data[idx] = "".join(x)
    idx += 1

Test your code by adding the following two lines:通过添加以下两行来测试您的代码:

print(f'First value in list: {data[0]} and second value in list: {data[1]}')

Explanation:解释:

data = [["a", "b", "c", "d"], ["e", "f", "g", "h"]]

The line of code above assigns the 2D list to a variable to be used on later.上面的代码行将 2D 列表分配给稍后要使用的变量。

idx = 0

The variable idx is used to assign the required value to each of the former values of the 2D list data .变量idx用于将所需的值分配给 2D 列表data每个先前值。 You can also assign each value to a separate list by using the .append method.您还可以使用.append方法将每个值分配给单独的列表。

for x in data:

This loops through each value (or list) inside of the list data , using the variable x to be referenced as in the code.这将循环遍历列表data每个值(或列表),使用变量x在代码中被引用。 This can also be done using a while loop.这也可以使用while循环来完成。

data[idx] = "".join(x)

This assigns the former value with the index of idx in the list with the new value.这会将具有新值的列表中的idx索引分配给前一个值。 This uses the python .join method to connect the values in the list by a "".这使用 python .join方法通过“”连接列表中的值。

To read more about the Python .join method visit:要阅读有关 Python .join方法的更多信息,请访问:

https://www.w3schools.com/python/ref_string_join.asp https://www.w3schools.com/python/ref_string_join.asp

idx += 1

This is used to add 1 to the former value of idx so that it outputs the new index of the next value in the list used in the for loop.这用于将idx的前一个值加1 ,以便它输出for循环中使用的列表中下一个值的新索引。

Testing Your Code测试你的代码

print(f'First value in list: {data[0]} and second value in list: {data[1]}')

the above line of code is a simple formatted string that outputs the first data[0] and second data[1] values of the variable data .上面这行代码是一个简单的格式化字符串,它输出变量data的第一个data[0]和第二个data[1]值。

I hope this helps you with your coding and understanding your code.我希望这可以帮助您进行编码和理解您的代码。

你可以试试这个:

[""join(d) for d in data]

Try this,尝试这个,

>>> data = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']]
>>> list(map(''.join, data))
['abcd', 'efgh']

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

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