'364|68|1B0E04,377|122|1D1006' 分割成('364', '68', '1B0E04),('364', '68', '1B0E04)
时间: 2023-08-16 19:09:42 浏览: 31
可以使用以下代码将 '364|68|1B0E04,377|122|1D1006' 分割成 ('364', '68', '1B0E04),('377', '122', '1D1006'):
```python
str_val = '364|68|1B0E04,377|122|1D1006'
tuple_list = []
for item in str_val.split(','):
tuple_list.append(tuple(item.split('|')))
print(tuple_list)
```
输出结果为:
```
[('364', '68', '1B0E04'), ('377', '122', '1D1006')]
```
其中,使用了 split 方法将字符串以逗号为分隔符分割成多个子字符串,并使用循环遍历每个子字符串。将每个子字符串再使用 split 方法以竖线为分隔符分割成多个子字符串,并使用 tuple 方法将多个子字符串合并成一个元组。将每个元组添加到一个列表中,并输出该列表。
相关问题
python 分割 364|68|1B0E04,377|122|1D1006
可以使用 Python 的字符串分割方法 `split()` 来实现:
```python
coordinates = '364|68|1B0E04,377|122|1D1006'
coords_list = coordinates.split(',')
print(coords_list)
```
输出:
```
['364|68|1B0E04', '377|122|1D1006']
```
然后再对每个坐标字符串进行二次分割,得到各个坐标值:
```python
for coords in coords_list:
x, y, z = coords.split('|')
print('x:', x, 'y:', y, 'z:', z)
```
输出:
```
x: 364 y: 68 z: 1B0E04
x: 377 y: 122 z: 1D1006
```
'开始',"364|68|1B0E04,377|122|1D1006",0.9 分割开 分别赋值
好的,我的理解是你想将字符串 "364|68|1B0E04,377|122|1D1006" 分割开来,并将分割后的子串分别赋值给不同的变量,是这样吗?
如果是的话,可以使用 Python 中的字符串分割函数 `split()`,像这样:
```python
s = "364|68|1B0E04,377|122|1D1006"
s1, s2 = s.split(",")
x1, y1, c1 = s1.split("|")
x2, y2, c2 = s2.split("|")
```
这段代码会将字符串 s 分割成两个子串 s1 和 s2,分别赋值为 "364|68|1B0E04" 和 "377|122|1D1006"。然后,我们再使用 `split()` 函数将 s1 和 s2 分别按照 "|" 字符进行分割,得到 x1、y1、c1、x2、y2 和 c2 这六个变量。
其中,x1 和 y1 分别表示第一个坐标的 x 和 y 坐标,c1 表示该位置的颜色值;x2、y2 和 c2 则分别表示第二个坐标的 x 和 y 坐标,以及该位置的颜色值。