All Questions

Tagged with
Filter by
Sorted by
Tagged with
0votes
1answer
39views

Add item to existing list and be able to print list for each item in range in new line

Hi am new just started Computer Science at London Met we have programming in Python. In this code am trying to add items to already existing appended list list.append([student_name, student_age, ...
user avatar
0votes
1answer
24views

Unable to compare the time field in dictionary to current time

I am learning python. I am trying to compare the date in Time field of a dictionary with the date in current_time and if it matches, i am trying to print the key , id in JIRA_ISSUE but however, ...
user avatar
  • 5
0votes
2answers
47views

How to split list in chunks in equally in python?

i am trying to add users to my group using pyrogram i have 200 user ids in a list - python list_of_users = [user_id1, user_id2, user_id3, user_id4, ...] i also, have a list of 7 clients, what i waana ...
user avatar
0votes
0answers
7views

How to combine and handle data list in dictionary python

I have a big problem. This is my data.The data structure looks like this. It include metric_name, date_time and data of date_time in metric_name. { "metric_name 1":{ "date_time 1&...
user avatar
  • 15
0votes
2answers
42views

How to remove last character of a string

I have many lines similar to "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|JJJ| |" "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|HHH|" "HHH |**** XYzz| *ABC*hgg|G~GG|G|HJJ|III| ...
user avatar
0votes
1answer
24views

Index out of range when it should not

Hello guys im learning python and im trying to make a string that concentrates binary numbers together baseed on similarity from a list .. so ['1','0','0','0','0','1','1'] becomes '1 0000 11' .. I ...
user avatar
  • 25
0votes
0answers
14views

subclass for NameError: name 'side_length' is not defined

class Polygon(): def __init__(self, side_lengths): self.side_lengths = side_lengths def perimeter(self): return sum(self.side_lengths) def __str__(self): side_lengths = len(self....
user avatar
-1votes
2answers
34views

Python get first k elements from the list, which are present in another list

I have a list1: [1, 2, 3, 4] and another list2: [4, 2, 10, 20, 3, 1] I want to take 1st 3 elements of list2, which are present in list1: Output-list [4, 2, 3] How to do this?...
user avatar
  • 775
-2votes
0answers
23views

Print the stair shape like list using "for" loop in python [closed]

How do I write in python3 to print a list using for loop and loop counter to print a list like the image shown below: Image Here The list is follow: berries = ['Blueberries','Raspberries','...
user avatar
0votes
1answer
39views

Why does this implementation of conversion of string to a list of characters work? [duplicate]

As referenced as Example 3 here for converting a string to a list of characters: l = [] s = 'abc' l[:0]=s print(l) #Output is ['a','b','c'] My question is how intuitive or logical it is that line 3 ...
user avatar
1vote
6answers
98views

How replace all these "if...elif...else" statements with something more concise?

Is there a way to reduce the amount of elif things in my statement? In another question I asked someone said that my code suffered from redundancy and it definitely does. I'm relatively new to python ...
user avatar
0votes
0answers
15views

Python: Function Error TypeError: tuple indices must be integers or slices, not tuple & TypeError: 'int' object is not subscriptable

Have been applying below code on my function, but regularly getting an error. Please can you explain where am i going wrong clusters = np.zeros((len(dataset),1)) def assign(centroids,dataset,clusters,...
user avatar
  • 9
0votes
2answers
32views

remove dict from nested list based on key-value condition with python

Im trying to remove a dict from a list only if the condition is met. there are with key decision = "Remove" in the nested dict, I wish to keep only the elements which with key decision = &...
user avatar
0votes
3answers
48views

Extract the string inside a list if it contains specific pattern [duplicate]

I have a list like this: ques = ['Normal adult dogs have how many teeth?', 'What is the most common training command taught to dogs?', 'What is this?;{;i1.png;};', 'Which part of cats is as unique as ...
user avatar
0votes
0answers
11views

Python-POS machine to generate bill and inventory management

I am new to python, I am doing a pos machine that can genarate the bill and inventory management for my school assignment . I had no idea why my input for the product id will only generate twice but ...
user avatar
0votes
1answer
24views

Find the shortest value in a list of dictionaries

I have a list like this one: players = [ { 'name': 'jowick42', 'stack': [[1, 3], [1, 3], [1, 3]] }, { 'name': 'robot', 'stack': [[2, 3], [2, 3], [2, 3]] } ] ...
user avatar
0votes
2answers
23views

How to make my code more simple by using a loop or someting else?

I have to return a symbol from a dictionary of lists when I am given a [x, y] coordinate. The dictionary of lists is this : GOBBLET_REPRESENTATION = { 1: ["▫", "◇", "◯&...
user avatar
0votes
1answer
30views

cycle through a list while skipping elements (python)

I'm trying to find a way of going through a loop of a single list skipping the first element each time. so for example, if I have a list: lst = [0,1,2,3,4,5,6] and wish to use a for loop to cycle ...
user avatar
-2votes
2answers
47views

creating a menu from user input

so iv been trying to write a code that works like a menu creating program that : 1# take the input from user to define the number of products and there names 2# assign prices separately after taking ...
user avatar
  • 3
0votes
2answers
35views

How do I compile a list from one line in python?

I want to create a list from one line in a file, but I cannot find a way on how to do it, does anybody have any tips on how to do it? "text.txt file" 22 21 20 And this is my latest attempt ...
user avatar
-1votes
1answer
22views

Separating mixed float/string in same element in python list

I have a list like this A = [14, 15.2, '22.6g', '27.28g', '10g', '15.2R', '12.4k'] I would like to split list A it into two lists like B = [14, 15.2, 22.6, 27.28, 10, 15.2, 12.4] C = ['NA', 'NA', 'g'...
user avatar
-1votes
1answer
27views

Issue with printing values in markdown format

Issue i have with the code is when i have two values in each fields for example deviceid: 123,245 and devicemodel: Windows, Linux. When i execute this script i get the output as attached image. i ...
user avatar
  • 1
0votes
1answer
14views

Look for value of dictionaries in a list without loop over the list

I am new to Python and I need some help here. I need to search a number in dictionary in a list. I have a long list that looks like this: userinfo= [{"username": "root", "uid&...
user avatar
  • 3
3votes
4answers
50views

Remove element from one list based on elements in another list

I have Two lists like this: options = [['64', '32', '42', '16'], ['Sit', 'Beg', 'Shake', 'Catch'], ['Lion', 'Dog', 'Cat', 'Puppy']] right_answer = ['42', 'Sit', 'Puppy'] I want to remove the ...
user avatar
-7votes
0answers
45views

i = [4, 5, 8 ,9 ,4,9] from list I want to fetch highest no. but no. should not be repeated if it repeat then fetch next highest no [closed]

import sys def NonRepetedhigh(l, N): map = dict.fromkeys(l, 0); for i in range(N): map[l[i]] += 1; LNRElem = -sys.maxsize; ind = -1; for i in range(N): if (map.get(...
user avatar
0votes
4answers
44views

Getting values from for loop at constant difference

I have a list like this: my_list = ['Normal adult dogs have how many teeth?\n', '64#32#42#16\n', '42\n', '10\n', '20\n', 'What is the most common training command taught to dogs?\n', 'Sit#Beg#Shake#...
user avatar
-3votes
0answers
20views

Python enumerate function in list [duplicate]

i am solving the question of two sum using two pointer technique, Can any one explain this code snippet newList = [i for i, j in enumerate(numss) if j == nums[low] ] Complete Code def findPair(nums, ...
user avatar
0votes
2answers
66views

Python quickest way to combine 2 list of dictionaries into one list of dictionary

ldict1 = [{"id":1, "name":"apple", "colour": "", "type": ""}, {"id":2, "name":"orange", &...
user avatar
  • 65
2votes
1answer
33views

how do I reverse the order of a list with a for loop? [duplicate]

This is what I did and my output. Why is it wrong? routers = ['R1', 'R2', 'R3', 'R4'] for router in routers: print('Router in use is', (router[::-1])) Output: Router in use is 1R Router in use ...
user avatar
  • 31
2votes
3answers
27views

Duplicate numbers in list with list comprehension

I'm trying to duplicate all numbers in a list of alpha values as cleanly as possible but can't figure out how. alphas = [.001, .01, .1, 1, 10, 100] data_alphas = [a for num in alphas for i in range(2)...
user avatar
0votes
1answer
37views

Converting the dictionary inside the list to the nested dictionary

have a list of tuples, called employee_data, where each list element is a tuple that corresponds to a class and a point that a employee can earn. For example, employee_data = [{ "name": "asd", ...
user avatar
1vote
2answers
16views

Defaultdict with tuples, sort tuples within all keys

I have following Dict: defaultdict(<class 'list'>, {'A': [('ID1', 'Frank', 7), ('ID2', 'Peter', 1), ('ID3', 'Geoerge', 6)], 'B': [('ID1', 'FrankGeorge', 2)], 'C': [('ID1', 'Renz', 10), ('ID10', '...
user avatar
  • 13
0votes
0answers
51views

Why are my plots connected - how to plot on multiple figures in a for-loop in Python?

I am new to Python and trying to re-write some of my previous MatLab scripts. I am running into issues plotting within a for-loop. My first plot is 3D and my second plot is 2D. For some reason the ...
user avatar
0votes
1answer
23views

Return large list of tuples with replaced dictionary value

In python, I have a list of tuples (lot) with patient data, as shown below: lot = [('490001', 'A-ARM1', '1', '2', "a", "b"), ('490001', 'A-ARM2', '3', '4', "c", &...
user avatar
0votes
2answers
39views

How to iterate and apply text pre processing on sublists

I have a corpus of 20k rows of twitter data, which I have already lower cased and tokenised using tweet tokenizer. For example: X = [ ["i","love,"to","play","...
user avatar
-1votes
0answers
15views

calculate number of days in last 12 months and update the given list - Python3

I have a list of variable names and the number of days as shown below. variable_name = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10', 'm11', 'm12'] number_days = [31, 28, 31, 30, 31, ...
user avatar
  • 2,451
0votes
0answers
17views

Sorting lists of floats

I would like to sort the list s and in the same manner the list s1. The code below workers for integers (after changing 2.2 to 2 and 20.6 to 20). How to adjust the code for floats, please? s = [2.2, 3,...
user avatar
  • 103
1vote
2answers
17views

How to add "?" sign if the sentence type is questions

I have a list. In this list have various type of sentences suppose question type and general sentences. I want to add question mark "?" sign after end of the sentence if it it is question. ...
user avatar
-1votes
1answer
26views

Creating a function to concatenate strings based on len(array)

I am trying to concatenate a string to send a message via python>telegram My plan is so that the function is modular. It first import lines from a .txt file and based on that many lines it creates ...
user avatar
0votes
2answers
44views

Comparing values between two rows in the same 2D list, returning their percentage delta - Python

For this assignment, I cannot import any modules. I have this list stateList = [['Set', 'state', 2022, 2023, 2024], ['A', 'start', 100, 100, 100],['A', 'end', 110, 100, 90]] I want to append ...
user avatar
  • 31
-2votes
0answers
48views

Why does the "+" operator throw a ValueError here? [closed]

#A l1 = p1.l[:mid] + p2.l[mid:] #B l1 = [*p1.l[:mid],*p2.l[mid:]] So I originally used code "A" to combine two lists at a midpoint, and it worked when it was tested in the file it was ...
user avatar
  • 131
0votes
1answer
24views

Is there a way to use compare a list with a dictionary key, then rearrange that list element to be in the value position?

I have a dictionary that defines the mapping I want. for example, I want list item 2 to be swapped to the list item 5 position # FOR ENCRYPTION - Create dictionary mapping inputs to outputs of pbox of ...
user avatar
1vote
4answers
40views

Casting part of a 2D list to int in Python

I have to convert all string elements of a 2-D list to integers except for the first element. For example, for a list: result = [['foo', '2', '21', '48'], ['bar', '18', '180', '54']] I only wish to ...
user avatar
1vote
1answer
24views

Arranging a list based on the distance of its elements from a given value

Let's assume we have a list like the following [2.3, 1.02, 1.99, 0.99, 0.089, 0, 1.1, -1.1, -2.1] We want to arrange the elements of this list based on their distance from target value equal to 1 in ...
user avatar
  • 25
0votes
1answer
22views

How can i sort a list consisting of strings, and dictionary in python [closed]

E.g items = ["a", "u", {"b":2, "c": 6}, "s", {"f":2,"n":1}] How can i sort this in python. There is nothing i can do about what ...
user avatar
1vote
1answer
41views

Replacing extended slice of a list with another list

I have a list s which looks as below: s = list(range(1, 11)) I am replacing slice of 's' using below code: s[1:4] = [0, 0, 0, 0] print(s) Output: [1, 0, 0, 0, 0, 5, 6, 7, 8, 9, 10] However, when ...
user avatar
  • 11.2k
0votes
3answers
35views

Why is this function overwriting my list?

I just started on a project of making different types of sorting algorithms for fun, but I just can't figure out why this function is overwriting a variable. I have a main file: import bubble test = [...
user avatar
  • 41
0votes
2answers
64views

Replacing slice by an iterable

I have a list s which looks as below: s = [1, 2, 3] I am replacing slice of 's' using below code: s[1:4] = [22, 3, 4, 5, 6, 7] print(s) Output: [1, 22, 3, 4, 5, 6, 7] My understanding s[1:4] should ...
user avatar
  • 11.2k
1vote
1answer
23views

Issues Sorting a list, list out of order

Hi, I'm trying to write a basic GUI using python and pytq5 which displays information received from an API call, when the information is returned from the API it seems to be out of order and i cant ...
user avatar
  • 25
0votes
0answers
17views

initializing lists in a loop give 2 different ids, not one

I'm using Python 3.6.9, I'm confused, why in the code below do I get 2 toggling ids? In [1]: [id([None]*3) for _ in range(5)] ...: ...
user avatar
  • 727

15 30 50 per page
1
2 3 4 5
245