在python中以所需格式排列列表的字符串

时间:2012-07-26 12:43:21

标签: python list

其实我正在使用scrapy解析一些html页面,我使用了xpath并从一些html标签中获取了一些地址,结果如下:

result = ["Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad","Mr Sravan"]

我想加入字符串,所以我在下面做了

final_result = ','.join(result)

并得到以下结果

final_result = "Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad,Mr Sravan"

在这里,我们可以看到final_result(地址)Mr Sravan中的名称在最后,但通常名称应该在任何联系人的任何地方的门号或门牌号之前?所以无论加入后列表中的字符串数量如何,我都应该在开始时获得名称,有没有办法做到这一点?谁能告诉我怎么做?

已编辑的代码:

目前我一次解析四个网址,所以当我使用xpath(或相关)并从不同网址获取地址时,结果(例如)如下所示每个网址

final_result = ['Addess_2','Newyork', 'Mr T.Jamal(Name)']
               ['Mr T. Jamal(Name)', 'Addres_1','Extra info'] 
               ['Addres_3','Mr T. Jamal(Name)','Extra info','Related scope'] 
               ['Addres_4','Extra info','Mr T. Jamal(Name)','Related info'] 
               ['Addres_5','Extra info','Related info']  # No name
               ['Addres_6','Extra info1','Related info1']  # No name

这里解析地址中的所有网址都想编写适用于上面所有列表的代码

3 个答案:

答案 0 :(得分:2)

你可以这样做:

final_result = ','.join(result[-1:] + result[:-1])


>>> 'Mr Sravan,Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad'

编辑: 在您发表评论后,如果您知道该名称位于排名i,则可以这样做:

result = ["is", "now", "the name", "at", "first", "place", ":)"]
i = 2
' '.join(result[i:i+1] + result[:i] + result[i+1:])
'the name is now at first place :)'

答案 1 :(得分:2)

这样的事情怎么样:

  final_result = result[-1] + ', ' + ''.join(result[:-1])

得到以下特性:

Mr Sravan, Hno 4-50, Plot No 301, Swathi Pooja Homes,Beside Rajadhani Theater, Vikas Nagar,Dilsukhnagar,Hyderabad

- 更新 -

这应该在原始列表中的任何位置找到“Mr”字符串,并使其按照您想要的方式工作。

rs = ','.join(result).split(',')
idx = [i for i,j in enumerate(rs) if j.strip().startswith('Mr')][0]
final_result = (rs[idx] +',' + ','.join(rs[1:idx] + rs[idx+1:])).strip()
print final_result

这适用于OP

更新帖子中提供的4个测试字符串

- 更新2 OP更改问题,以便“Mr”可能不在列表中 -

此代码将检测Mr是否在列表中并将其移至前面,如上所述。它还将检测它是否不在列表中并避免索引超出边界条件。见下面的输出。

测试字符串:

result = ['Addres_4','Extra info','Mr T. Jamal(Name)','Related info'] 
result = ['Addres_4','Extra info','T. Jamal(Name)','Related info'] 

代码:

rs = ','.join(result).split(',')
idx = [i for i,j in enumerate(rs) if j.strip().startswith('Mr')]

if len(idx) == 1:  # we found "Mr" in the list
    idx = idx[0]
    final_result = (rs[idx] +',' + ','.join(rs[1:idx] + rs[idx+1:])).strip()
else: # We didn't find "Mr" in the list .. adjust output to your needs
    final_result = (','.join(rs).strip())

print final_result

分别产量:

Mr T. Jamal(Name),Extra info,Related info
Addres_4,Extra info,T. Jamal(Name),Related info

答案 2 :(得分:1)

def name_first(alist):
    """Get the part of the list that begins with 'Mr', then the rest."""
    for i, el in enumerate(alist):
        if el.startswith('Mr'):
            yield alist.pop(i)
            break
    for el in alist:
        yield el