分割线Python字符串

2024-09-27 07:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个元素列表,其文本如下所示: aSampleElementText = "Vraj Shroff\nIndia"我现在想要两个列表,第一个列表的元素是“Vraj Shroff”,第二个列表的元素是“India”。你知道吗

我看了其他关于分割线和分割线的帖子。然而,我下面的代码并没有给我预期的结果。你知道吗

输出:

"V", 
"r"

所需输出:

"Vraj Shroff",
"India"

我的代码:

personalName = "Something" #first list
personalTitle = "Something" #second list
for i in range(len(names)-1) 
    #names is a list of elements (example above)
    #it is len - 1 becuase I don't want to do this to the first element of the list
    i += 1
    temp = names[i].text
    temp.splitlines()
    personName.append(temp[0])
    personTitle.append(temp[1])

Tags: of代码元素列表lennamesistemp
2条回答
names = []
locations = []
a = ["Vraj Shroff\nIndia", "Vraj\nIndia", "Shroff\nxyz", "abd cvd\nUS"]

for i in a:
    b = i.splitlines()
    names.append(b[0])
    locations.append(b[1])

print(names)
print(locations)

输出:

['Vraj Shroff', 'Vraj', 'Shroff', 'abd cvd']
['India', 'India', 'xyz', 'US']

这就是你要找的吗?你知道吗

名称是一个字符串。names[I]是与字符串中的索引相对应的字符。因此你得到了这种输出。你知道吗

像这样做

x = names.splitlines()

x将是包含元素的列表。你知道吗

相关问题 更多 >

    热门问题