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
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
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]0initial 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'] 9import 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"]') 2import 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'] 4import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 5import 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"]') 9initial array: ['1.1' '1.5' '2.7' '8.9'] final array: [ 1.1 1.5 2.7 8.9]0initial 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"]') 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"]') 5initial 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'] 9import 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"]') 2import 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'] 4import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 5import 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]9initial 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]5initial array: ['1.1' '1.5' '2.7' '8.9'] final array: [ 1.1 1.5 2.7 8.9]0initial 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]8initial 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'] 1initial 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"]') 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"]') 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'] 9import 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"]') 2import 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'] 4import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 5import 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]9initial 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]5initial array: ['1.1' '1.5' '2.7' '8.9'] final array: [ 1.1 1.5 2.7 8.9]0initial 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]8initial 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'] 1initial 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'] 9import 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"]') 2import 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'] 4import json obj1 = json.loads('["a", ["b", "c", "d"], "e"]') 5import 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]9initial 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]8initial 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'] 1initial 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]