How to slice a list containing a set of strings by comma?
I have a set of strings inside a list that I read from a CSV file and it looks like this:
myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...
I want to store it into a database (Django) that has three fields:
fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...
What should I do?
python python-3.x
add a comment |
I have a set of strings inside a list that I read from a CSV file and it looks like this:
myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...
I want to store it into a database (Django) that has three fields:
fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...
What should I do?
python python-3.x
2
ArefieldOne
,fieldTwo
etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.
– slider
Nov 20 '18 at 3:47
add a comment |
I have a set of strings inside a list that I read from a CSV file and it looks like this:
myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...
I want to store it into a database (Django) that has three fields:
fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...
What should I do?
python python-3.x
I have a set of strings inside a list that I read from a CSV file and it looks like this:
myList = ('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106'),...
I want to store it into a database (Django) that has three fields:
fieldOne = '17.0.1.34', '203.84.210.248','202.73.40.45', ....
fieldTwo = '17.1.182.21', '27.111.228.3', '27.111.228.6', ...
fieldThree = '714', '10310', '18106',...
What should I do?
python python-3.x
python python-3.x
edited Nov 20 '18 at 3:46
Mihai Chelaru
2,180101022
2,180101022
asked Nov 20 '18 at 3:44
Banthita LimwilaiBanthita Limwilai
213
213
2
ArefieldOne
,fieldTwo
etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.
– slider
Nov 20 '18 at 3:47
add a comment |
2
ArefieldOne
,fieldTwo
etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.
– slider
Nov 20 '18 at 3:47
2
2
Are
fieldOne
, fieldTwo
etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.– slider
Nov 20 '18 at 3:47
Are
fieldOne
, fieldTwo
etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.– slider
Nov 20 '18 at 3:47
add a comment |
4 Answers
4
active
oldest
votes
You have a list of 3-tuples:
myList = [
(a1, b1, c1),
(a2, b2, c2),
(a3, b3, c3),
...
]
You want to refactor this list into three variables:
fieldOne = [a1, a2, a3, ...]
fieldTwo = [b1, b2, b3, ...]
fieldThree = [c1, c2, c3, ...]
You can do this using list comprehension: "Make a list by taking the k
th value from each element of myList
". That would look like this:
fieldOne = [i[0] for i in myList] # k = 0
fieldTwo = [i[1] for i in myList] # k = 1
fieldThree = [i[2] for i in myList] # k = 2
Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.
add a comment |
You could also zip()
your results and store them in a dictionary:
>>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
>>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
>>> dict(zip(fields, zip(*myList)))
{'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}
Then you can just reference the 'fieldOne'
, 'fieldTwo'
, 'fieldThree'
keys from this dictionary and store it in your database.
add a comment |
I would suggest using the following way so you can use it multiple times.
def create_field(myList):
fieldOne = [myList[i][0] for i in range(len(myList))]
fieldTwo = [myList[i][1] for i in range(len(myList))]
fieldThree = [myList[i][2] for i in range(len(myList))]
return fieldOne, fieldTwo, fieldThree
create_field(myList)
add a comment |
Given your data, here is a possible solution:
# Initialize each field list
fieldOne =
fieldTwo =
fieldThree =
# fetch each **item** in **myList**, we extract the component corresponding
# append it to the appropriate field
for item in myList:
fieldOne.append(item[0])
fieldTwo.append(item[1])
fieldThree.append(item[2])
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%2f53385902%2fhow-to-slice-a-list-containing-a-set-of-strings-by-comma%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have a list of 3-tuples:
myList = [
(a1, b1, c1),
(a2, b2, c2),
(a3, b3, c3),
...
]
You want to refactor this list into three variables:
fieldOne = [a1, a2, a3, ...]
fieldTwo = [b1, b2, b3, ...]
fieldThree = [c1, c2, c3, ...]
You can do this using list comprehension: "Make a list by taking the k
th value from each element of myList
". That would look like this:
fieldOne = [i[0] for i in myList] # k = 0
fieldTwo = [i[1] for i in myList] # k = 1
fieldThree = [i[2] for i in myList] # k = 2
Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.
add a comment |
You have a list of 3-tuples:
myList = [
(a1, b1, c1),
(a2, b2, c2),
(a3, b3, c3),
...
]
You want to refactor this list into three variables:
fieldOne = [a1, a2, a3, ...]
fieldTwo = [b1, b2, b3, ...]
fieldThree = [c1, c2, c3, ...]
You can do this using list comprehension: "Make a list by taking the k
th value from each element of myList
". That would look like this:
fieldOne = [i[0] for i in myList] # k = 0
fieldTwo = [i[1] for i in myList] # k = 1
fieldThree = [i[2] for i in myList] # k = 2
Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.
add a comment |
You have a list of 3-tuples:
myList = [
(a1, b1, c1),
(a2, b2, c2),
(a3, b3, c3),
...
]
You want to refactor this list into three variables:
fieldOne = [a1, a2, a3, ...]
fieldTwo = [b1, b2, b3, ...]
fieldThree = [c1, c2, c3, ...]
You can do this using list comprehension: "Make a list by taking the k
th value from each element of myList
". That would look like this:
fieldOne = [i[0] for i in myList] # k = 0
fieldTwo = [i[1] for i in myList] # k = 1
fieldThree = [i[2] for i in myList] # k = 2
Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.
You have a list of 3-tuples:
myList = [
(a1, b1, c1),
(a2, b2, c2),
(a3, b3, c3),
...
]
You want to refactor this list into three variables:
fieldOne = [a1, a2, a3, ...]
fieldTwo = [b1, b2, b3, ...]
fieldThree = [c1, c2, c3, ...]
You can do this using list comprehension: "Make a list by taking the k
th value from each element of myList
". That would look like this:
fieldOne = [i[0] for i in myList] # k = 0
fieldTwo = [i[1] for i in myList] # k = 1
fieldThree = [i[2] for i in myList] # k = 2
Of course, if your tuples are of variable length this gets much more complicated - but these are the fundamentals, and you can play around with them as needed to solve your problem.
answered Nov 20 '18 at 3:49
Green Cloak GuyGreen Cloak Guy
2,5131720
2,5131720
add a comment |
add a comment |
You could also zip()
your results and store them in a dictionary:
>>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
>>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
>>> dict(zip(fields, zip(*myList)))
{'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}
Then you can just reference the 'fieldOne'
, 'fieldTwo'
, 'fieldThree'
keys from this dictionary and store it in your database.
add a comment |
You could also zip()
your results and store them in a dictionary:
>>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
>>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
>>> dict(zip(fields, zip(*myList)))
{'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}
Then you can just reference the 'fieldOne'
, 'fieldTwo'
, 'fieldThree'
keys from this dictionary and store it in your database.
add a comment |
You could also zip()
your results and store them in a dictionary:
>>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
>>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
>>> dict(zip(fields, zip(*myList)))
{'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}
Then you can just reference the 'fieldOne'
, 'fieldTwo'
, 'fieldThree'
keys from this dictionary and store it in your database.
You could also zip()
your results and store them in a dictionary:
>>> myList = [('17.0.1.34', '17.1.182.21', '714'),('203.84.210.248', '27.111.228.3', '10310'),('202.73.40.45', '27.111.228.6', '18106')]
>>> fields = 'fieldOne', 'fieldTwo', 'fieldThree'
>>> dict(zip(fields, zip(*myList)))
{'fieldOne': ('17.0.1.34', '203.84.210.248', '202.73.40.45'), 'fieldTwo': ('17.1.182.21', '27.111.228.3', '27.111.228.6'), 'fieldThree': ('714', '10310', '18106')}
Then you can just reference the 'fieldOne'
, 'fieldTwo'
, 'fieldThree'
keys from this dictionary and store it in your database.
answered Nov 20 '18 at 3:57
RoadRunnerRoadRunner
11.1k31340
11.1k31340
add a comment |
add a comment |
I would suggest using the following way so you can use it multiple times.
def create_field(myList):
fieldOne = [myList[i][0] for i in range(len(myList))]
fieldTwo = [myList[i][1] for i in range(len(myList))]
fieldThree = [myList[i][2] for i in range(len(myList))]
return fieldOne, fieldTwo, fieldThree
create_field(myList)
add a comment |
I would suggest using the following way so you can use it multiple times.
def create_field(myList):
fieldOne = [myList[i][0] for i in range(len(myList))]
fieldTwo = [myList[i][1] for i in range(len(myList))]
fieldThree = [myList[i][2] for i in range(len(myList))]
return fieldOne, fieldTwo, fieldThree
create_field(myList)
add a comment |
I would suggest using the following way so you can use it multiple times.
def create_field(myList):
fieldOne = [myList[i][0] for i in range(len(myList))]
fieldTwo = [myList[i][1] for i in range(len(myList))]
fieldThree = [myList[i][2] for i in range(len(myList))]
return fieldOne, fieldTwo, fieldThree
create_field(myList)
I would suggest using the following way so you can use it multiple times.
def create_field(myList):
fieldOne = [myList[i][0] for i in range(len(myList))]
fieldTwo = [myList[i][1] for i in range(len(myList))]
fieldThree = [myList[i][2] for i in range(len(myList))]
return fieldOne, fieldTwo, fieldThree
create_field(myList)
answered Nov 20 '18 at 4:05
hemantahemanta
178
178
add a comment |
add a comment |
Given your data, here is a possible solution:
# Initialize each field list
fieldOne =
fieldTwo =
fieldThree =
# fetch each **item** in **myList**, we extract the component corresponding
# append it to the appropriate field
for item in myList:
fieldOne.append(item[0])
fieldTwo.append(item[1])
fieldThree.append(item[2])
add a comment |
Given your data, here is a possible solution:
# Initialize each field list
fieldOne =
fieldTwo =
fieldThree =
# fetch each **item** in **myList**, we extract the component corresponding
# append it to the appropriate field
for item in myList:
fieldOne.append(item[0])
fieldTwo.append(item[1])
fieldThree.append(item[2])
add a comment |
Given your data, here is a possible solution:
# Initialize each field list
fieldOne =
fieldTwo =
fieldThree =
# fetch each **item** in **myList**, we extract the component corresponding
# append it to the appropriate field
for item in myList:
fieldOne.append(item[0])
fieldTwo.append(item[1])
fieldThree.append(item[2])
Given your data, here is a possible solution:
# Initialize each field list
fieldOne =
fieldTwo =
fieldThree =
# fetch each **item** in **myList**, we extract the component corresponding
# append it to the appropriate field
for item in myList:
fieldOne.append(item[0])
fieldTwo.append(item[1])
fieldThree.append(item[2])
edited Nov 20 '18 at 4:25
answered Nov 20 '18 at 4:00
eapetchoeapetcho
42927
42927
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%2f53385902%2fhow-to-slice-a-list-containing-a-set-of-strings-by-comma%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
2
Are
fieldOne
,fieldTwo
etc. lists of strings or strings separated by a comma? If they're lists, please surround them with square brackets to make it clearer.– slider
Nov 20 '18 at 3:47