Take BTech Tuition from the Best Tutors
Search in
public class DemoApplication {
public static void main(String[] args) {
//SpringApplication.run(DemoApplication.class, args);
List<Order> list=new ArrayList();
list.add(new Order("34587", 163.8));
list.add(new Order("98762", 284.0));
list.add(new Order("77226", 98.85));
list.add(new Order("88112", 74.97));
System.out.println("Before Processing List orders :: "+list);
int index=-1;
for(Order order: list) {
index++;
if(order.getPrice()<100) {
order.setPrice(order.getPrice()+10);
list.set(index, order);
}
}
System.out.println("After processing List orders :: "+list);
}
}
class Order{
private String orderNo;
private double price;
public Order(String orderNo, double price) {
super();
this.orderNo = orderNo;
this.price = price;
}
public void setOrderNo(String orderNo){
this.orderNo=orderNo;
}
public String getOrderNo(){
return orderNo;
}
public void setPrice(double price){
this.price=price;
}
public double getPrice(){
return price;
}
@Override
public String toString() {
return "Order [orderNo=" + orderNo + ", price=" + price + "]";
}
}
read lessPythonized my previous answer:
OUTPUT = [ ('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97) ]
oL = []
oL.extend([(id, price + 10) for id, price in OUTPUT if price < 100.00 or\
oL.append((id, price))])
OUTPUT = oL
print(OUTPUT)
Hello Anurag,
As per your question,
Input:- a list of tuples with two values (order-number: string, total-order-cost: float) each
Output:- the updated list of tuples.
Condition: When b < 100, b = b + 10 where b: total-order-cost (product of the price per items and the quantity) for every tuple of (a, b) in the input list.
Program:
1) With regular for-loop:
# You might use list(input()) to read list from user input.
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
outputList = []
for _tuple in inputList:
if (_tuple[1] < 100):
outputList.append((_tuple[0], _tuple[1]+10))
else:
outputList.append((_tuple[0],_tuple[1]))
print('Updated List:',outputList)
PS: Since tuples are immutable (cannot change it's value further) in python, we used one more list instead of replacing the given input list itself.
2) With list-comprehension:-
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
def getUpdatedList(inputList):
return [(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList]
outputList = getUpdatedList(inputTuple)
print(outputList)
Or simply,
print([(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList])
Feel free to ask me any further questions. Happy Coding!!!
read less
Hello Anurag,
As per your question,
Input:- a list of tuples with two values (order-number: string, total-order-cost: float) each
Output:- the updated list of tuples.
Condition: When b < 100, b = b + 10 where b: total-order-cost (product of the price per items and the quantity) for every tuple of (a, b) in the input list.
Program:
1) With regular for-loop:
# You might use list(input()) to read list from user input.
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
outputList = []
for _tuple in inputList:
if (_tuple[1] < 100):
outputList.append((_tuple[0], _tuple[1]+10))
else:
outputList.append((_tuple[0],_tuple[1]))
print('Updated List:',outputList)
PS: Since tuples are immutable (cannot change it's value further) in python, we used one more list instead of replacing the given input list itself.
2) With list-comprehension:-
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
def getUpdatedList(inputList):
return [(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList]
outputList = getUpdatedList(inputTuple)
print(outputList)
Or simply,
print([(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList])
Feel free to ask me any further questions. Happy Coding!!!
read less
Hello Anurag,
As per your query,
Input:- a list of tuples with two values (order-number: string, total-order-cost: float) each
Output:- the updated list of tuples.
Condition: When b < 100, b = b + 10 where b: total-order-cost (product of the price per items and the quantity) for every tuple of (a, b) in the input list.
Program:
1) With regular for-loop:
# You might use list(input()) to read list from user input.
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
outputList = []
for _tuple in inputList:
if (_tuple[1] < 100):
outputList.append((_tuple[0], _tuple[1]+10))
else:
outputList.append((_tuple[0],_tuple[1]))
print('Updated List:',outputList)
PS: Since tuples are immutable (cannot change it's value further) in python, we used one more list instead of replacing the given input list itself.
2) With list-comprehension:-
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
def getUpdatedList(inputList):
return [(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList]
outputList = getUpdatedList(inputTuple)
print(outputList)
Or simply,
print([(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList])
Feel free to ask me any further questions. Happy Coding!!!
read less
Hello Anurag,
As per your query,
Input:- a list of tuples with two values (order-number: string, total-order-cost: float) each
Output:- the updated list of tuples.
Condition: When b < 100, b = b + 10 where b: total-order-cost (product of the price per items and the quantity) for every tuple of (a, b) in the input list.
Program:
1) With regular for-loop:
# You might use list(input()) to read list from user input.
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
outputList = []
for _tuple in inputList:
if (_tuple[1] < 100):
outputList.append((_tuple[0], _tuple[1]+10))
else:
outputList.append((_tuple[0],_tuple[1]))
print('Updated List:',outputList)
PS: Since tuples are immutable (cannot change it's value further) in python, we used one more list instead of replacing the given input list itself.
2) With list-comprehension:-
inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)]
def getUpdatedList(inputList):
return [(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList]
outputList = getUpdatedList(inputTuple)
print(outputList)
Or simply,
print([(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList])
Feel free to ask me any further questions. Happy Coding!!!
read less
Input:-a list of tuples with two values (order-number: string, total-order-cost: float) each Output:- the updated list of tuples.
Condition: When b < 100, b = b + 10 where b: total-order-cost (product of the price per items and the quantity) for every tuple of (a, b) in the input list. Program: 1) With regular for-loop: # You might use list(input()) to read list from user input. inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)] outputList = [] for _tuple in inputList: if (_tuple[1] < 100): outputList.append((_tuple[0], _tuple[1]+10)) else: outputList.append((_tuple[0],_tuple[1])) print('Updated List:',outputList)
PS: Since tuples are immutable (cannot change it's value further) in python, we used one more list instead of replacing the given input list itself.
2) With list-comprehension:- inputList = [('34587',163.8),('98762',284.0),('77226',98.85),('88112',74.97)] def getUpdatedList(inputList): return [(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList] outputList = getUpdatedList(inputTuple) print(outputList) Or simply, print([(ele[0], ele[1]+10) if (ele[1]) < 100 else (ele[0], ele[1]) for ele in inputList])
Feel free to ask me any further questions. enjoy coding.
read lessView 22 more Answers
Related Questions
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Tuitions for Professional courses - Does...
MBA, Medicine and engineering are the three main professional courses in India. Engineering is still one of the highly sorted after professional courses in the under graduate level, while MBA is favoured as a preferred post graduate course. To shine in these courses, one needs to work and study hard. Engineering as a...
Finding Tuitions for Bachelor of Engineering...
According to a recent survey conducted by the NCAER (National Council of Advanced Economic Research), engineering is the most sought after course in India. Some engineering courses are offered as BE or Bachelor of Engineering while some as Bachelor in Technology or B.Tech. Since engineering is a professional course, the...
How To Choose The Right School For Children?
Quality education does not only help children to get a successful career and life, but it also hugely contributes to society. The formal education of every child starts from school. Although there are numerous schools, parents find it challenging to choose the right one that would fit their child. It is difficult for them...
The Trend of E-learning and it’s Benefits
E-learning is not just about delivering lessons online. It has a much broader scope that goes beyond manual paper or PowerPoint Presentations. To understand the reach of E-learning and how the whole process works in developing the Educational system, we will discuss a few points here. Let us find out how this new learning...
Looking for BTech Tuition ?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for BTech Tuition Classes are on UrbanPro
The best Tutors for BTech Tuition Classes are on UrbanPro