Hướng dẫn string array to array python - mảng chuỗi thành mảng python

14

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi đang cố gắng chuyển đổi một mảng mà tôi đã lưu trữ trong cơ sở dữ liệu MySQL (dưới dạng chuỗi) thành một mảng tiêu chuẩn trong Python một ví dụ về ý tôi là:

Đây là những gì tôi nhận được từ cơ sở dữ liệu:

"['a',['b','c','d'],'e']" # this is a string in the format of an array that holds strings inside it.

Tôi cần loại bỏ mảng khỏi chuỗi để nó hoạt động giống như một mảng bình thường, bất kỳ trợ giúp nào cũng được đánh giá rất cao. Tôi không chắc liệu điều này có được trả lời ở nơi khác không, xin lỗi nếu tôi không thể tìm thấy nó. Cảm ơn

Khi được hỏi ngày 29 tháng 8 năm 2014 lúc 16:20Aug 29, 2014 at 16:20

Hướng dẫn string array to array python - mảng chuỗi thành mảng python

0

Bạn có thể sử dụng ast.literal_eval

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']

Đã trả lời ngày 29 tháng 8 năm 2014 lúc 16:21Aug 29, 2014 at 16:21

Hướng dẫn string array to array python - mảng chuỗi thành mảng python

6

Nếu bạn có thể chuyển đổi các trích dẫn đơn đó thành trích dẫn gấp đôi, bạn có thể sử dụng phân tích cú pháp JSON.

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')

Đã trả lời ngày 29 tháng 8 năm 2014 lúc 16:30Aug 29, 2014 at 16:30

Shashank Agarwalshashank AgarwalShashank Agarwal

2.6811 Huy hiệu vàng23 Huy hiệu bạc24 Huy hiệu đồng1 gold badge23 silver badges24 bronze badges

3

numpy as np8

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
00
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
39
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
0
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
1

Example:

initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]

Làm cách nào để chuyển đổi một chuỗi thành một mảng mảng?astype 

Có bốn cách để chuyển đổi một chuỗi thành mảng chuỗi trong java:.a  column could be imported as a string but to do operations we have to convert it into a float, astype() is used to do such data type conversions.

Python3

import numpy as np

____10

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
2
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
5
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
7
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
9
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
0

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
2
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
5
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
6

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
7
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
9
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
0
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
1

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
2
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
5
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
7

Output:  

initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]

Chuyển đổi mảng chuỗi thành mảng phao bằng NP.FromString & nbsp;np.fromstring 

Hàm Numpy.FromString () tạo ra một mảng một chiều mới được khởi tạo từ dữ liệu văn bản trong một chuỗi.

Python3

import numpy as np

____10

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
2
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
5
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
7
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
9
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
0

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
2
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
5
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
6

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
0____11
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
0

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
0____11
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
5
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
0
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
7

initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
8
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
1

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
2
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
5
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
6

Output:  

initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]

Chuyển đổi mảng chuỗi thành mảng phao sử dụng np.asarray () và type & nbsp;np.asarray() and type 

Hàm numpy.asarray () được sử dụng khi chúng tôi muốn chuyển đổi đầu vào thành một mảng. Đầu vào có thể là danh sách, danh sách các bộ dữ liệu, bộ dữ liệu, bộ dữ liệu của bộ dữ liệu, bộ dữ liệu của danh sách và mảng.

Python3

import numpy as np

____10

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
2
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
5
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
7
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
9
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
0

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
2
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
5
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
6

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
0____11
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
0

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
0____11
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
5
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
0
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
7

initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
8
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
1

Output:  

initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]

import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 1 import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 2initial array: ['1.1' '1.5' '2.7' '8.9'] final array: [ 1.1 1.5 2.7 8.9]4>>> from ast import literal_eval >>> s = "['a',['b','c','d'],'e']" >>> print(literal_eval(s)) ['a', ['b', 'c', 'd'], 'e'] 4import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 5import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 6np.asfarray

Chuyển đổi mảng chuỗi thành mảng phao sử dụng np.asarray () và type & nbsp; numpy.asfarray() function is used when we want to convert input to a float type array. Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.

Python3

import numpy as np

____10

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
2
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
5
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
7
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
9
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
0

import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
1
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
2
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
3
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
4
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
5
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
6

>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
0____11
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
0

initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
8
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
1
initial array ['1.1' '1.5' '2.7' '8.9']
final array [ 1.1  1.5  2.7  8.9]
9
initial array: ['1.1' '1.5' '2.7' '8.9']
final array: [ 1.1  1.5  2.7  8.9]
1

Output:  

initial array ['1.1' '1.5' '2.7' '8.9']
final array [1.1 1.5 2.7 8.9]

Làm cách nào để chuyển đổi một chuỗi thành một mảng mảng?

Có bốn cách để chuyển đổi một chuỗi thành mảng chuỗi trong java:..
Sử dụng chuỗi. Chia () Phương thức ..
Sử dụng mẫu. Chia () Phương thức ..
Sử dụng chuỗi [] tiếp cận ..
Sử dụng phương thức toarray () ..

Làm thế nào để bạn chuyển đổi một mảng chuỗi thành một danh sách trong Python?

Một cách khác để chuyển đổi một chuỗi thành danh sách là bằng cách sử dụng phương thức python chia (). Phương thức chia () chia một chuỗi vào một danh sách, trong đó mỗi mục danh sách là mỗi từ tạo nên chuỗi. Mỗi từ sẽ là một mục danh sách cá nhân.using the split() Python method. The split() method splits a string into a list, where each list item is each word that makes up the string. Each word will be an individual list item.

Bạn có thể chuyển đổi một chuỗi thành một mảng không?

Chúng ta có thể chuyển đổi một chuỗi thành một mảng ký tự bằng cách sử dụng một vòng lặp cho vòng lặp hoặc phương thức tochararray ().Để chuyển đổi một chuỗi thành một mảng chuỗi dựa trên dấu phân cách, chúng ta có thể sử dụng chuỗi.chia () hoặc mẫu.. To convert a string to a string array based on a delimeter, we can use String. split() or Pattern.

Làm cách nào để chuyển đổi một mảng chuỗi thành một chuỗi trong Python?

Bạn có thể kết hợp một danh sách các chuỗi vào một chuỗi với phương thức chuỗi, hãy nối ().Gọi phương thức tham gia () từ 'chuỗi vào chèn' và truyền [danh sách các chuỗi].Nếu bạn sử dụng một chuỗi trống '', [Danh sách các chuỗi] chỉ đơn giản là được nối và nếu bạn sử dụng dấu phẩy, thì nó sẽ tạo ra một chuỗi được phân phối bằng dấu phẩy.with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.