如何在列表中插入引号来分隔项目?

时间:2015-09-11 21:46:02

标签: python list

新问题:

所以我有一个清单a:

a = ["abcd"]

但我希望它是:

a = ["a","b","c","d"]

我使用的列表很长(200个术语长;不是4个)

那么我如何让Python在每个字母后加上引号(")以便它们是个别条款?因为我不想手动为我的所有列表执行此操作。

2 个答案:

答案 0 :(得分:3)

In [4]: a = ["abcd"]

In [5]: list(a[0])
Out[5]: ['a', 'b', 'c', 'd']

对于以前的版本:

In [3]: a = ["a,b,c,d"]

In [4]: a[0].split(",")
Out[4]: ['a', 'b', 'c', 'd']

答案 1 :(得分:1)

@ Akavall的回答非常好,但是如果你正在处理一个可能的空格周围的列表,如下所示:

int storeFirstNum = 0;
int storeSecondNum = 0;
int storeThirdNum = 0;
int storeFourthNum = 0;

// secondPlace start
if (firstNum != firstPlace && firstNum != fourthPlace) {
    storeFirstNum = firstNum;
}
if (secondNum != firstPlace && secondNum != fourthPlace) {
    storeSecondNum = secondNum;
}
if (thirdNum != firstPlace && thirdNum != fourthPlace) {
    storeThirdNum = thirdNum;
}
if (fourthNum != firstPlace && fourthNum != fourthPlace) {
    storeFourthNum = fourthNum;
}
if (storeFirstNum > storeSecondNum && storeFirstNum > storeThirdNum
        && storeFirstNum > storeFourthNum) {
    secondPlace = storeFirstNum;
} else if (storeSecondNum > storeFirstNum
        && storeSecondNum > storeThirdNum
        && storeSecondNum > storeFourthNum) {
    secondPlace = storeSecondNum;
} else if (storeThirdNum > storeFirstNum
        && storeThirdNum > storeSecondNum
        && storeThirdNum > storeFourthNum) {
    secondPlace = storeThirdNum;
} else if (storeFourthNum > storeFirstNum
        && storeFourthNum > storeSecondNum
        && storeFourthNum > storeThirdNum) {
    secondPlace = storeFourthNum;
}
// secondPlace end

您想要删除结果项目,如下所示:

my_list = ["a, b, c, d"]

对于这样的列表:

new_list = [x.strip() for x in my_list.split(',')]

你需要一种不同的方法。只是做:

my_list = ["abcd"]