列表中多个元素怎么转成多个字符串 (python)
本帖最后由 hellokz 于 2020-9-12 03:17 编辑纯新手求助:
如:old = [[‘a','b','c'],['2',5','7']]
怎么才能转成
old = 'abc'
old = '257'
谢谢!
忘了说,我想要phthon的 本帖最后由 风过无痕1989 于 2020-9-12 02:32 编辑
时间不早了,直接给你一个程序:
// C 语言字符串连接的 3种方式
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *join(char *, char*);
int main(void) {
char a = "abc"; // char *a = "abc"
char b = "def"; // char *b = "def"
char *c = join(a, b); // 调用函数
printf("Concatenated String is %s\n", c);
free(c);
c = NULL;
return 0;
}
char* join(char *s1, char *s2)
{
char *result = malloc(strlen(s1)+strlen(s2)+1); //+1 for the zero-terminator
//in real code you would check for errors in malloc here
if (result == NULL) exit (1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
{:10_301:}感谢感谢!不过我忘了说我想要Phthon的{:10_269:} hellokz 发表于 2020-9-12 03:16
感谢感谢!不过我忘了说我想要Phthon的
错字def function(strs) -> None:
for i in range(len(strs)):
strs = "".join(strs)
old = [['a', 'b', 'c'], ['2', '5', '7']]
function(old)
print(old)def function(strs) -> list:
return ["".join(i) for i in strs]
old = [['a', 'b', 'c'], ['2', '5', '7']]
print(function(old)) 本帖最后由 code_noob 于 2020-9-12 12:08 编辑
new_list=[]
for i in old:
j = ''.join(i)
new_list.append(j)
new_list就是了
In : a
Out: [['1', '2', '3'], ['4', '5', '6']]
In : for index,i in enumerate(a):
...: i = ''.join(i)
...: a=i
...:
In : a
Out: ['123', '456']
这样也可以 感谢各位大神!
页:
[1]