Changing Array of elements to multiple array of elements
How do I split seperated elements in an array to new elements with separated elements within them.
The idea is to change this
[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
to this
[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]
I tried this code but it didnt work. I set my_string to the top array.
my_list = my_string.split(",")
python
|
show 2 more comments
How do I split seperated elements in an array to new elements with separated elements within them.
The idea is to change this
[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
to this
[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]
I tried this code but it didnt work. I set my_string to the top array.
my_list = my_string.split(",")
python
5
Would love to see what you've tried, and why it didn't work?
– coldspeed
Jan 2 at 6:22
please see above
– john samcock
Jan 2 at 6:26
what's the criteria to separate these?
– AkshayNevrekar
Jan 2 at 6:26
Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.
– john samcock
Jan 2 at 6:27
1
@johnsamcock Check my answer. I hope it helps.
– CodeIt
Jan 2 at 6:30
|
show 2 more comments
How do I split seperated elements in an array to new elements with separated elements within them.
The idea is to change this
[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
to this
[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]
I tried this code but it didnt work. I set my_string to the top array.
my_list = my_string.split(",")
python
How do I split seperated elements in an array to new elements with separated elements within them.
The idea is to change this
[ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
to this
[
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25"]
]
I tried this code but it didnt work. I set my_string to the top array.
my_list = my_string.split(",")
python
python
edited Jan 2 at 6:25
john samcock
asked Jan 2 at 6:21
john samcockjohn samcock
267
267
5
Would love to see what you've tried, and why it didn't work?
– coldspeed
Jan 2 at 6:22
please see above
– john samcock
Jan 2 at 6:26
what's the criteria to separate these?
– AkshayNevrekar
Jan 2 at 6:26
Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.
– john samcock
Jan 2 at 6:27
1
@johnsamcock Check my answer. I hope it helps.
– CodeIt
Jan 2 at 6:30
|
show 2 more comments
5
Would love to see what you've tried, and why it didn't work?
– coldspeed
Jan 2 at 6:22
please see above
– john samcock
Jan 2 at 6:26
what's the criteria to separate these?
– AkshayNevrekar
Jan 2 at 6:26
Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.
– john samcock
Jan 2 at 6:27
1
@johnsamcock Check my answer. I hope it helps.
– CodeIt
Jan 2 at 6:30
5
5
Would love to see what you've tried, and why it didn't work?
– coldspeed
Jan 2 at 6:22
Would love to see what you've tried, and why it didn't work?
– coldspeed
Jan 2 at 6:22
please see above
– john samcock
Jan 2 at 6:26
please see above
– john samcock
Jan 2 at 6:26
what's the criteria to separate these?
– AkshayNevrekar
Jan 2 at 6:26
what's the criteria to separate these?
– AkshayNevrekar
Jan 2 at 6:26
Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.
– john samcock
Jan 2 at 6:27
Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.
– john samcock
Jan 2 at 6:27
1
1
@johnsamcock Check my answer. I hope it helps.
– CodeIt
Jan 2 at 6:30
@johnsamcock Check my answer. I hope it helps.
– CodeIt
Jan 2 at 6:30
|
show 2 more comments
3 Answers
3
active
oldest
votes
Try this:
# initial list
mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
# empty list to store new values
array =
# loop through the list and split each value
for i in mystring:
array.append(i.split(",")) # splits into list and appends it a new list
print(array) # prints the resultant array
You can also use the below one liner list comprehension method.
mystring = [string.split(",") for string in mystring]
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
See the code in action here.
add a comment |
[i.split(',') for i in list_of_words]
output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
I think it helps!
1
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
add a comment |
Use list comprehension
with split()
l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
print([i.split(",") for i in l])
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002070%2fchanging-array-of-elements-to-multiple-array-of-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
# initial list
mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
# empty list to store new values
array =
# loop through the list and split each value
for i in mystring:
array.append(i.split(",")) # splits into list and appends it a new list
print(array) # prints the resultant array
You can also use the below one liner list comprehension method.
mystring = [string.split(",") for string in mystring]
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
See the code in action here.
add a comment |
Try this:
# initial list
mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
# empty list to store new values
array =
# loop through the list and split each value
for i in mystring:
array.append(i.split(",")) # splits into list and appends it a new list
print(array) # prints the resultant array
You can also use the below one liner list comprehension method.
mystring = [string.split(",") for string in mystring]
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
See the code in action here.
add a comment |
Try this:
# initial list
mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
# empty list to store new values
array =
# loop through the list and split each value
for i in mystring:
array.append(i.split(",")) # splits into list and appends it a new list
print(array) # prints the resultant array
You can also use the below one liner list comprehension method.
mystring = [string.split(",") for string in mystring]
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
See the code in action here.
Try this:
# initial list
mystring = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
# empty list to store new values
array =
# loop through the list and split each value
for i in mystring:
array.append(i.split(",")) # splits into list and appends it a new list
print(array) # prints the resultant array
You can also use the below one liner list comprehension method.
mystring = [string.split(",") for string in mystring]
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
See the code in action here.
edited Jan 2 at 6:35
answered Jan 2 at 6:29
CodeItCodeIt
66011020
66011020
add a comment |
add a comment |
[i.split(',') for i in list_of_words]
output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
I think it helps!
1
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
add a comment |
[i.split(',') for i in list_of_words]
output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
I think it helps!
1
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
add a comment |
[i.split(',') for i in list_of_words]
output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
I think it helps!
[i.split(',') for i in list_of_words]
output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'], ['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
I think it helps!
answered Jan 2 at 6:30
CSMaverickCSMaverick
1,5131628
1,5131628
1
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
add a comment |
1
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
1
1
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
wow ok i understand now we are just splitting based on comma and we can use pythons code
– john samcock
Jan 2 at 6:35
add a comment |
Use list comprehension
with split()
l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
print([i.split(",") for i in l])
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
add a comment |
Use list comprehension
with split()
l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
print([i.split(",") for i in l])
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
add a comment |
Use list comprehension
with split()
l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
print([i.split(",") for i in l])
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
Use list comprehension
with split()
l = [ "Tiger Nixon, System Architect, Edinburgh, 5421, 2011/04/25", "Garrett Winters, Accountant, Tokyo, 422, 2011/07/25" ]
print([i.split(",") for i in l])
Output:
[['Tiger Nixon', ' System Architect', ' Edinburgh', ' 5421', ' 2011/04/25'],
['Garrett Winters', ' Accountant', ' Tokyo', ' 422', ' 2011/07/25']]
answered Jan 2 at 6:31
AkshayNevrekarAkshayNevrekar
4,87191840
4,87191840
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002070%2fchanging-array-of-elements-to-multiple-array-of-elements%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Would love to see what you've tried, and why it didn't work?
– coldspeed
Jan 2 at 6:22
please see above
– john samcock
Jan 2 at 6:26
what's the criteria to separate these?
– AkshayNevrekar
Jan 2 at 6:26
Yeah i am trying to figure out how to separate that array of two elements which has commas in it above to a array that split them into new arrays and within those has an array of elements seperated by the commas.
– john samcock
Jan 2 at 6:27
1
@johnsamcock Check my answer. I hope it helps.
– CodeIt
Jan 2 at 6:30