python中的连接函数

字符串连接

  1. “+”连接
str_name1 = 'To'
str_name2 = 'ny'
str_name = str_name1 + str_name2
print(str_name)

运行结果:Tony

  1. join连接
    这个函数接受一个列表或元组,然后用字符串依次连接列表中每一个元素。
list1 = ['P', 'y', 't', 'h', 'o', 'n']
print("".join(list1))

运行结果:Python

str_name1 = 'To'
str_name2 = 'ny'
str_name = str_name1.join(str_name2)
print(str_name)

需要注意,运行结果为:nToy

  1. “,”连接
a, b = 'Hello', 'word'
c = a, b
print(a, b)
print(c)

运行结果:
Hello word
(‘Hello’, ‘word’)

  1. format连接
str_word_keyword = 'hellow, world!{a},{b}'.format(b='张三', a='李四')
print(str_word_keyword)

运行结果:
hellow, world!李四,张三

numpy数组连接

  1. np.append
import numpy as np
a = np.array([[1, 2],[3, 4],[5, 6]])
b = np.array([[11, 22],[33, 44],[55, 66]])
c = np.append(a,b)
print(c)

运行结果:[ 1 2 3 4 5 6 11 22 33 44 55 66]

  1. np.concatenate
import numpy as np
a = np.array([[1, 2],[3, 4],[5, 6]])
b = np.array([[11, 22],[33, 44],[55, 66]])
c = np.concatenate((a,b),axis=0)  
print(c)
d = np.concatenate((a,b),axis=1)  # 同 e = np.concatenate([a,b],axis=1)
print(d)

运行结果:
[[ 1 2]
[ 3 4]
[ 5 6]
[11 22]
[33 44]
[55 66]]

[[ 1 2 11 22]
[ 3 4 33 44]
[ 5 6 55 66]]

  1. np.hstack 和 np.vstack
import numpy as np
a = np.array([[1, 2],[3, 4],[5, 6]])
b = np.array([[11, 22],[33, 44],[55, 66]])
# 行连接:等价于 np.concatenate((a,b),axis = 1)
c = np.hstack((a,b))
print(c)
# 列连接:等价于 np.concatenate((a,b),axis = 0)
d = np.vstack((a,b))
print(d)

运行结果:
[[ 1 2 11 22]
[ 3 4 33 44]
[ 5 6 55 66]]

[[ 1 2]
[ 3 4]
[ 5 6]
[11 22]
[33 44]
[55 66]]

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值