|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> list.extend('just')
>>> list
[12, 23, 43, 65, 'j', 'u', 's', 't'] 为什么结果是一个一个字母分开来的字符串,不是完整的‘just’
帮助的意思是extend是将可迭代对象增加到列表中,这里的可迭代对象可以是列表、元组、集合、字典、字符串等。如果是字符串会将每个字符分隔开来一一加入。如果想把字符串作为整体加入,可参加下面的例子:
- >>> list1 = [12, 23, 43, 65]
- >>> list1.extend(['just'])
- >>> list1
- [12, 23, 43, 65, 'just']
- >>> list1.extend(('just',))
- >>> list1
- [12, 23, 43, 65, 'just', 'just']
- >>> list1.extend({'just'})
- >>> list1
- [12, 23, 43, 65, 'just', 'just', 'just']
- >>> list1.extend({'just':1})
- >>> list1
- [12, 23, 43, 65, 'just', 'just', 'just', 'just']
复制代码
|
|