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
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...
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...
6 Exam Hall Tips To Follow, For Every Good Student
Appearing for exams could be stressful for students. Even though they might have prepared well, they could suffer from anxiety, tension etc. These are not good for their health and mind. However, following a few exam preparation tips can save them from all these and help them to score good marks. Let’s find out all...
Top Benefits of e-Learning
With the current trend of the world going digital, electronic renaissance is a new movement that is welcomed by the new generation as it helps makes the lives of millions of people easier and convenient. Along with this rapidly changing movement and gaining popularity of Internet, e-Learning is a new tool that emerging...
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