Compiling a nested list with conditional statement
I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:
[[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5),
("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]
The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.
[[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5),
("Dresses",15)]]
The user input is a list with the preferences in order. For example:
preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
or
preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)
This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:
empty_list=[ for x in range (len(preferences)]
This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:
if preference[0]=="Fruits":
empty_list[0]=Fruits
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Pets
elif preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Clothes
if preference[0]=="Pets":
empty_list[0]=Pets
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Clothes
if preference[0]=="Clothes":
empty_list[0]=Clothes
if preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Pets
My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?
Any tip or guidance is most appreciated.
python
add a comment |
I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:
[[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5),
("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]
The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.
[[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5),
("Dresses",15)]]
The user input is a list with the preferences in order. For example:
preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
or
preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)
This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:
empty_list=[ for x in range (len(preferences)]
This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:
if preference[0]=="Fruits":
empty_list[0]=Fruits
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Pets
elif preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Clothes
if preference[0]=="Pets":
empty_list[0]=Pets
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Clothes
if preference[0]=="Clothes":
empty_list[0]=Clothes
if preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Pets
My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?
Any tip or guidance is most appreciated.
python
add a comment |
I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:
[[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5),
("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]
The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.
[[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5),
("Dresses",15)]]
The user input is a list with the preferences in order. For example:
preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
or
preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)
This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:
empty_list=[ for x in range (len(preferences)]
This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:
if preference[0]=="Fruits":
empty_list[0]=Fruits
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Pets
elif preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Clothes
if preference[0]=="Pets":
empty_list[0]=Pets
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Clothes
if preference[0]=="Clothes":
empty_list[0]=Clothes
if preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Pets
My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?
Any tip or guidance is most appreciated.
python
I want to create a nested list with one or more of the below lists of tuples, with the order of the list based on user's preference.
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
The order of the nested list will depend on the preference of the user.For instance, if the user prefer pets over clothes over fruits. The list will look like this:
[[("Jeans",10),("Shirts",5),("Dresses",15)],[("Jeans",10),("Shirts",5),
("Dresses",15)],[("Apples",2),("Oranges",3),("Pineapples",5)]]
The user also have the option of picking only one or two items. For instance, if the user only cares about pet and then clothing (doesn't care about fruits), his/her list will look like this.
[[("Dogs",3),("Cats",4),("Turtles",2)],[("Jeans",10),("Shirts",5),
("Dresses",15)]]
The user input is a list with the preferences in order. For example:
preference= ["Pets", "Fruits", "Clothing"] # preference list for users who care about pets over fruits over clothing.
or
preference= ["Fruits", "Clothing"] # preference list for users who care about fruits over clothing (no regard for pets)
This is how I've tried to tackle the problem. First I create an empty list with a corresponding number of nested list:
empty_list=[ for x in range (len(preferences)]
This creates a place holder for the number of nested list I need. I then run a bunch of conditional statement to pop in one list at the time:
if preference[0]=="Fruits":
empty_list[0]=Fruits
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Pets
elif preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Clothes
if preference[0]=="Pets":
empty_list[0]=Pets
if preference[1]=="Clothes":
empty_list[1]=Clothes
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Clothes
if preference[0]=="Clothes":
empty_list[0]=Clothes
if preference[1]=="Pets":
empty_list[1]=Pets
empty_list[2]=Fruits
elif preference[1]=="Fruits":
empty_list[1]=Fruits
empty_list[2]=Pets
My solution is inefficient and also causes problem with list assignment out of range if there are only two preference as opposed to three. Is there a more Pythonic way of writing this?
Any tip or guidance is most appreciated.
python
python
asked Nov 13 '18 at 23:57
sangurocactussangurocactus
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
you should store your data in a dictionary
and then access that dictionary by key
data = dict(
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
)
once you have it in a dict you can easily access the values with variables
fruits = "Fruits"
my_fruits = data[fruits]
you can then use a simple list comprehension to capture all of your interests
interests = ["Fruits","Pets"]
interesting_things = [data[interest] for interest in interests]
interests = ["Fruits","Clothes"]
interesting_things = [data[interest] for interest in interests]
add a comment |
Try this method.
def prefer(preference):
preference_list =
choices =
"fruits": [
("Apples", 2),
("Oranges", 3),
("Pineapples", 5)
],
"cloths": [
("Jeans", 10),
("Shirts", 5),
("Dresses", 15)
],
"pets": [
("Dogs", 3),
("Cats", 4),
("Turtles", 2)
]
for choice in choices:
if choice in preference:
preference_list.insert(preference.index(choice), choices[choice])
else:
preference_list.append(choices[choice])
print(preference_list)
prefer(("fruits", "cloths"))
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
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%2f53291213%2fcompiling-a-nested-list-with-conditional-statement%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you should store your data in a dictionary
and then access that dictionary by key
data = dict(
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
)
once you have it in a dict you can easily access the values with variables
fruits = "Fruits"
my_fruits = data[fruits]
you can then use a simple list comprehension to capture all of your interests
interests = ["Fruits","Pets"]
interesting_things = [data[interest] for interest in interests]
interests = ["Fruits","Clothes"]
interesting_things = [data[interest] for interest in interests]
add a comment |
you should store your data in a dictionary
and then access that dictionary by key
data = dict(
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
)
once you have it in a dict you can easily access the values with variables
fruits = "Fruits"
my_fruits = data[fruits]
you can then use a simple list comprehension to capture all of your interests
interests = ["Fruits","Pets"]
interesting_things = [data[interest] for interest in interests]
interests = ["Fruits","Clothes"]
interesting_things = [data[interest] for interest in interests]
add a comment |
you should store your data in a dictionary
and then access that dictionary by key
data = dict(
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
)
once you have it in a dict you can easily access the values with variables
fruits = "Fruits"
my_fruits = data[fruits]
you can then use a simple list comprehension to capture all of your interests
interests = ["Fruits","Pets"]
interesting_things = [data[interest] for interest in interests]
interests = ["Fruits","Clothes"]
interesting_things = [data[interest] for interest in interests]
you should store your data in a dictionary
and then access that dictionary by key
data = dict(
Fruits=[("Apples",2),("Oranges",3),("Pineapples",5)]
Clothes=[("Jeans",10),("Shirts",5),("Dresses",15)]
Pets=[("Dogs",3),("Cats",4),("Turtles",2)]
)
once you have it in a dict you can easily access the values with variables
fruits = "Fruits"
my_fruits = data[fruits]
you can then use a simple list comprehension to capture all of your interests
interests = ["Fruits","Pets"]
interesting_things = [data[interest] for interest in interests]
interests = ["Fruits","Clothes"]
interesting_things = [data[interest] for interest in interests]
edited Nov 14 '18 at 0:24
answered Nov 14 '18 at 0:13
Joran BeasleyJoran Beasley
73k678118
73k678118
add a comment |
add a comment |
Try this method.
def prefer(preference):
preference_list =
choices =
"fruits": [
("Apples", 2),
("Oranges", 3),
("Pineapples", 5)
],
"cloths": [
("Jeans", 10),
("Shirts", 5),
("Dresses", 15)
],
"pets": [
("Dogs", 3),
("Cats", 4),
("Turtles", 2)
]
for choice in choices:
if choice in preference:
preference_list.insert(preference.index(choice), choices[choice])
else:
preference_list.append(choices[choice])
print(preference_list)
prefer(("fruits", "cloths"))
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
add a comment |
Try this method.
def prefer(preference):
preference_list =
choices =
"fruits": [
("Apples", 2),
("Oranges", 3),
("Pineapples", 5)
],
"cloths": [
("Jeans", 10),
("Shirts", 5),
("Dresses", 15)
],
"pets": [
("Dogs", 3),
("Cats", 4),
("Turtles", 2)
]
for choice in choices:
if choice in preference:
preference_list.insert(preference.index(choice), choices[choice])
else:
preference_list.append(choices[choice])
print(preference_list)
prefer(("fruits", "cloths"))
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
add a comment |
Try this method.
def prefer(preference):
preference_list =
choices =
"fruits": [
("Apples", 2),
("Oranges", 3),
("Pineapples", 5)
],
"cloths": [
("Jeans", 10),
("Shirts", 5),
("Dresses", 15)
],
"pets": [
("Dogs", 3),
("Cats", 4),
("Turtles", 2)
]
for choice in choices:
if choice in preference:
preference_list.insert(preference.index(choice), choices[choice])
else:
preference_list.append(choices[choice])
print(preference_list)
prefer(("fruits", "cloths"))
Try this method.
def prefer(preference):
preference_list =
choices =
"fruits": [
("Apples", 2),
("Oranges", 3),
("Pineapples", 5)
],
"cloths": [
("Jeans", 10),
("Shirts", 5),
("Dresses", 15)
],
"pets": [
("Dogs", 3),
("Cats", 4),
("Turtles", 2)
]
for choice in choices:
if choice in preference:
preference_list.insert(preference.index(choice), choices[choice])
else:
preference_list.append(choices[choice])
print(preference_list)
prefer(("fruits", "cloths"))
answered Nov 14 '18 at 0:22
BaryonBaryon
365
365
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
add a comment |
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
This works great. Thank you! The only change I had to make was getting rid of the preference_list.append (choices[choice]) as the number of preference list can be fewer than 3.
– sangurocactus
Nov 14 '18 at 6:13
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%2f53291213%2fcompiling-a-nested-list-with-conditional-statement%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