小编use*_*215的帖子

Python 3.3函数将多个列表中的唯一值合并到一个列表中

我是Python新手.我正在尝试编写一个函数,将单独列表中的唯一值合并到一个列表中.我一直得到一个列表元组的结果.最后,我想从我的三个列表-a,b,c中得到一个唯一值列表.任何人都可以帮我一把吗?

def merge(*lists):
    newlist = lists[:]
    for x in lists:
        if x not in newlist:
            newlist.extend(x)
    return newlist

a = [1,2,3,4]
b = [3,4,5,6]
c = [5,6,7,8]

print(merge(a,b,c))
Run Code Online (Sandbox Code Playgroud)

我得到了一个列表元组

([1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8])
Run Code Online (Sandbox Code Playgroud)

python merge function list python-3.3

12
推荐指数
2
解决办法
1万
查看次数

Python 3.3中的re.sub

我想文本字符串从形式变化file1file01.我是python的新手,在尝试使用模式时无法弄清楚应该在'repl'位置进行什么.任何人都可以帮我一把吗?

text = 'file1 file2 file3'

x = re.sub(r'file[1-9]',r'file\0\w',text) #I'm not sure what should go in repl.
Run Code Online (Sandbox Code Playgroud)

python regex substitution python-3.x

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×2

function ×1

list ×1

merge ×1

python-3.3 ×1

python-3.x ×1

regex ×1

substitution ×1