python列表遍历索引_python – 通过循环中的索引遍历列表列表,以重新格式化字符串...

跟踪我们何时找到一个新客户:

from pprint import pprint as pp

out = []

for sub in DF:

if sub[0].startswith("Customer Number"):

cust = sub[0]

else:

out.append(cust + sub[0])

pp(out)

输出:

['Customer Number: 001 Notes: Bought a ton of stuff and was easy to deal with',

'Customer Number: 666 Notes: acted and looked like Chris Farley on that '

'hidden decaf skit from SNL',

'Customer Number: 103 Notes: bought a ton of stuff got a free keychain',

'Customer Number: 103 Notes: gave us a referral to his uncles cousins '

'hairdresser',

'Customer Number: 103 Notes: name address birthday social security number '

'on file',

'Customer Number: 007 Notes: looked a lot like James Bond',

'Customer Number: 007 Notes: came in with a martini']

如果客户可以稍后再重复一次,并且希望将它们组合在一起使用dict:

from collections import defaultdict

d = defaultdict(list)

for sub in DF:

if sub[0].startswith("Customer Number"):

cust = sub[0]

else:

d[cust].append(cust + sub[0])

print(d)

输出:

pp(d)

{'Customer Number: 001 ': ['Customer Number: 001 Notes: Bought a ton of '

'stuff and was easy to deal with'],

'Customer Number: 007 ': ['Customer Number: 007 Notes: looked a lot like '

'James Bond',

'Customer Number: 007 Notes: came in with a '

'martini'],

'Customer Number: 103 ': ['Customer Number: 103 Notes: bought a ton of '

'stuff got a free keychain',

'Customer Number: 103 Notes: gave us a referral '

'to his uncles cousins hairdresser',

'Customer Number: 103 Notes: name address '

'birthday social security number on file'],

'Customer Number: 666 ': ['Customer Number: 666 Notes: acted and looked '

'like Chris Farley on that hidden decaf skit '

'from SNL']}

根据您的评论和错误,您似乎有一行来自实际的客户,所以我们可以将它们添加到列表中的第一个客户:

# added ["foo"] before we see any customer

DF = [["foo"],['Customer Number: 001 '],

['Notes: Bought a ton of stuff and was easy to deal with'],

['Customer Number: 666 '],

['Notes: acted and looked like Chris Farley on that hidden decaf skit from SNL'],

['Customer Number: 103 '],

['Notes: bought a ton of stuff got a free keychain'],

['Notes: gave us a referral to his uncles cousins hairdresser'],

['Notes: name address birthday social security number on file'],

['Customer Number: 007 '],

['Notes: looked a lot like James Bond'],

['Notes: came in with a martini']]

from pprint import pprint as pp

from itertools import takewhile, islice

# find lines up to first customer

start = list(takewhile(lambda x: "Customer Number:" not in x[0], DF))

out = []

ln = len(start)

# if we had data before we actually found a customer this will be True

if start:

# so set cust to first customer in list and start adding to out

cust = DF[ln][0]

for sub in start:

out.append(cust + sub[0])

# ln will either be 0 if start is empty else we start at first customer

for sub in islice(DF, ln, None):

if sub[0].startswith("Customer Number"):

cust = sub[0]

else:

out.append(cust + sub[0])

哪些输出:

['Customer Number: 001 foo',

'Customer Number: 001 Notes: Bought a ton of stuff and was easy to deal with',

'Customer Number: 666 Notes: acted and looked like Chris Farley on that '

'hidden decaf skit from SNL',

'Customer Number: 103 Notes: bought a ton of stuff got a free keychain',

'Customer Number: 103 Notes: gave us a referral to his uncles cousins '

'hairdresser',

'Customer Number: 103 Notes: name address birthday social security number '

'on file',

'Customer Number: 007 Notes: looked a lot like James Bond',

'Customer Number: 007 Notes: came in with a martini']

我推测你会考虑任何客户之前的线条实际上属于那个第一个客户。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值